Skip to content

Instantly share code, notes, and snippets.

@mbourgon
mbourgon / AWS_CW_RDS_Log_with_Filter.ps1
Last active March 13, 2018 19:44
Using Cloudwatch to scan AWS RDS Aurora log files for DML changes
import-module awspowershell
#You should have already saved your credentials locally using:
# set-awscredentials -accesskey myaccesskey -secretkey mysecretkey -StoreAs MyDeveloper
$credential = get-awscredentials -ProfileName MyDeveloper
# Get the current timestamp
$Current_Unix_Timestamp = [Math]::Floor([decimal](Get-Date(Get-Date).ToUniversalTime()-uformat "%s")) * 1000
@mbourgon
mbourgon / webPOST.cs
Last active August 5, 2022 17:23
SQL Server CLR to make Web API calls using HttpWebRequest
//See full article at http://thebakingdba.blogspot.com/2017/07/clr-soup-to-nuts-making-generic-clr-to.html
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Collections;
using System.Net;
@mbourgon
mbourgon / last_4_stats_updates_for_a_database
Last active April 27, 2019 09:37
List the last 4 times that each statistic in a database was updated
--Written by mbourgon 2016 as per http://thebakingdba.blogspot.com/2016/08/statistics-get-last-4-stat-update-time.html
--CC, noncommerical with attribution. Use it as you want noncommercially, but this notice must remain.
--run in the database you need to evaluate.
DECLARE @sql NVARCHAR (1000), @MIN INT, @MAX INT, @stats_name NVARCHAR(128), @tablename sysname, @the_schema_name sysname, @db_name sysname
if object_id('tempdb..#stats_info') is not null
DROP TABLE #stats_info
CREATE TABLE #stats_info (updated DATETIME, [Table cardinality] BIGINT, [snapshot ctr] BIGINT, steps INT, density DECIMAL(19,16), [rows above] INT, [rows below] INT, [squared variance error] DECIMAL (19,12), [inserts since last update] MONEY, [deletes since last update] MONEY, [leading column type] VARCHAR(50))
if object_id('tempdb..#stats_info2') is not null
@mbourgon
mbourgon / EN_report_on_added_dropped_ROUTE.sql
Created July 1, 2016 20:02
Event Notifications - send HTML email when a ROUTE is changed/added/dropped
--check last 15 minutes to make sure nobody did a stupid.
DECLARE @tableHTML NVARCHAR(MAX);
WITH cte AS
(
SELECT PostTime, EventType, LoginName, ObjectName, ServerName FROM ENAudit_Events WHERE EventType LIKE '%route%'
AND PostTime > dateadd(minute, datediff(minute,0,GETDATE()) / 15 * 15, 0)
)
select @tableHTML = N'<H3>Event Notification ROUTE changes - make sure you meant to add/remove these from EN!' + '</H3>'
+ N'<table border="1">'
@mbourgon
mbourgon / kill_and_fix_our_custom_app.ps1
Created June 29, 2016 15:47
Kill app on remote computer using powershell, then replace files
#get the list of servers to tackle
foreach ($Server in Get-Content "C:\UpdateUsers.txt")
#now do each server
{
#making a \\server and server variable
$server2 = $Server -replace ("\\","")
#Which server is it updating
Write-host "Working on $Server2"
@mbourgon
mbourgon / ssis_job_failure__find_failures_to_report.sql
Created April 13, 2016 21:19
SSISDB job failure - scans sysjobhistory, and passes to code that sends a useful email
CREATE PROCEDURE ssis_job_failure__find_failures_to_report
AS
/*
2016/04/13 MDB - 1.00 - Since SQLSentry doesn't always work with the stored procedure (performance issues because we have so many jobs running frequently)
, we're trying this. Scan sysjobhistory for errors. Run each job failure through the SSISDB SP I wrote.
Set the job to run every minute.
this is a companion to https://gist.github.com/mbourgon/5502095, which will send useful SSISDB emails when a job fails.
CREATE TABLE ssisdb_failure_email_last_instance_id_checked (id INT IDENTITY PRIMARY KEY, instance_id int, insert_datetime DATETIME)
--109911
@mbourgon
mbourgon / EN_basic_traffic_monitor.sql
Created January 29, 2016 14:20
Event Notifications - make sure something's inserted into the table in the last hour. If not, EMAIL.
DECLARE @last_insert DATETIME
SET @last_insert = (SELECT TOP 1 insert_datetime FROM EventNotificationRec.dbo.ENAudit_Events ORDER BY id DESC)
SELECT @last_insert
SELECT DATEDIFF(MINUTE,@last_insert, GETDATE())
IF (SELECT DATEDIFF(MINUTE,@last_insert, GETDATE())) >60
begin
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'me',
@mbourgon
mbourgon / dynamic_pivot.sql
Created December 10, 2015 21:02
Dynamic Pivot using T-SQL
DECLARE @SQL as VARCHAR (MAX)
DECLARE @Columns AS VARCHAR (MAX)
SELECT @Columns=
COALESCE(@Columns + ',','') + QUOTENAME(across)
FROM
(
SELECT DISTINCT across
From #TEMP
) AS B
@mbourgon
mbourgon / find_recent_stats_for_a_database.sql
Created September 16, 2015 21:43
Statistics - find last 4 stats for a database
--gets the last 4 stats updates for each non-system/non-MS table/statistic
--thebakingdba.blogspot.com
use yourdatabasenamehere
DECLARE @sql NVARCHAR(1000),
@MIN INT,
@MAX INT,
@statname NVARCHAR(128),
@tablename NVARCHAR(128),
@schemaname NVARCHAR(128);
DECLARE @listofstats TABLE
@mbourgon
mbourgon / Eventlog_Tracking_Notifying_on_Busy_Servers.sql
Created July 29, 2015 21:52
Eventlog_Capture - reporting on 100 in a minute
SET QUOTED_IDENTIFIER ON
SET ANSI_NULLS ON
GO
CREATE PROCEDURE [dbo].[Eventlog_Tracking_Notifying_on_Busy_Servers] @threshold INT = 100
AS
--mdb 2015/07/17 1.00 - Basically, making sure that we don't get blindsided by something going wrong.
-- Piggybacks on top of the eventlog_tracking job, so if that fails or stops working, so will this.
-- Looks for any server/log combinations that have more than 100 entries in a minute.
-- Once a couple issues are fixed, we could drop this number. Obviously, filtering will have to occur, too.