Skip to content

Instantly share code, notes, and snippets.

View petesql's full-sized avatar

Peter Whyte petesql

View GitHub Profile
@petesql
petesql / get-db-log-space-used.sql
Created February 26, 2024 13:42
Get db log space usage information
-- Get db log space usage information
CREATE TABLE #LogSpaceUsage (
DatabaseName NVARCHAR(100),
TotalLogSize_MB DECIMAL(18, 2),
LogSpaceUsed_MB DECIMAL(18, 2),
LogSpaceUsed_Percent DECIMAL(5, 2),
LogSpaceFree_MB DECIMAL(18, 2)
)
-- Loop through all databases
@petesql
petesql / quick-performance-troubleshooting-script.sql
Last active February 22, 2024 18:55
MSSQL Quick Performance Troubleshooting Script
-- MSSQL Quick Performance Troubleshooting Script
SELECT
s.host_name,
s.program_name,
r.blocking_session_id 'Blk by',
Coalesce(Quotename(Db_name(st.dbid)) + N'.' + Quotename(Object_schema_name(st.objectid,st.dbid)) + N'.' +
Quotename(Object_name(st.objectid,st.dbid)), '') AS command_text,
s.session_id,
r.cpu_time,
r.logical_reads,
@petesql
petesql / sql-server-get-database-file-information.sql
Created February 20, 2024 00:37
SQL Server Get Database File Information
-- Get db filenames, paths, sizes and growth info
SELECT DB_NAME(database_id) AS 'Database Name',
file_id, name, physical_name, type_desc, state_desc,
is_percent_growth, growth,
CONVERT(bigint, growth/128.0) AS 'Growth in MB',
CONVERT(bigint, size/128.0) AS 'Total Size in MB', max_size
FROM sys.master_files WITH (NOLOCK)
ORDER BY DB_NAME(database_id), file_id OPTION (RECOMPILE);
@petesql
petesql / sql-server-get-cpu-utilization-history.sql
Last active February 18, 2024 02:06
SQL Server Get CPU Utilization History
-- Get CPU Utilization History for last 4hrs (one minute intervals)
DECLARE @ts_now bigint = (SELECT ms_ticks FROM sys.dm_os_sys_info WITH (NOLOCK));
SELECT
SQLProcessUtilization,
SystemIdle As SystemIdleProcess,
100 - SystemIdle - SQLProcessUtilization AS OtherProcessCPUUtilization,
DATEADD(ms, -1 * (@ts_now - timestamp), GETDATE()) AS EventTime
FROM ( SELECT record.value('(./Record/@id)[1]', 'int') AS record_id,
record.value('(./Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int') AS SystemIdle,
record.value('(./Record/SchedulerMonitorEvent/SystemHealth/ProcessUtilization)[1]', 'int') AS SQLProcessUtilization, timestamp
@petesql
petesql / get-sql-agent-jobs-with-job-steps.sql
Last active February 17, 2024 22:53
Get SQL Agent Jobs with Job Steps
-- Get all SQL Agent Jobs with Job Steps
SELECT
s.name AS JobName,
dp.name AS JobOwner,
sc.name AS JobCategory,
s.description AS JobDescription,
CASE s.enabled
WHEN 1 THEN 'Yes'
WHEN 0 THEN 'No'
END AS IsEnabled,
@petesql
petesql / get-all-sql-server-agent-jobs.sql
Last active February 17, 2024 22:49
Get all SQL Server Agent Jobs
-- List all SQL Agent Jobs
SELECT
s.name AS JobName,
dp.name AS JobOwner,
sc.name AS JobCategory,
s.description AS JobDescription,
CASE s.enabled
WHEN 1 THEN 'Yes'
WHEN 0 THEN 'No'
END AS IsEnabled,
@petesql
petesql / generate-sql-backup-all-databases.sql
Created February 2, 2024 19:40
Generate SQL to Backup All Databases SQL Server
-- Generate SQL for backing up all databases
DECLARE @BackupDirectory NVARCHAR(255) = 'D:\mssql_backups'; -- Replace with your desired backup directory
DECLARE @Compression BIT = 1; -- 1 for compression, 0 for no compression
DECLARE @NumberOfFiles INT = 2; -- Specify the desired number of backup files
DECLARE @BackupDate SMALLDATETIME = CONVERT(SMALLDATETIME, GETDATE(), 101); -- Get current date with MM/DD/YYYY format
DECLARE @FormattedBackupDate NVARCHAR(20) = REPLACE(REPLACE(CONVERT(NVARCHAR(20), @BackupDate, 106), ' ', '_'), ',', '');
DECLARE @BackupFiles TABLE (FileIndex INT, DatabaseName NVARCHAR(255), FileName NVARCHAR(255));
-- Generate backup file names excluding tempdb
INSERT INTO @BackupFiles
@petesql
petesql / generate-sql-for-database-full-backup.sql
Last active February 2, 2024 22:12
Generate SQL for a Database Full Backup
-- Generate SQL for running a full database backup
DECLARE @DatabaseName NVARCHAR(255) = 'Jupiter'; -- Replace with your actual database name
DECLARE @BackupDirectory NVARCHAR(255) = 'D:\mssql_backups'; -- Replace with your desired backup directory
DECLARE @Compression BIT = 1; -- 1 for compression, 0 for no compression
DECLARE @NumberOfFiles INT = 1; -- Specify the desired number of backup files
DECLARE @BackupDate SMALLDATETIME = CONVERT(SMALLDATETIME, GETDATE(), 101); -- Get current date with MM/DD/YYYY format
DECLARE @FormattedBackupDate NVARCHAR(20) = REPLACE(REPLACE(CONVERT(NVARCHAR(20), @BackupDate, 106), ' ', '_'), ',', '');
@petesql
petesql / disable-all-sql-agent-jobs.sql
Created February 2, 2024 16:31
Disable All SQL Agent Jobs
-- Generate SQL script to disable enabled SQL Server Agent jobs
DECLARE @job_id UNIQUEIDENTIFIER
DECLARE @sqlScript NVARCHAR(MAX) = ''
DECLARE job_cursor CURSOR FOR
SELECT job_id
FROM msdb.dbo.sysjobs
WHERE enabled = 1; -- Only select jobs that are currently enabled
OPEN job_cursor
@petesql
petesql / sql-server-users-and-permissions-audit.sql
Last active January 22, 2024 21:36
SQL Server User & Permissions Audit Script
-- SQL Server User & Permissions Audit Script
USE master
GO
DECLARE
@errorMessage nvarchar(4000),
@errorNumber int,
@errorSeverity int,
@errorState int,
@errorLine int,