SQL Server – Get Table Sizes

The script below will retrieve the number of rows, reserved size, data size, index size and unused size. The​​ reserved size​​ is the overall size of the table (it is the total of data size, index size and unused size).

 

 

--DROP TABLE #tmpTableSizes

CREATE​​ TABLE​​ #tmpTableSizes

(

 ​​ ​​ ​​​​ tableName​​ varchar(100),

 ​​ ​​ ​​​​ numberofRows​​ varchar(100),

 ​​ ​​ ​​​​ reservedSize​​ varchar(50),

 ​​ ​​ ​​​​ dataSize​​ varchar(50),

 ​​ ​​ ​​​​ indexSize​​ varchar(50),

 ​​ ​​ ​​​​ unusedSize​​ varchar(50)

)

insert​​ #tmpTableSizes

EXEC​​ sp_MSforeachtable​​ @command1="EXEC sp_spaceused '?'"

 

 

select ​​​​ *​​ from​​ #tmpTableSizes

order​​ by​​ cast(LEFT(dataSize,​​ LEN(dataSize)​​ -​​ 4)​​ as​​ int) ​​​​ desc

 

Knowledge worth sharing...Share on linkedin
Linkedin
Share on facebook
Facebook
Share on google
Google
Share on twitter
Twitter

SQL Server – Change Database State

--Make Database Read Only

 

USE​​ [master]

GO

ALTER​​ DATABASE​​ [TESTDB]​​ SET​​ READ_ONLY​​ WITH​​ NO_WAIT

GO

 

--Make Database Read/Write

 

USE​​ [master]

GO

ALTER​​ DATABASE​​ [TESTDB]​​ SET​​ READ_WRITE​​ WITH​​ NO_WAIT

GO

 

 

 

--If you face error that if database is already in use, you can resolve the same by making database in single user mode

 

--1. Set the database to single mode:

ALTER​​ DATABASE​​ [TESTDB]​​ SET​​ SINGLE_USER​​ WITH​​ ROLLBACK​​ IMMEDIATE;

 

 

 

--2. Set the database to Multi-user mode

ALTER​​ DATABASE​​ [TESTDB]

SET​​ MULTI_USER​​ WITH​​ ROLLBACK​​ IMMEDIATE

 

Knowledge worth sharing...Share on linkedin
Linkedin
Share on facebook
Facebook
Share on google
Google
Share on twitter
Twitter

SQL Server- View Current Isolation Level

To view the current isolation level of SQL Server, execute the script below.

 

SELECT​​ 

CASE​​ transaction_isolation_level​​ 

WHEN​​ 0​​ THEN​​ 'Unspecified'​​ 

WHEN​​ 1​​ THEN​​ 'ReadUncommitted'​​ 

WHEN​​ 2​​ THEN​​ 'ReadCommitted'​​ 

WHEN​​ 3​​ THEN​​ 'Repeatable'​​ 

WHEN​​ 4​​ THEN​​ 'Serializable'​​ 

WHEN​​ 5​​ THEN​​ 'Snapshot'​​ 

END​​ AS​​ TRANSACTION_ISOLATION_LEVEL​​ 

FROM​​ 

sys.dm_exec_sessions​​ 

WHERE​​ 

session_id​​ =​​ @@SPID

 

 

 

Knowledge worth sharing...Share on linkedin
Linkedin
Share on facebook
Facebook
Share on google
Google
Share on twitter
Twitter

Change Data Capture (CDC) T-SQL Useful Commands

Below are some very useful​​ Change Data Capture (CDC) T-SQL Commands.

 

--Check if cdc is enabled

 

USE​​ master​​ 

GO​​ 

SELECT​​ [name],​​ database_id,​​ is_cdc_enabled ​​​​ 

FROM​​ sys.databases ​​ ​​ ​​ ​​ ​​ ​​​​ 

GO ​​ ​​ ​​ ​​​​ 

 

 

--Enabled CDC at database level

 

USE​​ AdventureWorks​​ 

GO​​ 

EXEC​​ sys.sp_cdc_enable_db​​ 

 GO ​​ 

 

 

--Check which tables are CDC enabled

 

 USE​​ AdventureWorks​​ 

GO​​ 

SELECT​​ [name],​​ is_tracked_by_cdc ​​​​ 

FROM​​ sys.tables​​ 

GO ​​​​ 

 

 

--Cnabled CDC for a table

 

