Category: T-SQL Fundamentals
-
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 Add Columns to Tables in SQL Server
Adding a column is one line of T-SQL, and on most tables it finishes at once. The incidents come from two places: an ALTER TABLE that waits for a schema lock and blocks every query that arrives behind it, and a default value that makes SQL Server rewrite every row. This post covers the safe…
Written by
-
Kill All User Sessions on a Database in SQL Server
When you need to drop, restore, or change database settings, active user sessions often get in the way. SQL Server will block these operations until all connections are cleared. In busy environments, waiting is rarely practical. If you arrived here from Msg 5030, the database could not be exclusively locked, or from a restore that…
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
-
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)