Skip to content

Instantly share code, notes, and snippets.

View tcartwright's full-sized avatar

Tim Cartwright tcartwright

  • Houston, Texas
View GitHub Profile
@tcartwright
tcartwright / Scorch TFS.bat
Last active August 18, 2021 16:21
POWERSHELL: Script that will scorch your TFS folders like TFPT /Scorch did. SEE: https://docs.microsoft.com/en-us/azure/devops/repos/tfvc/reconcile-command?view=azure-devops
@rem bat file to ease use of the script
@%~d0
@cd "%~dp0"
powershell.exe -ExecutionPolicy Bypass -NoLogo -NoProfile -file "%~dpn0.ps1"
@echo Done.
@pause
@tcartwright
tcartwright / UninstallOldModuleVersions.md
Last active February 15, 2023 18:19
POWERSHELL: Uninstall ALL older versions of modules
@tcartwright
tcartwright / UpdateAllModules.ps1.md
Last active February 14, 2023 16:44
POWERSHELL: Will scan all your installed modules and update them if there is a newer version out in the repo, and then uninstall the older version if it can
@tcartwright
tcartwright / ExtractCLRDllFromDatabase.md
Last active January 3, 2023 16:20
POWERSHELL: Will extract all user CLR assemblies from a SQL SERVER database as DLL files, and or PDB files
@tcartwright
tcartwright / dbo.sp_generate_merge_crud.sql
Last active October 9, 2025 22:15
SQL SERVER: Generates (DUI or UPSERT) delete, update, insert statements instead of a MERGE statement to merge data into a table.
USE master
IF OBJECT_ID (N'dbo.sp_generate_merge_crud') IS NULL BEGIN
EXEC('CREATE PROCEDURE dbo.sp_generate_merge_crud AS SELECT ''PROCEDURE STUB, TO BE REPLACED'';');
END
GO
/*
TIM C: Because of issues with the MERGE statement (https://www.mssqltips.com/sqlservertip/3074/use-caution-with-sql-servers-merge-statement/)
@tcartwright
tcartwright / dbo.GetBackupRestoreHeaders.sql
Last active February 22, 2023 14:58
SQL SERVER: A stored procedure that export the latest record from RESTORE HEADERONLY
/*
Original script from here: https://karaszi.com/restore-all-databases-from-a-number-of-backup-files
Tim C: made a modification so the script adjusts the columns per the SQL Server version
*/
IF OBJECT_ID (N'dbo.GetBackupRestoreHeaders') IS NULL BEGIN
EXEC('CREATE PROCEDURE dbo.GetBackupRestoreHeaders AS SET NOCOUNT ON;');
END
GO
ALTER PROCEDURE dbo.GetBackupRestoreHeaders (
@backup_file_path VARCHAR(8000),
@tcartwright
tcartwright / restore_headeronly_into_temp_table.sql
Created July 10, 2021 00:58
SQL SERVER: Uses RESTORE HEADERONLY to generate a temp table. The output columns are adjusted for the sql server version queried.
/*
Original script from here: https://karaszi.com/restore-all-databases-from-a-number-of-backup-files
Tim C: made a modification so the script adjusts the columns per the SQL Server version
*/
DECLARE @backup_file_path VARCHAR(500) = 'path to backup file.bak'
IF OBJECT_ID('tempdb..#restore_headeronly') IS NOT NULL BEGIN
DROP TABLE #restore_headeronly
END
@tcartwright
tcartwright / restore_filelistonly_into_temp_table.sql
Created July 10, 2021 00:57
SQL SERVER: Uses RESTORE FILELISTONLY to generate a temp table. The output columns are adjusted for the sql server version queried.
/*
Original script from here: https://karaszi.com/restore-all-databases-from-a-number-of-backup-files
Tim C: made a modification so the script adjusts the columns per the SQL Server version
*/
DECLARE @backup_file_path VARCHAR(500) = 'path to backup file.bak'
IF OBJECT_ID('tempdb..#restore_filelistonly') IS NOT NULL BEGIN
DROP TABLE #restore_filelistonly
END
@tcartwright
tcartwright / nibbling_deletes_with_transaction.sql
Last active July 6, 2021 22:11
SQL SERVER: Demonstrates how to do nibbling inserts with a transaction using SAVE TRANSACTION. Can easily be modified for nibbling deletes. Useful for avoiding lock escalation as well.
BEGIN -- SETUP FAKE SOURCE AND DESTINATION
IF OBJECT_ID('tempdb..#destination') IS NOT NULL BEGIN
DROP TABLE #destination
END
CREATE TABLE #destination (
id INT NOT NULL IDENTITY,
name VARCHAR(100),
number INT
)
@tcartwright
tcartwright / get_session_locks.sql
Last active September 14, 2021 20:04
SQL SERVER: Get session lock information
-- only create the temp table #partitions ONCE. DROP if needed to recreate
-- DROP TABLE #partitions
IF OBJECT_ID('tempdb..#partitions') IS NULL BEGIN
-- table to hold all of the partition information
CREATE TABLE #partitions (
database_id int NOT NULL,
database_name sysname NOT NULL,
partition_id bigint NOT NULL,
object_id INT NOT NULL,