Skip to content

Instantly share code, notes, and snippets.

@brovish
brovish / CaptureTSQLErrors_XE_buffer.sql
Last active June 7, 2020 14:07 — forked from EitanBlumin/CaptureTSQLErrors_XE_buffer.sql
Collect T-SQL Error Events using an Extended Events Buffer
-- Author: Eitan Blumin (t: @EitanBlumin | b: eitanblumin.com)
-- Date: 2020-05-31
-- 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
, @MinimumSeverity INT
CREATE TABLE RockClub (
ID INT IDENTITY(1, 1),
Address_NS NVARCHAR(128) COLLATE SQL_Latin1_General_CP1_CI_AI,
Address_VS VARCHAR(128) COLLATE SQL_Latin1_General_CP1_CI_AI,
Address_NW NVARCHAR(128) COLLATE Latin1_General_CI_AI,
Address_VW VARCHAR(128) COLLATE Latin1_General_CI_AI
)
INSERT INTO RockClub (Address_NS, Address_VS, Address_NW, Address_VW) VALUES (N'Saarbrücker Straße 24, 10405 Berlin','Saarbrücker Straße 24, 10405 Berlin',N'Saarbrücker Straße 24, 10405 Berlin','Saarbrücker Straße 24, 10405 Berlin')
@brovish
brovish / create_database_snapshot.sql
Created September 5, 2020 17:45 — forked from EitanBlumin/create_database_snapshot.sql
Easily create a database snapshot for a given database using T-SQL
DECLARE @CurrDB SYSNAME = 'MyDBName'
DECLARE @CMD NVARCHAR(MAX);
SELECT @CMD = ISNULL(@CMD + N',
', N'') + N'(NAME = ' + QUOTENAME(name) + N'
, FILENAME = ' + QUOTENAME(LEFT(physical_name, LEN(physical_name) - CHARINDEX('\', REVERSE(physical_name)) + 1)
+ DB_NAME(database_id) + '_' + REPLACE(NEWID(),'-','') + '.ss', N'''')
+ N')'
@brovish
brovish / GeneratePartitionedView.sql
Created September 5, 2020 18:03 — forked from EitanBlumin/GeneratePartitionedView.sql
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
@brovish
brovish / ntfs-filenames.txt
Created October 21, 2020 22:14 — forked from doctaphred/ntfs-filenames.txt
Invalid characters for Windows filenames
Information from https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file :
Use any character in the current code page for a name, including Unicode
characters and characters in the extended character set (128–255), except
for the following:
- The following reserved characters:
< (less than)
> (greater than)
@brovish
brovish / RSSFeeds.opml
Created November 28, 2020 16:03 — forked from bertwagner/RSSFeeds.opml
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"/>
@brovish
brovish / CaptureTSQLEvents_XE_Buffer.sql
Created January 22, 2021 12:36 — forked from EitanBlumin/CaptureTSQLEvents_XE_Buffer.sql
Collect T-SQL Events using an Extended Events Buffer
-- Author: Eitan Blumin | https://www.eitanblumin.com
-- Date: 2020-02-26
-- Last Update: 2020-04-07
-- Description: Collect T-SQL Events using an Extended Events Buffer
SET NOCOUNT ON;
DECLARE
@SourceLinkedServer SYSNAME
, @MinimumDurationMilliSeconds BIGINT
@brovish
brovish / fix_all_orphan_users.sql
Created February 18, 2021 19:51 — forked from EitanBlumin/fix_all_orphan_users.sql
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
Date Created: 2018-01-02
Last Update: 2020-03-30
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. No login with same name exists - generate DROP USER to delete the orphan user.
3. Orphan user is [dbo] - change the database owner to SA (or whatever SA was renamed to)
@brovish
brovish / install-sqlservermodule.md
Created May 2, 2021 05:52 — forked from ronascentes/Install-SqlServerPSModule.ps1
Install SQL Server Powershell module without access to the PowerShell Gallery modules
  1. Run from PS console: Invoke-WebRequest -Uri powershellgallery.com/api/v2/package/sqlserver -Out D:\temp\sqlserver.zip

  2. Extract and save to Program Files\WindowsPowerShell\Modules\sqlserver

  3. Check if Powershell is able to see it: Get-Module -ListAvailable

  4. Import SqlServer module Import-Module SqlServer