forked from nate-hughes/dba-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f34c67f
commit d9955f0
Showing
15 changed files
with
398 additions
and
69 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
DECLARE @default_trace_path VARCHAR(500) | ||
,@tracefilename VARCHAR(500) | ||
,@indx INT; | ||
|
||
SET @default_trace_path = (SELECT path FROM sys.traces WHERE is_default = 1); | ||
SET @default_trace_path = REVERSE(@default_trace_path); | ||
SELECT @indx = PATINDEX('%\%', @default_trace_path); | ||
SET @default_trace_path = REVERSE(@default_trace_path); | ||
SET @tracefilename = LEFT( @default_trace_path,LEN(@default_trace_path) - @indx) + '\log.trc'; | ||
|
||
SELECT SUBSTRING(CONVERT(NVARCHAR(MAX),TEXTData),36, PATINDEX('%executed%',TEXTData)-36) [Command] | ||
,LoginName | ||
,StartTime | ||
,CONVERT(INT,SUBSTRING(CONVERT(NVARCHAR(MAX),TEXTData),PATINDEX('%found%',TEXTData)+6,PATINDEX('%errors %',TEXTData)-PATINDEX('%found%',TEXTData)-6)) [Errors], CONVERT(INT,SUBSTRING(CONVERT(NVARCHAR(MAX),TEXTData),PATINDEX('%repaired%',TEXTData)+9,PATINDEX('%errors.%',TEXTData)-PATINDEX('%repaired%',TEXTData)-9)) [Repaired] | ||
,SUBSTRING(CONVERT(NVARCHAR(MAX),TEXTData),PATINDEX('%time:%',TEXTData)+6,PATINDEX('%hours%',TEXTData)-PATINDEX('%time:%',TEXTData)-6)+':'+SUBSTRING(CONVERT (NVARCHAR(MAX),TEXTData),PATINDEX('%hours%',TEXTData)+6,PATINDEX('%minutes%',TEXTData)-PATINDEX('%hours%',TEXTData)-6)+':'+SUBSTRING(CONVERT(NVARCHAR (MAX),TEXTData),PATINDEX('%minutes%',TEXTData)+8,PATINDEX('%seconds.%',TEXTData)-PATINDEX('%minutes%',TEXTData)-8) [Duration] | ||
FROM ::fn_trace_gettable( @tracefilename, DEFAULT) | ||
WHERE EventClass = 22 | ||
AND SUBSTRING(TEXTData,36,12) = 'DBCC CHECKDB' | ||
ORDER BY StartTime DESC; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
DECLARE @DynamicPivotQuery AS NVARCHAR(MAX) | ||
,@ColumnName AS NVARCHAR(MAX); | ||
|
||
DROP TABLE IF EXISTS #CourseSales; | ||
CREATE TABLE #CourseSales (Course VARCHAR(50), Year INT, Earning INT); | ||
INSERT #CourseSales VALUES | ||
('course_a',2020, 1), ('course_a',2021,2) | ||
, ('course_b',2019,1), ('course_b',2020,2),('course_b',2021,3) | ||
, ('course_c',2018,1), ('course_c',2019,2), ('course_c',2020,3),('course_c',2021,4) | ||
, ('other',2019,2), ('other',2020,4), ('other',2021,6); | ||
|
||
--Get distinct values of the PIVOT Column(s) | ||
SELECT @ColumnName= ISNULL(@ColumnName + ',','') + QUOTENAME(Course) | ||
FROM (SELECT DISTINCT Course FROM #CourseSales) AS Courses; | ||
|
||
--Prepare the PIVOT query using the dynamic value(s) | ||
SET @DynamicPivotQuery = | ||
N'SELECT Year, ' + @ColumnName + ' | ||
FROM #CourseSales | ||
PIVOT(SUM(Earning) | ||
FOR Course IN (' + @ColumnName + ')) AS PVTTable'; | ||
|
||
--Execute the Dynamic Pivot Query | ||
EXEC sp_executesql @DynamicPivotQuery; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
DECLARE @logfiles TABLE ( | ||
[FileArchive] TINYINT, | ||
[Date] DATETIME, | ||
[LogFileSizeB] BIGINT | ||
); | ||
|
||
INSERT @logfiles | ||
EXEC xp_enumerrorlogs; | ||
|
||
SELECT [FileArchive] | ||
,[Date] | ||
,CONVERT(VARCHAR(50),CAST(SUM(CAST([LogFileSizeB] AS FLOAT)) / 1024 / 1024 AS DECIMAL(10,4))) + ' MB' SizeMB | ||
FROM @logfiles | ||
GROUP BY [FileArchive] | ||
,[Date] | ||
,[LogFileSizeB]; | ||
|
||
-- to identify error log file location | ||
SELECT SERVERPROPERTY('ErrorLogFileName') [Error_Log_Location]; |
This file contains 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 file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
USE <target-schema>; | ||
|
||
DELIMITER // | ||
|
||
CREATE PROCEDURE <procedure-name> () | ||
BEGIN | ||
SELECT <column-list> | ||
FROM <table>; | ||
END // | ||
|
||
DELIMITER ; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
USE <target-schema>; | ||
|
||
DELIMITER // | ||
|
||
CREATE TRIGGER <trigger-name> | ||
AFTER INSERT | ||
ON <table> FOR EACH ROW | ||
BEGIN | ||
UPDATE <table> | ||
SET <column> = <some-value> | ||
WHERE <id-column> = NEW.<id-column> | ||
END; | ||
|
||
DELIMITER ; |
This file contains 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 file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
ALTER LOGIN [user] WITH | ||
PASSWORD = 'NewPassword' | ||
OLD_PASSWORD = 'OldPassword'; | ||
GO |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
-- Show all logins where the password is over 60 days old | ||
SELECT name | ||
,LOGINPROPERTY([name], 'PasswordLastSetTime') AS 'PasswordChanged' | ||
FROM sys.sql_logins | ||
WHERE LOGINPROPERTY([name], 'PasswordLastSetTime') < DATEADD(dd, -60, GETDATE()); |
This file contains 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 file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
NOTE: sys.dm_io_virtual_file_stats counters are reset whenever the SQL Server service is started | ||
*/ | ||
|
||
WITH AggregateIOStatistics AS ( | ||
SELECT DB_NAME(database_id) AS [DB Name] | ||
,CAST(SUM(num_of_bytes_read)/1048576 AS DECIMAL(12, 2)) AS reads_in_mb | ||
,CAST(SUM(num_of_bytes_written)/1048576 AS DECIMAL(12, 2)) AS writes_in_mb | ||
,CAST(SUM(num_of_bytes_read + num_of_bytes_written)/1048576 AS DECIMAL(12, 2)) AS io_in_mb | ||
FROM sys.dm_io_virtual_file_stats(NULL, NULL) AS [DM_IO_STATS] | ||
GROUP BY database_id | ||
) | ||
SELECT ROW_NUMBER() OVER(ORDER BY io_in_mb DESC) AS [I/O Rank] | ||
,[DB Name] | ||
,CAST(reads_in_mb/ SUM(reads_in_mb) OVER() * 100.0 AS DECIMAL(5,2)) AS [Read Percent] | ||
,reads_in_mb AS [Read I/O (MB)] | ||
,CAST(writes_in_mb/ SUM(writes_in_mb) OVER() * 100.0 AS DECIMAL(5,2)) AS [Write Percent] | ||
,writes_in_mb AS [Write I/O (MB)] | ||
,CAST(io_in_mb/ SUM(io_in_mb) OVER() * 100.0 AS DECIMAL(5,2)) AS [I/O Percent] | ||
,io_in_mb AS [Total I/O (MB)] | ||
FROM AggregateIOStatistics | ||
ORDER BY [I/O Rank]; | ||
|
||
|
||
SELECT DB_NAME(DB_ID()) AS [DB_Name] | ||
,DFS.name AS [Logical_Name] | ||
,DIVFS.[file_id] | ||
,DFS.physical_name AS [PH_Name] | ||
,DIVFS.num_of_reads | ||
,DIVFS.io_stall_read_ms | ||
,CAST(100. * DIVFS.io_stall_read_ms/(DIVFS.io_stall_read_ms + DIVFS.io_stall_write_ms) AS DECIMAL(10,1)) AS [IO_Stall_Reads_Pct] | ||
,CAST(DIVFS.num_of_bytes_read/1048576.0 AS DECIMAL(19, 2)) AS [MB Read] | ||
,CAST(100. * DIVFS.num_of_reads/(DIVFS.num_of_reads + DIVFS.num_of_writes) AS DECIMAL(10,1)) AS [# Reads Pct] | ||
,CAST(100. * DIVFS.num_of_bytes_read/(DIVFS.num_of_bytes_read + DIVFS.num_of_bytes_written) AS DECIMAL(10,1)) AS [Read Bytes Pct] | ||
,DIVFS.num_of_writes | ||
,DIVFS.io_stall_write_ms | ||
,CAST(100. * DIVFS.io_stall_write_ms/(DIVFS.io_stall_write_ms + DIVFS.io_stall_read_ms) AS DECIMAL(10,1)) AS [IO_Stall_Writes_Pct] | ||
,CAST(DIVFS.num_of_bytes_written/1048576.0 AS DECIMAL(19, 2)) AS [MB Written] | ||
,CAST(100. * DIVFS.num_of_writes/(DIVFS.num_of_reads + DIVFS.num_of_writes) AS DECIMAL(10,1)) AS [# Write Pct] | ||
,CAST(100. * DIVFS.num_of_bytes_written/(DIVFS.num_of_bytes_read + DIVFS.num_of_bytes_written) AS DECIMAL(10,1)) AS [Written Bytes Pct] | ||
,(DIVFS.num_of_reads + DIVFS.num_of_writes) AS [Writes + Reads] | ||
FROM sys.dm_io_virtual_file_stats(DB_ID(), NULL) AS DIVFS | ||
JOIN sys.database_files AS DFS WITH (NOLOCK) ON DIVFS.[file_id]= DFS.[file_id] | ||
ORDER BY (DIVFS.num_of_reads + DIVFS.num_of_writes) DESC; |
Oops, something went wrong.