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 / FindQueriesUsingUDFs.sql
Created April 17, 2026 15:03
SQL SERVER: Find plan cache queries using user defined functions
-- Finds cached plans whose showplan XML contains a UDF reference,
-- and tags them by DML statement type (SELECT/INSERT/UPDATE/DELETE/MERGE).
WITH XMLNAMESPACES ('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p)
SELECT
udf_ref.value('@Database', 'sysname') AS udf_database,
udf_ref.value('@Schema', 'sysname') AS udf_schema,
udf_ref.value('@Table', 'sysname') AS udf_name, -- showplan (oddly) labels UDF names with @Table
CASE
WHEN UPPER(LTRIM(stmt_text)) LIKE 'INSERT%' THEN 'INSERT'
@tcartwright
tcartwright / Install-DotNet.ps1
Created April 7, 2026 15:05
POWERSHELL: Install / Update Dotnet sdks or runtimes
<#
.SYNOPSIS
Cross-platform .NET runtime or SDK installer.
.DESCRIPTION
All-in-one script that queries Microsoft's release index, downloads
the latest installers, and installs them. Skips both download and
install when the target version is already present.
Works on Windows, Linux, and macOS under PowerShell 7 (pwsh).
@tcartwright
tcartwright / PerRowTimeDiff.sql
Last active March 30, 2026 14:48
SQL SERVER: Per Row Time diff with days and milliseconds in timespan format
-- expects rows to be in reverse order, if not in reverse order, then need to switch the columns in the datediff
CONCAT(
DATEDIFF(MILLISECOND, LEAD([all_logs].[timestamp]) OVER (ORDER BY [all_logs].[Id] DESC), [all_logs].[timestamp]) / 86400000, '.',
CONVERT(VARCHAR, DATEADD(MILLISECOND, DATEDIFF(MILLISECOND, LEAD([all_logs].[timestamp]) OVER (ORDER BY [all_logs].[Id] DESC), [all_logs].[timestamp]) % 86400000, 0), 114)
) AS [TimeDiff]
@tcartwright
tcartwright / FindImplicitConversions.sql
Last active March 26, 2026 19:59
SQL SERVER: Find high cost execution plans with implicit conversions
/*
Find cached query plans containing implicit conversions (CONVERT_IMPLICIT)
Sorted by total subtree cost (descending), then total elapsed time (descending)
Targets the plan cache via sys.dm_exec_query_stats + sys.dm_exec_query_plan
Parses the XML query plan for CONVERT_IMPLICIT warnings and expressions
*/
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
@tcartwright
tcartwright / IdentityColumnCapacityReport.sql
Last active March 18, 2026 20:57
SQL SERVER: Identity Column Capacity Report
-- ============================================================
-- Identity Column Capacity Report
-- ============================================================
-- Reports on all tables with identity columns that have 100,000
-- or more rows, showing how much ID space remains and when each
-- table is projected to exhaust its identity range.
--
-- For each qualifying table this query provides:
-- - Current identity value and data type maximum
@tcartwright
tcartwright / GenerateLoginAndDatabaseMapping.sql
Created February 13, 2026 16:01
SQL: Generate Login and database mapping for login
DECLARE @LoginName NVARCHAR(128) = 'loginname'; -- Change this
DROP TABLE IF EXISTS #ScriptOutput;
CREATE TABLE #ScriptOutput (
SortOrder INT IDENTITY(1,1),
ScriptLine NVARCHAR(MAX)
);
-- =============================================
-- Part 1: Script the LOGIN creation
@tcartwright
tcartwright / Prune-UpstreamPackages.ps1
Last active January 26, 2026 17:22
Azure: Prune upstream packages like nuget.org
#Requires -Version 5.1
<#
.SYNOPSIS
Prunes upstream-cached NuGet packages from an Azure Artifacts feed while preserving internal packages.
.DESCRIPTION
This script identifies packages in your Azure Artifacts feed that originated from upstream sources
(like nuget.org) and deletes old versions, keeping only a specified number of recent versions.
Internal packages (those published directly to your feed) are not touched.
@tcartwright
tcartwright / WindowsBuildServerReport.yml
Last active January 9, 2026 15:01
AZURE YML: Windows On Premise Build Server Agent Report
trigger: none
variables:
cleanPoolName: ${{ replace(replace(replace(parameters.agentPool, '/', '_'), '\', '_'), ':', '_') }}
cleanAgentName: ${{ replace(replace(replace(replace(parameters.agentName, '*', 'ANY'), '/', '_'), '\', '_'), ':', '_') }}
reportFile: $(Build.ArtifactStagingDirectory)\BuildServerReport.txt
name: 'BuildServerReport_${{ variables.cleanPoolName }}_${{ variables.cleanAgentName }}'
parameters:
@tcartwright
tcartwright / sp_GetDDL.sql
Last active December 29, 2025 14:34
SQL SERVER: Clone of sp_GetDDL with some fixes
USE [master]
GO
/****** Object: StoredProcedure [dbo].[sp_GetDDL] Script Date: 12/19/2025 2:03:44 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--#################################################################################################
-- Real World DBA Toolkit Version 2019-08-01 Lowell Izaguirre [email protected]
--#################################################################################################
@tcartwright
tcartwright / sp_ArchiveTableData.sql
Last active January 30, 2026 18:44
SQL SERVER: Archive table data stored procedure
/* EXEC sp_helptext 'dbo.sp_ArchiveTableData' */
USE [master]
GO
/*
Author: Tim Cartwright
Purpose:
Archives large tables using a "nibbling delete" pattern - moving data in small batches