USE​​ AdventureWorks​​ 

GO​​ 

EXEC​​ sys.sp_cdc_enable_table​​ 

@source_schema​​ =​​ N'HumanResources',​​ 

@source_name ​​ ​​​​ =​​ N'Shift',​​ 

@role_name ​​ ​​ ​​ ​​​​ =​​ NULL​​ 

GO

 

--Disable CDC on a table​​ 

 

USE​​ AdventureWorks;

GO

EXECUTE​​ sys.sp_cdc_disable_table

@source_schema​​ =​​ N'HumanResources',

@source_name​​ =​​ N'Shift',

@capture_instance​​ =​​ N'HumanResources_Shift';

GO

 

 

--CaptureSelectedColumns

 

EXEC​​ sys.sp_cdc_enable_table​​ 

@source_schema​​ =​​ N'HumanResources',​​ 

@source_name ​​ ​​​​ =​​ N'Shift',​​ 

@role_name ​​ ​​ ​​ ​​​​ =​​ NULL,​​ 

@captured_column_list​​ =​​ '[ShiftID],[Name]'​​ 

GO

 

Knowledge worth sharing...Share on linkedin
Linkedin
Share on facebook
Facebook
Share on google
Google
Share on twitter
Twitter

SQL Server – Restore With VERIFY ONLY

Restore With VERIFY ONLY

 

RESTORE​​ VERIFYONLY​​ FROM ​​​​ DISK​​ =​​ N'H:\Backup_location_directory_Path\backupfile.bak'​​ with​​ NOUNLOAD;​​ 

 

Knowledge worth sharing...Share on linkedin
Linkedin
Share on facebook
Facebook
Share on google
Google
Share on twitter
Twitter

SQL Server – Monitor Backup and Restore activity

This is a cool script to monitor backup and restore activity.

 

SELECT​​ session_id​​ as​​ SPID,​​ command,​​ a.text​​ AS​​ Query,​​ start_time,​​ percent_complete,​​ dateadd(second,estimated_completion_time/1000,​​ getdate())​​ as​​ estimated_completion_time​​ 

FROM​​ sys.dm_exec_requests​​ r​​ CROSS​​ APPLY​​ sys.dm_exec_sql_text(r.sql_handle)​​ a​​ 

WHERE​​ r.command​​ in​​ ('BACKUP DATABASE','RESTORE DATABASE')

 

 

 

Knowledge worth sharing...Share on linkedin
Linkedin
Share on facebook
Facebook
Share on google
Google
Share on twitter
Twitter

SQL Server – Script to Backup a Single Table

Script to backup a single table:

 

select * into newtablename from oldtablename

Knowledge worth sharing...Share on linkedin
Linkedin
Share on facebook
Facebook
Share on google
Google
Share on twitter
Twitter

SQL Server – Always-ON Availability Groups Monitoring Script

Below is the script I use to monitor Always-ON

 

