Skip to content

Instantly share code, notes, and snippets.

View paschott's full-sized avatar

Peter Schott paschott

View GitHub Profile
@paschott
paschott / Script_Permissions.sql
Last active April 13, 2017 15:10
TSQL Script DB Permissions
SELECT
state_desc + ' ' + permission_name + ' on ['+ ss.name + '].[' + so.name + ']
to [' + sdpr.name + ']' COLLATE LATIN1_General_CI_AS as [Permissions T-SQL]
FROM SYS.DATABASE_PERMISSIONS AS sdp
JOIN sys.objects AS so
ON sdp.major_id = so.OBJECT_ID
JOIN SYS.SCHEMAS AS ss
ON so.SCHEMA_ID = ss.SCHEMA_ID
JOIN SYS.DATABASE_PRINCIPALS AS sdpr
ON sdp.grantee_principal_id = sdpr.principal_id
@paschott
paschott / ExtendedEvent_LongRunningQuery.sql
Created April 13, 2017 15:12
Create an Extended Event Trace for Long-Running Queries
CREATE EVENT SESSION [Long-Running Queries] ON SERVER
ADD EVENT sqlserver.rpc_completed (
ACTION ( sqlserver.client_app_name, sqlserver.client_hostname,
sqlserver.database_id, sqlserver.database_name, sqlserver.nt_username,
sqlserver.query_hash, sqlserver.server_principal_name,
sqlserver.session_id, sqlserver.sql_text )
WHERE ( ( ( package0.greater_than_uint64(sqlserver.database_id, ( 4 )) )
AND ( package0.equal_boolean(sqlserver.is_system, ( 0 )) ) )
AND ( duration >= ( 500000 ) )
) ),
@paschott
paschott / WorkAroundConstraintsWithMerge.sql
Created April 13, 2017 15:19
Example of working around FK Constraint restrictions with a MERGE statement
--1: Create Temp table
CREATE TABLE #MyFactTable(
ID INT NULL
, CustomerName VARCHAR(100) NULL
, SourceID INT NULL
, OutputAction VARCHAR(100) NULL
);
--2: INSERT into the temp table instead of your normal target table
-- Merge query will be the same otherwise
@paschott
paschott / SQL Server DDL Audit.sql
Created April 13, 2017 15:24
Create a DB and triggers for DDL Audits in SQL Server
-------------------------------------------------------------
-- Create Audit Database
-------------------------------------------------------------
USE master
GO
CREATE DATABASE Audit
GO
USE [Audit]
GO
@paschott
paschott / BCP Data.sql
Created April 17, 2017 22:39
Generate batch files to BCP Data in/out. Can use some work to make this more flexible. Requires target to have a copy of the DB with empty tables.
--Need details on creating a batch file and running it. Include a "PAUSE" and newline at the end.
--Note - need to create the appropriate folders. Default is "C:\bcp\databasename".
--Source
SELECT 'bcp ' + DB_NAME() + '.dbo.' + Table_name + ' OUT "C:\bcp\' + DB_NAME() + '\' + Table_name + '.bcp" -S' + @@servername + ' -N -T -E -b 100000'
FROM Information_Schema.Tables
WHERE Table_Schema = 'dbo'
ORDER BY TABLE_NAME
SELECT 'PAUSE
'
@paschott
paschott / MERGE_and_OUTPUT.sql
Created November 24, 2018 00:18
Use MERGE and OUTPUT in TSQL to capture the old and new values when importing data.
--Create test table to represent data we want to import.
CREATE TABLE #ProductsToImport (
OriginalSystemID INT,
ProductName NVARCHAR(50),
ProductNumber NVARCHAR(25),
SafetyStockLevel SMALLINT,
ReorderPoint SMALLINT,
StandardCost MONEY,
ListPrice MONEY,
DaysToManufacture INT,
@paschott
paschott / pr_SendSSISErrors.sql
Created January 2, 2019 22:53
Reporting for SSIS Errors
--Note that this uses a DB Mail Profile named "Default".
CREATE PROC dbo.pr_SendSSISErrors
@FolderName sysname = NULL,
@ProjectName sysname = NULL,
@PackageName sysname = NULL,
@ToAddress NVARCHAR(500)
AS
BEGIN --Proc
SET NOCOUNT ON
@paschott
paschott / ImportSlack.ps1
Created January 19, 2019 21:28
Imports Slack files into a "Posts" table in SQL Server.
# lets just bucket all the comments from all users and then print it out
# for each file, read the file sequentially and add it to a hash table count
$database = "SQLSlack"
$server = "localhost"
$parent_folder = "C:\SlackExport"
Import-Module SqlServer
function Invoke-Sql {
@paschott
paschott / Slack.sql
Created January 19, 2019 21:31
Creates SQL objects for Slack analysis and adjusts the usernames in the message text column
--Create table to store posts
CREATE TABLE [dbo].[Posts](
[Username] [nvarchar](255) NULL,
[Type] [nvarchar](255) NULL,
[Subtype] [nvarchar](255) NULL,
[TS] [nvarchar](255) NULL,
[Channel] [nvarchar](255) NOT NULL,
[MessageText] [nvarchar](max) NULL,
[MessageDate] [datetime2](7) NULL,
[PostID] [bigint] IDENTITY(1,1) NOT NULL,
--replace the URL in the function with the appropriate URL for your file
SELECT TOP 1000 *
FROM sys.fn_get_audit_file('https://subscription.blob.core.windows.net/paths/auditfile.xel', default, default)
WHERE (event_time <= '2019-02-20T21:56:36.631Z')
/* additional WHERE clause conditions/filters can be added here */
and action_id IN ('DBAF') --look specifically for auth failures
ORDER BY event_time DESC