Skip to content

Instantly share code, notes, and snippets.

View TheRockStarDBA's full-sized avatar
🏠
Working from home

Kin Shah TheRockStarDBA

🏠
Working from home
View GitHub Profile
DECLARE @FileName NVARCHAR(4000)
SELECT @FileName =
LEFT(target_data.value('(EventFileTarget/File/@name)[1]','nvarchar(4000)') ,
CHARINDEX('system_health',target_data.value('(EventFileTarget/File/@name)[1]','nvarchar(4000)') )-1) + 'system_health*.xel'
FROM (
SELECT
CAST(target_data AS XML) target_data
@TheRockStarDBA
TheRockStarDBA / Cummulative IO stats for SQL Server
Created September 26, 2014 21:23
Cummulative IO stats for SQL Server
SELECT
QUOTENAME(DB_NAME(iovfs.[database_id])) [DatabaseName]
,iovfs.[file_id] [FileID]
,mf.[name] [LogicalName]
,mf.[type_desc] [FileType]
,SUM(iovfs.[num_of_bytes_read]) [Read_bytes]
,SUM(iovfs.[num_of_bytes_written]) [Written_bytes]
,SUM(iovfs.[num_of_bytes_read])/1048576 [Read_MiB]
,SUM(iovfs.[num_of_bytes_written])/1048576 [Written_MiB]
,SUM(iovfs.[num_of_reads]) [Read_Count]
@TheRockStarDBA
TheRockStarDBA / IndexBreakDownWithBuffered
Created September 26, 2014 21:31
Detailed Table and Index Breakdown With Buffered
/*
--http://jongurgul.com/blog/detailed-table-index-breakdown-buffered/
This script builds on the Detailed Table and Index script to further show the buffered metrics. This script will give you a comprehensive breakdown of your tables and indexes, which is shown with schema(s),table(s),index type(s),index size(s),row count(s) as well as the filegroup(s) and partition(s) information.
Also in this script I have included two columns called TippingPoint* which relate to the rough bounderies where a nonclustered index is no longer selective enough. I have applied the formula to the clustered index for ease. For more detail on an indexes tipping point please see Kimberly Tripp's article.
http://www.sqlskills.com/BLOGS/KIMBERLY/category/The-Tipping-Point.aspx
*/
SELECT
DB_NAME() [DatabaseName]
CREATE PROCEDURE sp_server_diag_event_parser
as
--SP_SERVER_DIAGNOSTICS Dynamic Parser for "events", v1.23
--You may use this at will, you may share it provided this header remains.
-- Copyright 2012 Michael Bourgon
-- Commercial use or sale prohibited without permission. Personal, Internal Company, or Private use is fine.
-- If you're just running this as your job as a DBA, enjoy.
-- Please feel free to share, and feel free to send corrections or enhancements - thebakingdba.blogspot.com
-- Thanks to Marc_S on Stackoverflow for the help on parsing XML.
-- Thanks to Stack Overflow for forcing me to come up with a good question - so I found the flawed derived table slowdown.
@TheRockStarDBA
TheRockStarDBA / ChangeDatabaseCollation
Created April 10, 2015 19:24
Change Database Collation
/************************************************************************************************************************************************************************************************************************************
************************************************************************************************************************************************************************************************************************************
Author : Kin Shah
Bug Identified The incuded columns were not generated as a part of original script.
and Fixed : Modified the script to include the included columns.
Disclaimer
The views expressed on my posts on this site are mine alone and do not reflect the views of my company. All posts of mine are provided "AS IS" with no warranties, and confers no rights.
@TheRockStarDBA
TheRockStarDBA / WeakPasswordChecker.sql
Created April 15, 2015 14:08
Check Weak Passwords on a SQL Server instance using PWDCOMPARE()
/************************************************************************************************************************************************************************************************************************************
************************************************************************************************************************************************************************************************************************************
Author : Kin Shah
Purpose : Checks weak passwords on a sql server instance using PWDCOMPARE()
The list of weak passwords can be updated as per your needs.
Below is the source of WEAK passwords :
-- Ref: http://security.blogoverflow.com/category/password/
@TheRockStarDBA
TheRockStarDBA / SearchAllTablesForString.sql
Created April 28, 2015 16:43
Search All tables for particular String or word
/*
- Search through tables to find specific text
- Written by Luis Chiriff (with help from SQL Server Central)
- luis.chiriff@gmail.com @ 24/11/2008 @ 11:54
*/
-- Variable Declaration
Declare @StringToFind VARCHAR(200), @Schema sysname, @Table sysname, @FullTable int, @NewMinID int, @NewMaxID int,
@SQLCommand VARCHAR(8000), @BaseSQLCommand varchar(8000), @Where VARCHAR(8000), @CountCheck varchar(8000) , @FieldTypes varchar(8000),
@TheRockStarDBA
TheRockStarDBA / read_write_latency-sys.dm_io_virtual_file_stats.sql
Created June 16, 2015 18:18
Find out read write latency - sys.dm_io_virtual_file_stats
/*
Created By: David Wiseman
Website: http://www.wisesoft.co.uk
Description:
Script to generate reports based on sys.dm_io_virtual_file_stats.
See: http://msdn.microsoft.com/en-us/library/ms190326.aspx
*/
-- I/O Stats by Database
SELECT d.name AS DatabaseName,
ROUND(CAST(SUM(num_of_bytes_read+num_of_bytes_written) AS FLOAT) / SUM(SUM(num_of_bytes_read+num_of_bytes_written)) OVER() *100,2) AS [% Total I/O],
Write-Warning "This code is not meant to be run sequentially, run it in sections following comment instructions"
exit
<##############################################################################
create transfer file
##############################################################################>
$FilePath = "C:\temp\transfer_file.txt"
$DesiredFileSizeKB = 1024 * 7 # 7 MB
@TheRockStarDBA
TheRockStarDBA / GetDBNameAndPhyscDeviceName.sql
Created July 9, 2015 14:37
Map a databaseName with a physical device name SybaseASE
-- Map a databaseName with a physical device name SybaseASE
select sda.name
,sde.name
,sde.phyname
from master..sysdatabases sda
,master..sysdevices sde
,master..sysusages su
where su.dbid = sda.dbid
and su.vdevno = sde.vdevno
and sda.name like '%temp%' --- CHANGE HERE with your database Name!