Tag: T-SQL
-
RAND() vs NEWID() in SQL Server
Most people searching for “random numbers in SQL Server” want one of two things: SQL Server gives you two common tools for this: RAND() and NEWID(). They solve different problems, and confusion usually starts when they are treated as interchangeable. This post shows how to use both, starting with simple examples, and explains why they…
Written by
-
Fix “Msg 207: Invalid Column Name” in SQL Server
Msg 207 invalid column name: the column is missing, misspelt, in another schema or table, or the query runs against a different database than you think.
Written by
-
What WITH (NOLOCK) Actually Does in SQL Server
Almost every SQL Server developer has copied WITH (NOLOCK) onto the end of a query at some point, usually because someone said it “makes queries faster” or “avoids blocking.” Both of those things are technically true. What rarely gets explained is what you’re trading away to get them. WITH (NOLOCK) doesn’t make a query faster…
Written by
-
Get Current Date & Time in SQL Server
Use GETDATE() (or SYSDATETIME() for more precision) for the current date and time, and CAST(GETDATE() AS date) for today’s date. There is no NOW() in SQL Server. Bring the habit over from MySQL or PostgreSQL and you get this: Msg 195 · Level 15 · State 10 ‘NOW’ is not a recognized built-in function name.…
Written by
-
How to Kill a SPID in SQL Server
⚡Find the head blocker, then KILL 69; with its session id. Whatever it had not committed rolls back, which can take longer than the work did and cannot be stopped. Watch it with KILL 69 WITH STATUSONLY;. In SQL Server, every connection to the database engine is assigned a Session Process ID, commonly known as…
Written by
-
How to Check Blocking SPIDs in SQL Server
Blocking is one of the most common causes of performance issues in SQL Server. When one session holds a lock on a resource and another session needs that same resource, the second session waits. If that wait persists, users experience slowness. Understanding how to quickly identify blocking SPIDs is a core DBA skill. This guide…
Written by
-
Deleting Rows in Batches in SQL Server
A DELETE that removes hundreds of thousands of rows in one go is the one that fills the transaction log, locks the whole table and leaves everything else waiting. Delete in batches instead: small transactions whose log space can be reused, and a lock footprint other sessions can work around. Here is the loop, and…
Written by
-
Applying Data Retention Safely in SQL Server
Data retention in SQL Server usually means deleting old data in a controlled and repeatable way. That might be driven by compliance requirements, table growth that’s starting to hurt performance, SQL Server Express size limits, or simply keeping log and audit tables under control. Whether this is a one-off cleanup or a permanent maintenance task,…
Written by
-
Understanding WHERE 1=1 and WHERE 1=2 in SQL Queries
⚡Neither changes the plan or the speed of a query. WHERE 1=1 lets every real condition start with AND, so conditions can be commented out or appended in dynamic SQL. WHERE 1=2 returns no rows without reading the table, which is how SELECT … INTO copies a table’s columns and nothing else. You have opened…
Written by
-
What the USE Command Does in SQL Server
The USE command changes the database context for the current session. Every statement in SQL Server runs in the context of a database. When you run USE, you are telling SQL Server which database subsequent statements should execute against until the context changes again or the session ends. That single behaviour explains a lot of…
Written by
- Tune a Slow Query With Copilot in SSMS
- xp_readerrorlog: Read the SQL Server Error Log with T-SQL
- Cannot Drop Database Because It Is Currently in Use (Error 3702)
- Could Not Drop Login Because the User Is Currently Logged In (Error 15434)
- Set a Maximum Database File Size in SQL Server (MAXSIZE)
- SQL Server Activity Monitor in SSMS (and the Performance Dashboard)
- Backups & Recovery (18)
- DBA Scripts (154)
- High Availability (HA) (13)
- Installation & Configuration (60)
- Maintenance (25)
- Migration & Upgrades (14)
- Monitoring (10)
- Performance Tuning (38)
- Security (Encryption & Permissions) (25)
- Storage & Capacity (18)
- T-SQL Fundamentals (17)
- Troubleshooting (108)
- Wait Types (236)