;WITH​​ AG_Stats​​ AS​​ (

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ SELECT​​ AGS.name ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ AS​​ AGGroupName,​​ 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ AR.replica_server_name ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ AS​​ InstanceName,​​ 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ HARS.role_desc,​​ 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ Db_name(DRS.database_id) ​​ ​​ ​​ ​​ ​​ ​​​​ AS​​ DBName,​​ 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ DRS.database_id,​​ 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ AR.availability_mode_desc ​​ ​​ ​​ ​​ ​​​​ AS​​ SyncMode,​​ 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ DRS.synchronization_state_desc​​ AS​​ SyncState,​​ 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ DRS.last_hardened_lsn,​​ 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ DRS.end_of_log_lsn,​​ 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ DRS.last_redone_lsn,​​ 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ DRS.last_hardened_time,​​ -- On a secondary database, time of the log-block identifier for the last hardened LSN (last_hardened_lsn).

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ DRS.last_redone_time,​​ -- Time when the last log record was redone on the secondary database.

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ DRS.log_send_queue_size,​​ 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ DRS.redo_queue_size,

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ --Time corresponding to the last commit record.

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ --On the secondary database, this time is the same as on the primary database.

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ --On the primary replica, each secondary database row displays the time that the secondary replica that hosts that secondary database​​ 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ --  ​​​​ has reported back to the primary replica. The difference in time between the primary-database row and a given secondary-database​​ 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ --  ​​​​ row represents approximately the recovery time objective (RPO), assuming that the redo process is caught up and that the progress​​ 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ --  ​​​​ has been reported back to the primary replica by the secondary replica.

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ DRS.last_commit_time

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ FROM ​​ ​​​​ sys.dm_hadr_database_replica_states​​ DRS​​ 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ LEFT​​ JOIN​​ sys.availability_replicas​​ AR​​ 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ ON​​ DRS.replica_id​​ =​​ AR.replica_id​​ 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ LEFT​​ JOIN​​ sys.availability_groups​​ AGS​​ 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ ON​​ AR.group_id​​ =​​ AGS.group_id​​ 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ LEFT​​ JOIN​​ sys.dm_hadr_availability_replica_states​​ HARS​​ ON​​ AR.group_id​​ =​​ HARS.group_id​​ 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ AND​​ AR.replica_id​​ =​​ HARS.replica_id​​ 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ ),

 ​​ ​​ ​​​​ Pri_CommitTime​​ AS​​ 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ (

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ SELECT ​​​​ DBName

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ ,​​ last_commit_time

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ FROM ​​ ​​ ​​​​ AG_Stats

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ WHERE ​​ ​​​​ role_desc​​ =​​ 'PRIMARY'

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ ),

 ​​ ​​ ​​​​ Rpt_CommitTime​​ AS​​ 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ (

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ SELECT ​​​​ DBName,​​ last_commit_time

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ FROM ​​ ​​ ​​​​ AG_Stats

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ WHERE ​​ ​​​​ role_desc​​ =​​ 'SECONDARY'​​ AND​​ [InstanceName]​​ =​​ 'InstanceNameB-PrimaryDataCenter'

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ ),

 ​​ ​​ ​​​​ FO_CommitTime​​ AS​​ 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ (

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ SELECT ​​​​ DBName,​​ last_commit_time

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ FROM ​​ ​​ ​​​​ AG_Stats

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ WHERE ​​ ​​​​ role_desc​​ =​​ 'SECONDARY'​​ AND​​ ([InstanceName]​​ =​​ 'InstanceNameC-SecondaryDataCenter'​​ OR​​ [InstanceName]​​ =​​ 'InstanceNameD-SecondaryDataCenter')

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ )

SELECT​​ p.[DBName]​​ AS​​ [DatabaseName],​​ p.last_commit_time​​ AS​​ [Primary_Last_Commit_Time]

 ​​ ​​ ​​​​ ,​​ r.last_commit_time​​ AS​​ [Reporting_Last_Commit_Time]

 ​​ ​​ ​​​​ ,​​ DATEDIFF(ss,r.last_commit_time,p.last_commit_time)​​ AS​​ [Reporting_Sync_Lag_(secs)]

 ​​ ​​ ​​​​ ,​​ f.last_commit_time​​ AS​​ [FailOver_Last_Commit_Time]

 ​​ ​​ ​​​​ ,​​ DATEDIFF(ss,f.last_commit_time,p.last_commit_time)​​ AS​​ [FailOver_Sync_Lag_(secs)]

FROM​​ Pri_CommitTime​​ p

LEFT​​ JOIN​​ Rpt_CommitTime​​ r​​ ON​​ [r].[DBName]​​ =​​ [p].[DBName]

LEFT​​ JOIN​​ FO_CommitTime​​ f​​ ON​​ [f].[DBName]​​ =​​ [p].[DBName]

 

Knowledge worth sharing...Share on linkedin
Linkedin
Share on facebook
Facebook
Share on google
Google
Share on twitter
Twitter

SQL Server – Check Always-ON replica States

Below is the script to check Always-ON replica states

 

SELECT​​ 

GETDATE()​​ AS​​ DateTimeCaptured

,r.replica_server_name

,s.role_desc

,d.synchronization_state_desc

,d.log_send_queue_size

,d.log_send_rate

,d.redo_queue_size

,d.redo_rate

,d.truncation_lsn

,d.last_sent_lsn

,d.last_sent_time

,d.last_received_lsn

,d.last_received_time

,d.last_hardened_lsn

,d.last_hardened_time

,d.last_redone_time

,d.last_commit_time

,d.low_water_mark_for_ghosts

FROM​​ sys.availability_replicas r​​ INNER​​ JOIN

 ​​ ​​ ​​ ​​​​ sys.dm_hadr_availability_replica_states s​​ 

ON​​ r.replica_id=s.replica_id​​ INNER​​ JOIN

 ​​ sys.dm_hadr_database_replica_states d

