Skip to content

Commit a36807d

Browse files
committed
Add next batch of standard headers
1 parent dff4e28 commit a36807d

File tree

57 files changed

+2455
-3245
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+2455
-3245
lines changed

Agent Jobs/Script to see running jobs in SQL Server with Job Start Time.sql renamed to Agent Jobs/List running jobs in SQL Server with Job Start Time.sql

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
-- List running jobs in SQL Server with Job Start Time
2+
-- Part of the SQL Server DBA Toolbox at https://github.com/DavidSchanzer/Sql-Server-DBA-Toolbox
3+
-- This script lists all SQL Agent jobs that are currently running.
14
-- From http://dba.stackexchange.com/questions/58859/script-to-see-running-jobs-in-sql-server-with-job-start-time
5+
26
SELECT ja.job_id,
37
j.name AS job_name,
48
ja.start_execution_date,
@@ -14,7 +18,7 @@ FROM msdb.dbo.sysjobactivity ja
1418
AND ISNULL(ja.last_executed_step_id, 0) + 1 = js.step_id
1519
WHERE ja.session_id =
1620
(
17-
SELECT TOP 1
21+
SELECT TOP (1)
1822
session_id
1923
FROM msdb.dbo.syssessions
2024
ORDER BY agent_start_date DESC

Auditing/Create audit for database.sql

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
-- Create audit for database
2+
-- Part of the SQL Server DBA Toolbox at https://github.com/DavidSchanzer/Sql-Server-DBA-Toolbox
3+
-- This script creates server and database audits for the nominated database
14
-- Values to be modified before execution:
25
DECLARE @DBName VARCHAR(100) = '<DatabaseName>';
36
DECLARE @AuditPath VARCHAR(100) = '<PathToAuditsFolder>' + '\' + @DBName;
@@ -6,7 +9,7 @@ DECLARE @createDirSql NVARCHAR(100);
69

710
SET @createDirSql = N'EXEC master.sys.xp_create_subdir ''' + @AuditPath + N'''';
811

9-
EXEC sys.sp_executesql @createDirSql;
12+
EXEC sys.sp_executesql @stmt = @createDirSql;
1013

1114
DECLARE @AuditSql NVARCHAR(1000);
1215

@@ -17,5 +20,5 @@ SET @AuditSql
1720
+ N'CREATE DATABASE AUDIT SPECIFICATION [' + @DBName + N']' + N'FOR SERVER AUDIT [' + @DBName + N']'
1821
+ N'ADD (SCHEMA_OBJECT_ACCESS_GROUP) WITH (STATE = ON);';
1922

20-
EXEC sys.sp_executesql @AuditSql;
23+
EXEC sys.sp_executesql @stmt = @AuditSql;
2124
GO

Auditing/Drop server audits for which there is no corresponding database.sql

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
-- Drop server audits for which there is no corresponding database
2+
-- Part of the SQL Server DBA Toolbox at https://github.com/DavidSchanzer/Sql-Server-DBA-Toolbox
3+
-- This script generates the SQL to drop the server audits for those databases that no longer exist
4+
15
SELECT 'ALTER SERVER AUDIT [' + name + '] WITH (STATE=OFF); DROP SERVER AUDIT [' + name + ']' AS DropCommand,
26
sa.audit_id,
37
sa.name,

Auditing/Query an audit file.sql

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
-- Query an audit file
2+
-- Part of the SQL Server DBA Toolbox at https://github.com/DavidSchanzer/Sql-Server-DBA-Toolbox
3+
-- This script returns all relevant columns from a database audit for a nominated login and date/time range
14
-- Values to be modified before execution:
25
DECLARE @DBName VARCHAR(100) = '<DatabaseName>';
36
DECLARE @AuditPath VARCHAR(100) = '<PathToAuditsFolder>' + '\' + @DBName;

Availability Groups/Failover all Availability Groups.sql

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
-- Run this script on the Secondary node
1+
-- Failover all Availability Groups
2+
-- Part of the SQL Server DBA Toolbox at https://github.com/DavidSchanzer/Sql-Server-DBA-Toolbox
3+
-- This script executes a manual failover for each Availability Group on the instance
4+
-- ** Run this script on the SECONDARY node **
25
DECLARE @name sysname,
36
@sql VARCHAR(255);
47

Availability Groups/Manually add database to an AG - needed for databases with a database master key.sql

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
-- Manually add database to an AG - needed for databases with a database master key
2+
-- Part of the SQL Server DBA Toolbox at https://github.com/DavidSchanzer/Sql-Server-DBA-Toolbox
3+
-- This script, which must be run in SQLCMD mode, will use the backup-and-restore method of initialising the secondary node
4+
-- Make the appropriate global replacements of <AGName>, <PrimaryNode>, <SecondaryNode>, <DBName>, <FullBackupPath> and <LogBackupPath>
5+
16
:CONNECT <PrimaryNode>
27
ALTER AVAILABILITY GROUP <AGName>
38
ADD DATABASE <DBName>;
49
GO
510
BACKUP DATABASE <DBName>
6-
TO DISK = '<FullBackupPath>';
11+
TO DISK = '<FullBackupPath>';
712
GO
813
:CONNECT <SecondaryNode>
914
RESTORE DATABASE <DBName>
@@ -20,3 +25,4 @@ WITH NORECOVERY;
2025
GO
2126
:CONNECT <PrimaryNode>
2227
ALTER DATABASE <DBName> SET HADR AVAILABILITY GROUP = <AGName>;
28+
GO

Availability Groups/Read-only routing url generation script.sql

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
--
2-
-- From http://blogs.msdn.com/b/mattn/archive/2012/04/25/calculating-read-only-routing-url-for-alwayson.aspx
3-
--
4-
-- Read-only routing url generation script.
5-
-- Connect to each replica in your AlwaysOn cluster and run this script to get the read_only_routing_url for the replica.
1+
-- Read-only routing url generation script
2+
-- Part of the SQL Server DBA Toolbox at https://github.com/DavidSchanzer/Sql-Server-DBA-Toolbox
3+
-- This script connects to each replica in your AlwaysOn cluster and run this script to get the read_only_routing_url for the replica.
64
-- Then set this to the read_only_routing_url for the availability group replica =>
75
-- alter availability group MyAvailabilityGroup modify replica on N'ThisReplica' with (secondary_role(read_only_routing_url=N'<url>'))
6+
-- From http://blogs.msdn.com/b/mattn/archive/2012/04/25/calculating-read-only-routing-url-for-alwayson.aspx
7+
88
PRINT 'Read-only-routing url script v.2012.1.24.1';
99

1010
PRINT 'This SQL Server instance version is [' + CAST(SERVERPROPERTY('ProductVersion') AS VARCHAR(256)) + ']';
@@ -143,7 +143,7 @@ BEGIN -- if server supports read-only routing
143143
SET @ror_url = '';
144144

145145
-- Get first available port for instance.
146-
SELECT TOP 1 -- Select first matching port
146+
SELECT TOP (1) -- Select first matching port
147147
@instance_port = port
148148
FROM sys.dm_tcp_listener_states
149149
WHERE type = 0 -- Type 0 = TSQL (to avoid mirroring endpoint)

Availability Groups/View read-only routing configurations.sql

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
--
1+
-- View read-only routing configurations
2+
-- Part of the SQL Server DBA Toolbox at https://github.com/DavidSchanzer/Sql-Server-DBA-Toolbox
3+
-- This script returns information on any read-only routing configurations that have been set up on this instance
24
-- From http://msdn.microsoft.com/en-au/library/hh710054.aspx
3-
--
5+
46
SELECT replica_server_name,
57
endpoint_url,
68
secondary_role_allow_connections_desc,
79
backup_priority,
810
read_only_routing_url
911
FROM sys.availability_replicas;
12+
1013
SELECT replica_id,
1114
routing_priority,
1215
read_only_replica_id
1316
FROM sys.availability_read_only_routing_lists;
17+
1418
SELECT dns_name,
1519
port,
1620
is_conformant,

Backup and Restore/Generate RESTORE script for all user databases.sql

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
-- Generate RESTORE script for all user databases
2+
-- Part of the SQL Server DBA Toolbox at https://github.com/DavidSchanzer/Sql-Server-DBA-Toolbox
3+
-- This script queries the msdb database on the current instance to return the RESTORE DATABASE and RESTORE LOG statements
4+
-- to recover databases to the latest backup. Note that Differential backups are not included.
5+
16
EXEC dbo.sp_ineachdb @command = '
27
DECLARE @databaseName sysname;
38
DECLARE @backup_set_id_start INT;

Backup and Restore/Progress of BACKUP and RESTORE.sql

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
-- Progress of BACKUP and RESTORE
2+
-- Part of the SQL Server DBA Toolbox at https://github.com/DavidSchanzer/Sql-Server-DBA-Toolbox
3+
-- This script displays information on the progress of currently running BACKUP and RESTORE commands
4+
15
SELECT der.percent_complete,
26
der.start_time,
37
DATEADD(n, (der.estimated_completion_time / 60 / 1000), GETDATE()) AS estimated_completion_time,

Backup and Restore/Update database backup schedules.sql

+5-74
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
-- Update database backup schedules
2+
-- Part of the SQL Server DBA Toolbox at https://github.com/DavidSchanzer/Sql-Server-DBA-Toolbox
3+
-- This script updates the schedule for the Ola Hallengren SQL Agent jobs "DatabaseBackup - USER_DATABASES - FULL" and "DatabaseBackup - USER_DATABASES - DIFF".
4+
-- The full backup will be on a random day of the week at a random time between 7pm and 11pm, and the differential backup will be likewise but daily.
5+
16
SET NOCOUNT ON;
27

38
-- 1. Update the schedule for the Full backup job to a random day of the week and a random time between 7pm and 11pm
49
DECLARE @schedule_id INT,
510
@freq_interval INT,
611
@random_time INT,
7-
@job_id UNIQUEIDENTIFIER,
8-
@name sysname,
9-
@command NVARCHAR(MAX),
1012
@message VARCHAR(1000);
1113

1214
-- To generate a random integer between @Upper and @Lower: ROUND(((@Upper - @Lower) * RAND() + @Lower), 0)
@@ -92,74 +94,3 @@ EXEC [msdb].[dbo].[sp_update_schedule] @schedule_id = @schedule_id,
9294

9395
SET @message = 'Differential database backup job: updated schedule to every day at ' + CAST(@random_time AS CHAR(6));
9496
PRINT @message;
95-
96-
-- 3. Update the @Directory parameter for all Ola Hallengren database backup jobs from <FromValue> to <ToValue>
97-
DECLARE @test TABLE
98-
(
99-
[step_id] INT NULL,
100-
[step_name] sysname NULL,
101-
[subsystem] NVARCHAR(40) NULL,
102-
[command] NVARCHAR(MAX) NULL,
103-
[flags] INT NULL,
104-
[cmdexec_success_code] INT NULL,
105-
[on_success_action] TINYINT NULL,
106-
[on_success_step_id] INT NULL,
107-
[on_fail_action] TINYINT NULL,
108-
[on_fail_step_id] INT NULL,
109-
[server] sysname NULL,
110-
[database_name] sysname NULL,
111-
[database_user_name] sysname NULL,
112-
[retry_attempts] INT NULL,
113-
[retry_interval] INT NULL,
114-
[os_run_priority] INT NULL,
115-
[output_file_name] NVARCHAR(200) NULL,
116-
[last_run_outcome] INT NULL,
117-
[last_run_duration] INT NULL,
118-
[last_run_retries] INT NULL,
119-
[last_run_date] INT NULL,
120-
[last_run_time] INT NULL,
121-
[proxy_id] INT NULL
122-
);
123-
124-
DECLARE [job_cur] CURSOR LOCAL FAST_FORWARD FOR
125-
SELECT [job_id],
126-
[name]
127-
FROM [msdb].[dbo].[sysjobs]
128-
WHERE [name] LIKE 'DatabaseBackup%'
129-
FOR READ ONLY;
130-
131-
OPEN [job_cur];
132-
133-
FETCH [job_cur]
134-
INTO @job_id,
135-
@name;
136-
137-
WHILE @@FETCH_STATUS = 0
138-
BEGIN
139-
DELETE FROM @test;
140-
141-
INSERT INTO @test
142-
EXEC [msdb].[dbo].[sp_help_jobstep] @job_id = @job_id, @step_id = 1;
143-
144-
SELECT @command = [command]
145-
FROM @test;
146-
147-
IF CHARINDEX(N'<FromValue>', @command) > 0
148-
BEGIN
149-
SET @command = REPLACE(@command, N'<FromValue>', N'<ToValue>');
150-
151-
EXEC [msdb].[dbo].[sp_update_jobstep] @job_id = @job_id,
152-
@step_id = 1,
153-
@command = @command;
154-
155-
SET @message = @name + ': updated command to ' + @command;
156-
PRINT @message;
157-
END;
158-
159-
FETCH [job_cur]
160-
INTO @job_id,
161-
@name;
162-
END;
163-
164-
CLOSE [job_cur];
165-
DEALLOCATE [job_cur];

0 commit comments

Comments
 (0)