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 / JDBCConnectionString.ps1
Last active February 3, 2022 15:53
POWERSHELL: JDBC Connection String Builder Class
class JDBCConnectionString {
[string]$subprotocol
[string]$subname
[string]$host
[int]$port
JDBCConnectionString([string]$connectionString) {
$parts = $connectionString.Split(";")
$protocols = $parts[0].Split(":")
$this.subprotocol = $protocols[1]
@tcartwright
tcartwright / Install-FRK.ps1
Last active February 3, 2022 19:09
POWERSHELL: Installs the latest version of Brent Ozars FRK to a server or list of servers
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[string[]]$servers,
[Parameter()]
[string]$dbName = "master",
[Parameter()]
[string]$InstallScriptFileName = "Install-All-Scripts.sql"
)
@tcartwright
tcartwright / SetupAliases.ps1
Last active July 23, 2024 14:39
SQL SERVER: Setup aliases
#Requires -RunAsAdministrator
Clear-Host
#controls whether or not the alias keys are deleted before adding first
$cleanKeys = $true
#backup in case the requires does not work
$principal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
if (!($principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))) {
throw "Administrator rights required"
@tcartwright
tcartwright / StackTrace.sql
Last active March 2, 2023 14:46
SQL SERVER: Stored proc stack trace. Tested for 2012 and higher
/*
Authors: Matthew Naul, Tim Cartwright
Purpose: to build a stack trace so that nested stored proces can be followed in the case of an exception.
Versions: Tested against 2008+
NOTES: The context functions in use for the 2016 version of the stored procedure are not available in lower versions,
So a different version of the proc is applied that does string manipulation to perform nearly identical stack traces.
*/
USE [master]
GO
@tcartwright
tcartwright / SetupSessionContextObjects.sql
Created January 26, 2022 16:16
SQL SERVER: Session context set / get
USE [master]
IF OBJECT_ID('dbo.sp_session_set') IS NULL BEGIN
EXEC ('CREATE PROCEDURE dbo.sp_session_set AS BEGIN SELECT 1; END;');
END
IF OBJECT_ID('dbo.fn_session_get') IS NULL BEGIN
EXEC ('CREATE FUNCTION dbo.fn_session_get (@key sysname) RETURNS SQL_VARIANT AS BEGIN RETURN 1; END;');
END
GO
@tcartwright
tcartwright / RSAT-Management.ps1
Created January 20, 2022 15:49
POWERSHELL: List and or INSTALL RSAT for windows (non-servers)
# list installed RSAT tools
Get-WindowsCapability -Name RSAT* -Online | Select-Object -Property DisplayName, State
# install ALL RSAT tools
Get-WindowsCapability -Name RSAT* -Online | ForEach-Object { Add-WindowsCapability -Online -Name "$($_.DisplayName)" }
# install a specific RSAT tool
Add-WindowsCapability -Online -Name "DISPLAY NAME HERE"
@tcartwright
tcartwright / GenerateDBDependecyHierarchy.ps1
Last active November 12, 2021 15:14
POWERSHELL: Generates a dependency graph in JSON format from SQL Server dependencies
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[string]$server,
[Parameter(Mandatory=$true)]
[string]$dbName
)
Clear-Host
@tcartwright
tcartwright / GenerateObjectDependencyGraph.sql
Last active November 9, 2021 21:36
SQL SERVER: Generate sys.objects hierarchical xml dependency graph
IF OBJECT_ID('dbo.fn_dependency_hierarchy') IS NOT NULL BEGIN
DROP FUNCTION dbo.fn_dependency_hierarchy
END
GO
CREATE FUNCTION dbo.fn_dependency_hierarchy (@rootObjectId INT, @objectId int) RETURNS XML
BEGIN RETURN (
SELECT CONCAT(QUOTENAME(OBJECT_SCHEMA_NAME (sed.referencing_id)), '.', QUOTENAME(OBJECT_NAME(sed.referencing_id))) AS [@name],
o.type_desc AS [@type],
dbo.fn_dependency_hierarchy (@rootObjectId, sed.referencing_id) AS [Dependencies]
@tcartwright
tcartwright / SafeShrinkDatabase.md
Last active January 3, 2023 16:18
POWERSHELL: Safely shrink database without fragmenting indexes
@tcartwright
tcartwright / CaptureTsqlErrors-Event.sql
Last active August 4, 2023 14:01
SQL SERVER: A system to capture t-sql errors and log them to a table
SET NOEXEC OFF
GO
-- Uncomment the :SETVAR lines out if running from SSMS, and enable SQLCMD mode
-- :SETVAR __UnInstall "0"
GO
/*
Original: https://www.sqlserver-dba.com/2020/03/extended-events-capture-query-errors.html