ON​​ s.replica_id=d.replica_id

ORDER​​ BY​​ s.role_desc,​​ r.replica_server_name​​ DESC

 

 

 

 

 

 

Knowledge worth sharing...Share on linkedin
Linkedin
Share on facebook
Facebook
Share on google
Google
Share on twitter
Twitter

SQL Server – Check Disk Usage

Below is the script I use to check disk usage.

USE​​ [master]

 

begin

set​​ nocount​​ on

/*******************************************************/

/* Enabling Ole Automation Procedures */

exec​​ sp_configure​​ 'show advanced options',​​ 1

RECONFIGURE​​ with​​ override

 

exec​​ sp_configure​​ 'Ole Automation Procedures',​​ 1

RECONFIGURE​​ with​​ override

 

 

/*******************************************************/

DECLARE​​ @hr​​ int

DECLARE​​ @fso​​ int

DECLARE​​ @drive​​ char(1)

DECLARE​​ @odrive​​ int

DECLARE​​ @TotalSize​​ varchar(20)​​ DECLARE​​ @MB​​ Numeric​​ ;​​ SET​​ @MB​​ =​​ 1048576

CREATE​​ TABLE​​ #drives​​ (drive​​ char(1)​​ PRIMARY​​ KEY,​​ FreeSpace​​ int​​ NULL,

TotalSize​​ int​​ NULL)​​ INSERT​​ #drives(drive,FreeSpace)​​ EXEC

master.dbo.xp_fixeddrives​​ EXEC​​ @hr=sp_OACreate

'Scripting.FileSystemObject',@fso​​ OUT​​ IF​​ @hr​​ <>​​ 0​​ EXEC​​ sp_OAGetErrorInfo

@fso

DECLARE​​ dcur​​ CURSOR​​ LOCAL​​ FAST_FORWARD

FOR​​ SELECT​​ drive​​ from​​ #drives​​ ORDER​​ by​​ drive

OPEN​​ dcur​​ FETCH​​ NEXT​​ FROM​​ dcur​​ INTO​​ @drive

WHILE​​ @@FETCH_STATUS=0

BEGIN

EXEC​​ @hr​​ =​​ sp_OAMethod​​ @fso,'GetDrive',​​ @odrive​​ OUT,​​ @drive

IF​​ @hr​​ <>​​ 0​​ EXEC​​ sp_OAGetErrorInfo​​ @fso​​ EXEC​​ @hr​​ =

sp_OAGetProperty

@odrive,'TotalSize',​​ @TotalSize​​ OUT​​ IF​​ @hr​​ <>​​ 0​​ EXEC​​ sp_OAGetErrorInfo

@odrive​​ UPDATE​​ #drives​​ SET​​ TotalSize=@TotalSize/@MB​​ WHERE

drive=@drive​​ FETCH​​ NEXT​​ FROM​​ dcur​​ INTO​​ @drive

End

Close​​ dcur

DEALLOCATE​​ dcur

EXEC​​ @hr=sp_OADestroy​​ @fso​​ IF​​ @hr​​ <>​​ 0​​ EXEC​​ sp_OAGetErrorInfo​​ @fso

 

SELECT

drive,​​ cast(TotalSize/1024.0​​ as​​ decimal(8,2))​​ as​​ 'Total(GB)',​​ cast(FreeSpace/1024.0​​ as​​ decimal(8,2))​​ as​​ 'Free(GB)',cast(TotalSize/1024.0-FreeSpace/1024.0​​ as​​ decimal(8,2))​​ as​​ 'Used(GB)',cast((TotalSize/1024.0-FreeSpace/1024.0)/(TotalSize/1024.0)*100​​ as​​ decimal​​ (8,2))​​ as​​ 'PercentUsed'

FROM​​ #drives

ORDER​​ BY​​ drive​​ 

DROP​​ TABLE​​ #drives​​ 

​​ 

/*******************************************************/

/* Disabling Ole Automation Procedures */

exec​​ sp_configure​​ 'show advanced options',​​ 1

RECONFIGURE​​ with​​ override

 

exec​​ sp_configure​​ 'Ole Automation Procedures',​​ 0

RECONFIGURE​​ with​​ override

/*******************************************************/

 

end

 

Knowledge worth sharing...Share on linkedin
Linkedin
Share on facebook
Facebook
Share on google
Google
Share on twitter
Twitter