Skip to content

Instantly share code, notes, and snippets.

@mbourgon
mbourgon / sp_server_diag_event_parser.sql
Created February 24, 2014 07:03
sp_server_diag_event_parser - Parses SQL Server 2012's sp_server_diagnostics, dynamically, and returns all result sets.
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.
@mbourgon
mbourgon / EN_certificate_setup.sql
Last active August 29, 2015 14:00
Event Notifications - using certificates
--For the repository server I only have one cert and one key.
--For the new servers I have to create a new certificate and key for each one.
-------------------------------------------------------------
--Part 1, Run on new server adding to Event Notifications--
-------------------------------------------------------------
USE master
GO
--Creating master key and an user which will be used by the certificate
CREATE MASTER KEY ENCRYPTION BY PASSWORD = N'';
@mbourgon
mbourgon / rss_feed_SqlServer_Releases.ps1
Created June 26, 2014 21:34
RSS checker for SQL Release Services Blog (aka SQL Server patch release notifications) (powershell portion, 1/2)
<#
.SYNOPSIS
Script downloads an rss feed of data about SQL Server service packs and cummulative updates.
.DESCRIPTION
Script downloads data into a staging table and is called by a job.
.PARAMETER <paramName>
There are no parameters.
.EXAMPLE
Call from a job: powershell "& 'C:\Powershell_Scripts\rss_feed_SqlServer_Releases.ps1'"
#>
@mbourgon
mbourgon / EN_add_new_server.sql
Last active August 29, 2015 14:06
Event Notifications - add new server
--reminder, this runs on the central monitorING box, and uses xp_cmdshell and the like to connect to the "new" box.
/*
Event Notification - add server to monitoring. Probably simpler to do by hand, but I like this.
See http://thebakingdba.blogspot.com/ for more information, look for Event Notifications
Given a parameter of servername, WHEN RUN ON THE EN "repository", it will
a) try to determine the servername for the "sender"
@mbourgon
mbourgon / system_health_parser_one_table
Created April 13, 2015 16:11
System_Health dynamic parser - one table
if object_id('tempdb..#systemhealthsessiondata') is not null
DROP TABLE #systemhealthsessiondata
SELECT CAST(xet.target_data AS XML) AS XMLDATA
INTO #SystemHealthSessionData
FROM sys.dm_xe_session_targets xet
JOIN sys.dm_xe_sessions xe
ON (xe.address = xet.event_session_address)
WHERE xe.name = 'system_health'
@mbourgon
mbourgon / system_health_parser_multi_table
Created April 13, 2015 16:12
System_Health dynamic parser - one table per event
if object_id('tempdb..#systemhealthsessiondata') is not null
DROP TABLE #systemhealthsessiondata
SELECT CAST(xet.target_data AS XML) AS XMLDATA
INTO #SystemHealthSessionData
FROM sys.dm_xe_session_targets xet
JOIN sys.dm_xe_sessions xe
ON (xe.address = xet.event_session_address)
WHERE xe.name = 'system_health'
@mbourgon
mbourgon / Zenos_Arrow_Date_Search
Created April 16, 2015 20:05
Searching for a Date via PK ala Zeno's Arrow
@mbourgon
mbourgon / get_process_uptime
Created April 28, 2015 17:46
Powershell - Process Uptime for multiple machines, saved to a table
. C:\powershell_scripts\out-datatable.ps1
. C:\powershell_scripts\write-datatable.ps1
$server_repository = 'myrepo'
$database_repository = 'repodb'
#here we create $starttime, then it will get the values when passed through the for-each. Could just do inline, but it's a clever concept so leaving it here.
$StartTime= @{n='StartTime';e={$_.ConvertToDateTime($_.CreationDate)}}
#get list of servers we want to look at
@mbourgon
mbourgon / dynamic_csv_etl_basic
Created May 5, 2015 18:59
Basic dynamic CSV ripper - complete implementation.
USE tempdb
/*
2015/04/22 - mdb - 2.00 - pull raw CSV, shred via function, pivot into a virtual table, then insert into
actual table based on existing fields. Practically: forwards/backwards compatability.
Add new fields to your target table, and as they show up in the new file they'll be added.
Practically, they'll have to rename the file when they do this, so that we know the
names of the new fields. But that's just a parameter change in the job.
2015/05/05 - mdb - 2.1 - very basic implementation, complete code. Just fill out the parameters!
Downside: SLOW?! A 5mb file, which has 106k rows, takes 7 seconds.
@mbourgon
mbourgon / EventLog_Capture.ps1
Last active August 29, 2015 14:20
EventLog_Capture - save your event logs, from multiple servers, to a database, excluding the noise
#Note this is JUST the capture portion; there's a database component as well
# the 4 scripts below are obviously prereqs, from Hey Scripting Guy and PoshCode
clear
. C:\powershell_scripts\invoke-sqlcmd2.ps1;
. C:\powershell_scripts\split-job.ps1;
. C:\powershell_scripts\write-datatable.ps1;
. C:\powershell_scripts\out-datatable.ps1;
$server_repository = 'yourreposerverhere'
$database_repository = 'yourrepodbhere'