Skip to content

Instantly share code, notes, and snippets.

View paschott's full-sized avatar

Peter Schott paschott

View GitHub Profile
Import-Module dbatools
$scripts = get-childitem "C:\ScriptsToRun\" -Filter *.sql | sort-object Name
$servers = "servername"
$database = "databasename"
foreach ($script in $scripts) {
$OutputFile = $script.directoryname + "\" + $script.basename + ".txt"
Write-Host $OutputFile
Invoke-DbaQuery -SqlInstance $servers -File $script.FullName -Database $database -MessagesToOutput | Out-File -FilePath $OutputFile
@paschott
paschott / ChangeSSISProtectionLevel.ps1
Created March 9, 2020 19:17
Changes SSIS Protection Level for all packages and project
#PowerShell script
################################
########## PARAMETERS ##########
################################
$projectFolder = "C:\SSIS\myProject\myProject"
$dtutilPath = "C:\Program Files\Microsoft SQL Server\130\DTS\Binn\dtutil.exe"
# The number changes per SQL Server version
# 130=2016, 120=2014, 110=2012
# Also check the drive where SQL Server is
# installed
@paschott
paschott / File_Autogrowth.sql
Last active November 23, 2019 14:21
Generates TSQL to update File Auto-Growth for SQL Server for files using the old defaults
DECLARE @NewDataFileGrowth varchar(100) = '500MB'
DECLARE @NewLogGrowth varchar(100) = '100MB'
select
CASE WHEN physical_name like '%.ldf'
THEN 'ALTER DATABASE [' + DB_NAME(mf.database_id) + '] MODIFY FILE (NAME = [' + mf.name + '], FILEGROWTH = ' + @NewLogGrowth + ')'
ELSE 'ALTER DATABASE [' + DB_NAME(mf.database_id) + '] MODIFY FILE (NAME = [' + mf.name + '], FILEGROWTH = ' + @NewDataFileGrowth + ')'
END as ChangeAutoGrowSettings,
DB_NAME(mf.database_id) database_name,
mf.name logical_name,
--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
@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,
@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 / 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 / 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 / Disabling_Triggers.sql
Last active July 12, 2023 15:52
Disabling Triggers
CREATE TABLE TriggerStatus (
TriggerStatusID INT IDENTITY(1,1) NOT NULL,
TriggerName varchar(100) NOT NULL,
IsEnabled BIT NOT NULL CONSTRAINT DF_TriggerStatus_IsEnabled DEFAULT 1,
CONSTRAINT PK_TriggerStatus PRIMARY KEY CLUSTERED (TriggerStatusID)
)
GO
CREATE TABLE MyAuditTable (
MyAuditTableID INT NOT NULL IDENTITY(1,1),
@paschott
paschott / SSISDB Cleanup.sql
Last active June 20, 2019 19:26
Cleanup script for the SSISDB Catalog to make up for MS' poor performing query and high default days for history retention.
--With thanks to Bill Fellows for the script from here:
-- http://stackoverflow.com/questions/21781351/how-can-i-clean-up-the-ssisdb
USE SSISDB;
SET nocount ON;
IF Object_id('tempdb..#DELETE_CANDIDATES') IS NOT NULL
BEGIN
DROP TABLE #delete_candidates;
END;