This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- logging sp_who2 results to table | |
CREATE TABLE #sp_who2 ( | |
SPID INT, | |
Status VARCHAR(MAX), | |
Login VARCHAR(MAX), | |
HostName VARCHAR(MAX), | |
BlkBy VARCHAR(MAX), | |
DBName VARCHAR(MAX), | |
Command VARCHAR(MAX), | |
CPUTime INT, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Show all SQL Agent Jobs (without steps) | |
SELECT * FROM msdb.dbo.sysjobs; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- List all SQL Agent jobs with schedule details | |
SELECT | |
j.job_id, | |
j.name, | |
j.description, | |
j.enabled, | |
j.date_created, | |
j.date_modified, | |
s.step_id, | |
s.step_name, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- show exclusive locks on tables for a database | |
DECLARE @DatabaseName NVARCHAR(128) = 'database01' | |
DECLARE @DatabaseID INT = DB_ID(@DatabaseName) | |
DECLARE @SQL NVARCHAR(MAX) | |
SET @SQL = N' | |
USE ' + QUOTENAME(@DatabaseName) + '; | |
SELECT | |
ses.login_name AS UserName, | |
ses.session_id AS SessionID, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Create temp table for storing database size information | |
DECLARE @command NVARCHAR(MAX); | |
DROP TABLE IF EXISTS #MSSQL_Database_Sizes; | |
CREATE TABLE #MSSQL_Database_Sizes ( | |
[database_name] NVARCHAR(MAX) NULL, | |
current_size_mb INT NULL, | |
freespace_mb INT NULL, | |
collection_date DATE NULL | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Show last backups on all databases | |
SELECT | |
ISNULL(d.[name], bs.[database_name]) AS [Database], d.recovery_model_desc AS [Recovery Model], | |
MAX(CASE WHEN [type] = 'D' THEN bs.backup_finish_date ELSE NULL END) AS [Last Full Backup], | |
MAX(CASE WHEN [type] = 'I' THEN bs.backup_finish_date ELSE NULL END) AS [Last Differential Backup], | |
MAX(CASE WHEN [type] = 'L' THEN bs.backup_finish_date ELSE NULL END) AS [Last Log Backup] | |
FROM | |
sys.databases AS d WITH (NOLOCK) | |
LEFT OUTER JOIN msdb.dbo.backupset AS bs WITH (NOLOCK) | |
ON bs.[database_name] = d.[name] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Prepare SQL for TempDB Database File Move | |
SELECT 'ALTER DATABASE tempdb MODIFY FILE (NAME = [' + f.name + '],' | |
+ ' FILENAME = ''Z:\MSSQL\DATA\' + f.name | |
+ CASE WHEN f.type = 1 THEN '.ldf' ELSE '.mdf' END | |
+ ''');' | |
FROM sys.master_files f | |
WHERE f.database_id = DB_ID(N'tempdb'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- This query retrieves and suggests unused index information from the DMV (Dynamic Management Views) in SQL Server. | |
-- It identifies unused or rarely used non-clustered indexes and generates DROP INDEX statements for them. | |
SELECT TOP 25 | |
o.name AS ObjectName, | |
i.name AS IndexName, | |
i.index_id AS IndexID, | |
dm_ius.user_seeks AS UserSeek, | |
dm_ius.user_scans AS UserScans, | |
dm_ius.user_lookups AS UserLookups, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- This query retrieves and suggests missing index information from the DMV (Dynamic Management Views) in SQL Server. | |
-- It calculates an improvement measure for each missing index and generates a CREATE INDEX statement to address the missing index. | |
SELECT | |
CONVERT(varchar(30), GETDATE(), 126) AS runtime, | |
mig.index_group_handle, | |
mid.index_handle, | |
CONVERT(decimal(28, 1), migs.avg_total_user_cost * migs.avg_user_impact * (migs.user_seeks + migs.user_scans)) AS improvement_measure, | |
'CREATE INDEX missing_index_' + | |
CONVERT(varchar, mig.index_group_handle) + '_' + |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Script to retrieve index fragmentation levels in SQL Server | |
-- This script will list fragmented indexes in the database, showing table name, index name, fragmentation percentage, and more | |
SELECT | |
OBJECT_NAME(s.OBJECT_ID) AS TableName, -- Table name where the index is located | |
i.name AS IndexName, -- Name of the index | |
ROUND(s.avg_fragmentation_in_percent, 2) AS FragmentationPercentage, -- Fragmentation percentage rounded to 2 decimal places | |
s.page_count AS PageCount, -- Number of pages used by the index | |
i.type_desc AS IndexType, -- Type of the index (Clustered, Nonclustered, etc.) | |
s.stats_id AS StatsID -- Statistics ID for the index | |
FROM |
NewerOlder