Monday, September 9, 2024

How do I find blocked sessions and the session that is blocking them?

 To identify blocked sessions and the session causing the block:

SELECT blocking_session_id AS BlockingSession, session_id AS BlockedSession, wait_type, wait_time, wait_resource FROM sys.dm_exec_requests WHERE blocking_session_id <> 0;

This query shows all sessions that are currently blocked and the blocking session’s ID.

How can I backup a database in SQL Server?

 You can backup a database using the following command:

BACKUP DATABASE YourDatabaseName TO DISK = 'C:\Backup\YourDatabaseName.bak';

This command creates a backup of the YourDatabaseName database to the specified location.

How do I enable a query execution plan in SQL Server?

  To enable the execution plan for a query, run:

SET SHOWPLAN_ALL ON;

Then run your query. The execution plan will be displayed as part of the result. To turn off the execution plan, use:

SET SHOWPLAN_ALL OFF;

How do I find the largest tables in a SQL Server database?

 You can use the following query to find the largest tables by data size:

SELECT t.NAME AS TableName, p.rows AS RowCounts, SUM(a.total_pages) * 8 AS TotalSpaceKB, SUM(a.used_pages) * 8 AS UsedSpaceKB, (SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB FROM sys.tables t INNER JOIN sys.indexes i ON t.OBJECT_ID = i.object_id INNER JOIN sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id INNER JOIN sys.allocation_units a ON p.partition_id = a.container_id WHERE t.NAME NOT LIKE 'dt%' AND i.OBJECT_ID > 255 GROUP BY t.Name, p.Rows ORDER BY TotalSpaceKB DESC;


This query returns the largest tables by row count and space usage.

How can I retrieve a list of all users in a specific SQL Server database?

 You can get the list of all users in a database using this query:

SELECT name AS Username FROM sys.database_principals WHERE type IN ('S', 'U') -- 'S' for SQL user, 'U' for Windows user AND name NOT LIKE '##%'; -- Exclude system users

This lists all users in the current database.

How can I find out the currently running queries in SQL Server?

 Use the following query to find currently running queries:

SELECT r.session_id, r.start_time, s.text AS query_text, r.status, r.command, r.total_elapsed_time FROM sys.dm_exec_requests r CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) s;

This returns details about all currently running queries, including the session ID, query text, and execution status.

How do I check the status of a SQL Server transaction?

 You can check the status of running transactions using this query:

SELECT session_id, transaction_id, database_id, transaction_begin_time, transaction_state, transaction_status

FROM sys.dm_tran_active_transactions;

This query gives you information about active transactions in SQL Server.

How do I list all tables in a specific database?

 You can use the following query to list all tables in the current database:

SELECT TABLE_NAME 

FROM INFORMATION_SCHEMA.TABLES

WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_CATALOG = 'YourDatabaseName';

This query returns the names of all base tables in the specified database.


How can I check the size of a specific database in SQL Server?

 You can check the size of a database using the following query:

EXEC sp_spaceused;

Or, for a specific database:

USE YourDatabaseName;

EXEC sp_spaceused;

This command returns the database size, reserved space, unallocated space, and more.

How do I find the SPID (Session Process ID) for a specific database in SQL Server?

 SELECT 

    es.session_id AS SPID, 

    es.login_name, 

    es.status, 

    es.host_name, 

    es.program_name, 

    es.database_id, 

    DB_NAME(es.database_id) AS database_name,

    ec.client_net_address,

    er.wait_type, 

    er.wait_time, 

    er.blocking_session_id

FROM sys.dm_exec_sessions es

LEFT JOIN sys.dm_exec_connections ec ON es.session_id = ec.session_id

LEFT JOIN sys.dm_exec_requests er ON es.session_id = er.session_id

WHERE es.database_id = DB_ID('YourDatabaseName');

------------------------------------------------------------------------------------

kill SSID