Skip to content

Instantly share code, notes, and snippets.

@EitanBlumin
EitanBlumin / create_database_snapshot.sql
Last active November 16, 2021 08:58
Easily create a database snapshot for a given database using T-SQL
DECLARE
@CurrDB SYSNAME = DB_NAME()
,@WhatIf BIT = 1
DECLARE @CMD NVARCHAR(MAX), @SnapshotName SYSNAME;
SET @SnapshotName = @CurrDB + '_snapshot_' + CONVERT(nvarchar, GETDATE(), 112) + REPLACE(CONVERT(nvarchar, GETDATE(), 114),':','');
SELECT @CMD = ISNULL(@CMD + N',
', N'') + N'(NAME = ' + QUOTENAME(name) + N'
@EitanBlumin
EitanBlumin / CaptureTSQLErrors_XE_buffer.sql
Last active September 3, 2020 00:45
Collect T-SQL Error Events using an Extended Events Buffer
-- Author: Eitan Blumin (t: @EitanBlumin | b: eitanblumin.com)
-- Date: 2020-05-31
-- Last Update: 2020-07-15
-- Description: Collect T-SQL Error Events using an Extended Events Buffer
-- The script automatically detects whether you're in an Azure SQL DB, or a regular SQL Server instance.
SET NOCOUNT ON;
DECLARE
@SourceLinkedServer SYSNAME
@EitanBlumin
EitanBlumin / GeneratePartitionedView.sql
Last active November 16, 2021 08:59
Stored procedure that creates a partitioned view on top of identically-named tables that exist in multiple databases
/*
Author: Eitan Blumin | @EitanBlumin, https://www.eitanblumin.com
Create Date: 2016-06-03
Last Update: 2020-05-19
Description:
This procedure creates a partitioned view on top of identically-named tables that exist in multiple databases.
Parameters:
@DBNamePattern - Database name pattern to use for filtering the relevant databases
@EitanBlumin
EitanBlumin / CaptureTSQLEvents_XE_Buffer.sql
Last active December 5, 2021 12:14
Collect T-SQL Events using an Extended Events Buffer
-- Author: Eitan Blumin (t: @EitanBlumin | b: eitanblumin.com)
-- Date: 2020-05-31
-- Last Update: 2021-04-22
-- Description: Collect T-SQL Events using an Extended Events Buffer
SET NOCOUNT ON;
DECLARE
@SourceLinkedServer SYSNAME
, @MinimumDurationMilliSeconds BIGINT
@EitanBlumin
EitanBlumin / SQL_Server_Error_Log_Based_on_Severity_with_Full_Message.sql
Last active September 17, 2020 01:52
T-SQL script to output a single row per each high severity error from the SQL Server Error Log
-- Author: Eitan Blumin (t: @EitanBlumin | b: eitanblumin.com)
-- Description: Get a single row per each high severity error from the SQL Server Error Log
DECLARE
@SampleTime DATETIME = DATEADD(MINUTE,-30,SYSDATETIME())
, @MinimumSeverity INT = 17
, @MaximumSeverity INT = 25;
IF OBJECT_ID(N'tempdb..#errors') IS NOT NULL
DROP TABLE #errors;
CREATE EVENT SESSION [TrackFailedLogins] ON SERVER
ADD EVENT sqlserver.error_reported(
    ACTION(sqlserver.client_app_name,sqlserver.client_hostname,sqlserver.nt_username,sqlserver.database_id,sqlserver.session_id)
WHERE (([severity]=(20) OR [severity]=(14) OR [severity]=(16))
AND ([error_number]=(18056)
OR [error_number]=(17892)
OR [error_number]=(18061)
OR [error_number]=(18452)
OR [error_number]=(11248)
OR [error_number]=(17806)
@bertwagner
bertwagner / RSSFeeds.opml
Created July 15, 2019 19:33
The OPML file of all of the SQL Server (and some other) RSS feeds I subscribe to as of 2019-07-01.
<?xml version="1.0" encoding="UTF-8"?>
<opml version="1.0">
<head>
<title>Bert's mostly SQL subscriptions in feedly Cloud</title>
</head>
<body>
<outline text="Marketing" title="Marketing">
<outline type="rss" text="Signal v. Noise" title="Signal v. Noise" xmlUrl="https://signalvnoise.com/posts.rss" htmlUrl="https://m.signalvnoise.com"/>
<outline type="rss" text="Austin Kleon" title="Austin Kleon" xmlUrl="http://feeds2.feedburner.com/AustinKleon" htmlUrl="https://austinkleon.com"/>
@EitanBlumin
EitanBlumin / fix_all_orphan_users.sql
Last active October 31, 2023 07:47
Fix all orphaned users in current database, or all databases in the instance (more info: https://eitanblumin.com/2018/10/31/t-sql-script-to-fix-orphaned-db-users-easily/)
/*
Author: Eitan Blumin | https://eitanblumin.com | https://madeiradata.com
Date Created: 2018-01-02
Last Update: 2023-06-18
Description:
Fix All Orphaned Users Within Current Database, or all databases in the instance.
Handles 3 possible use-cases:
1. Login with same name as user exists - generate ALTER LOGIN to map the user to the login.
2. Login with a different name but the same sid exists - generate ALTER LOGIN to map the user to the login.
3. Login SID is identifiable but login doesn't exist in SQL - generate CREATE LOGIN FROM WINDOWS to create a Windows authentication login.
@its-a-feature
its-a-feature / get_gists.py
Created November 2, 2017 21:37 — forked from leoloobeek/get_gists.py
Download all gists for a specific user
# first: mkdir user && cd user && cp /path/to/get_gists.py .
# python3 get_gists.py user
import requests
import sys
from subprocess import call
user = sys.argv[1]
r = requests.get('https://api.github.com/users/{0}/gists'.format(user))