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 / check_constraints_single_db_or_all_dbs.sql
Last active July 13, 2021 13:50
SQL SERVER: Check foreign key constraints, and generate cleanup sql to remove bad data, and sql to reenable the constraints. Does not execute the sql.
/*
TIM C:
Purpose: To check the check and foreign keys that are disabled or not trusted of all user databases or a single db for invalid data in tables by
using DBCC CHECKCONSTRAINTS, and generate the cleanup scripts, and the scripts to re-enable all constraints.
Will also try to re-enable untrusted constraints.
NOTE: Change the @current_db_only variable to run either just the current db, or all dbs
*/
DECLARE @current_db_only BIT = 1,
@db_id VARCHAR(20) = NULL
@tcartwright
tcartwright / GetBlockChain.sql
Last active October 8, 2024 15:08
SQL SERVER: Get the block chain for blocks
-- https://www.techrepublic.com/blog/the-enterprise-cloud/find-blocking-processes-using-recursion-in-sql-server-2005/
-- TIM C:
-- 1) Modified the query so as not to return multiple sets for each block
-- 2) Switched to using dm_exec_requests
IF OBJECT_ID('tempdb..#Processes') IS NOT NULL BEGIN
DROP TABLE #Processes
END
CREATE TABLE #processes (
@tcartwright
tcartwright / Get Running Transaction Information.sql
Last active June 28, 2021 23:49
SQL SERVER: Gets information about running transactions
/*
-- TIM C
Purpose: To show any runnning transactions, and how much transaction activity is being generated. Will also show
if the spid is blocking any other spids and what spids are being blocked. Can be helpful in determining the
rollback impact of killing a SPID.
NOTE: Does not show any queries that are NOT enlisted in transactions.
Origin SQL sourced from these articles:
-- https://www.red-gate.com/simple-talk/sql/database-administration/investigating-transactions-using-dynamic-management-objects/
@tcartwright
tcartwright / PostBuildEvent.bat
Created June 2, 2021 20:37
VISUAL STUDIO 2017+: Enable large address aware on post build
@rem bat file to ease use of the powershell
@%~d0
@cd "%~dp0"
@REM NOTE: Save the PostBuildEvent.ps1 to your project directory
powershell.exe -ExecutionPolicy Bypass -NoLogo -NoProfile -file "$(ProjectDir)PostBuildEvent.ps1" -TargetPath "$(TargetPath)"
@tcartwright
tcartwright / QueryDBaseFile.ps1
Last active June 1, 2021 22:30
DBASE FILE: Query from a dbase (*.dbf) file
Clear-Host
# NOTE: IF YOU GET AN EXECUTION POLICY ERROR RUN THIS:
# Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass
# NOTE: THIS MUST BE RUN FROM POWERSHELL ISE (X86)
# OTHERWISE YOU GET: "The 'Microsoft.Jet.OLEDB.4.0' provider is not registered on the local machine."
# ********************************************************************
@tcartwright
tcartwright / RefreshAllSqlModules.sql
Created April 26, 2021 19:09
SQL SERVER: Refresh all sql modules
SET NOCOUNT ON
DECLARE @sql NVARCHAR(MAX),
@nl CHAR(1) = CHAR(10),
@tab CHAR(1) = CHAR(9)
SELECT @sql = CAST((
SELECT 'RAISERROR(''REFRESHING ' + o.type_desc + ' ' + fn.name + ''', 0, 1) WITH NOWAIT;' + @nl
+ 'BEGIN TRY EXEC sys.sp_refreshsqlmodule @name = N''' + fn.name
+ '''; END TRY BEGIN CATCH PRINT CHAR(9) + ''ERROR: '' + ERROR_MESSAGE(); END CATCH;' + @nl + @nl
@tcartwright
tcartwright / TestReadOnlyRouting.md
Last active January 12, 2023 18:54
POWERSHELL: Test SQL Server ReadOnly Routing. If configured properly you will get a different server name for the second query.
@tcartwright
tcartwright / ArchiveExample.sql
Last active March 25, 2021 15:14
SQL SERVER: Two alternative methods to archive data from one table to another
/*
TIM C: This is an example script showing two ways of archiving data from one table
to another. I am not the originator of this technique. I am just documenting it here so as to be
shareable with others who ask.
This is typically more efficient and produces shorter and less locks than the standard:
INSERT INTO ARCHIVE_TABLE
SELECT ... FROM MAIN_TABLE WHERE ...
@tcartwright
tcartwright / GetServerSecurity.sql
Last active July 12, 2021 17:48
SQL SERVER: Get Server Level Security in xml format
/*
TIM C: I wrote this so I can dump server level security and easily glance over it, as well as:
- can be used to compare security from server to server
- automatically exported every so often so changes can be monitored over time
- used to clean up dead accounts
- password hash could be added to the sql users for AG primary->secondary comparison,
- TO ENABLE PWD HASH EXPORT CHANGE @output_pwds BELOW
*/
BEGIN --principals table
@tcartwright
tcartwright / MyRolesAndPermissions.sql
Last active March 3, 2021 19:37
SQL Server: Dump of server / database roles and permissions
-- my roles
SELECT [level] = 'SERVER', name, [is_member] = IS_SRVROLEMEMBER(name)
FROM sys.server_principals WHERE type = 'R'
UNION
SELECT [level] = 'DATABASE', name, [is_member] = IS_MEMBER(name)
FROM sys.database_principals WHERE type = 'R'
-- my permissions