Posts

Showing posts from May, 2022

🧱 Identifying Head Blockers in SQL Server

Many DBAs find it hard to quickly identify what is blocking other processes in SQL Server. Here's a standalone script that helps you identify the head blockers and the sessions they are blocking. It uses sp_who2 , DBCC INPUTBUFFER , and a bit of cursor logic to tie it all together. 🔍 Tip: Run this in SQL Server Management Studio (SSMS) and set the output to "Results in Text" using CTRL+T . SET NOCOUNT ON DECLARE @spid VARCHAR(10) DECLARE @blkby VARCHAR(10) DECLARE @stmt VARCHAR(100) IF NOT EXISTS (SELECT TOP 1 name FROM tempdb..sysobjects WHERE name LIKE '#temp%') BEGIN CREATE TABLE #temp ( spid INT, status VARCHAR(100), login VARCHAR(50), hostname VARCHAR(25), blkby VARCHAR(10), dbname VARCHAR(25), command VARCHAR(100), cputime INT, diskio INT, lastbatch VARCHAR(25), programname VARCHAR(255), spid2 INT ) END ELSE BEGIN TRUNCATE TABLE #temp END INSERT INTO #temp EXEC sp_who2 DECLARE curs CURSOR FOR SELECT CONVERT(VARCHA...

How to SELECT Data in SQL – Basic Queries Explained

How to SELECT Data in SQL – Basic Queries Explained When you're working with databases, one of the most important things you'll do is retrieve data using SELECT queries. Whether you're checking that your data has been inserted correctly, or analyzing information, learning how to select data is a core SQL skill. Let’s walk through several useful ways to SELECT data from a table. 1. Counting Rows in a Table To count how many rows exist in a table, we use the COUNT(*) function. This is useful to quickly check how many records you have. SELECT COUNT(*) FROM test; Example Output: +----------+ | count(*) | +----------+ | 148 | +----------+ 1 row in set (0.00 sec) This tells us that the test table currently contains 148 rows. 2. Selecting All Data from a Table Once you've inserted data into a table, you’ll want to make sure it’s stored correctly. The most basic way to do this is to select all columns and rows using SELECT * . SELECT * FROM te...