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 / DUISample.sql
Last active December 11, 2023 21:56
SQL SERVER: DUI Pattern Example
/* Use DUI pattern to mimic merge */
DELETE aq
FROM dbo.AppQuestionResponse aq
LEFT JOIN @QuestionAnswers aqtt ON aq.appQuestionID = aqtt.QuestionID
WHERE aq.policyID = @policyID
AND aqtt.QuestionID IS NULL
UPDATE aq
SET aq.response = aqtt.answer, aq.comment = aqtt.Explanation
@tcartwright
tcartwright / StartAllAppPools.ps1
Last active August 23, 2023 17:21
POWERSHELL: Start all app pools regardless of server / workstation
#Requires -RunAsAdministrator
$osInfo = Get-WmiObject -Class Win32_OperatingSystem
# $osInfo | Format-List *
if ($osInfo.ServicePackMajorVersion -gt 0) {
$spInfo = " SP $($osInfo.ServicePackMajorVersion).$($osInfo.ServicePackMinorVersion))"
}
Write-Output "Running on $($osInfo.PSComputerName) ($($osInfo.Caption) Build $($osInfo.properties["BuildNumber"].Value)$spInfo)"
@tcartwright
tcartwright / AddCheckAGJobStepToJobs.sql
Created July 7, 2023 19:10
SQL SERVER: Adds a step to all agent jobs to check if the job is running on the AG Primary and bails if not the primary. Ignores stand alone servers.
DECLARE @job_name sysname,
@job_id UNIQUEIDENTIFIER;
DECLARE inject_jobs_cursor CURSOR FAST_FORWARD
FOR (
SELECT [j].[name], [j].[job_id]
FROM msdb.dbo.[sysjobs] AS [j]
WHERE [j].[enabled] = 1
AND NOT EXISTS (
SELECT * FROM [msdb].[dbo].[sysjobsteps] AS [s2] WHERE [s2].[job_id] = [j].[job_id] AND [s2].[step_name] = 'CHECK AG'
@tcartwright
tcartwright / sp_GetLongRunningJobHistory.sql
Last active July 13, 2023 13:28
SQL SERVER: Get Long Running Job History
USE [master]
GO
CREATE OR ALTER PROC dbo.sp_GetLongRunningJobHistory (
@DaysPast INT = 1,
@HistoryStartDate DATETIME = NULL, /* @HistoryStartDate - Start date for historical average */
@HistoryEndDate DATETIME = NULL, /* @HistoryEndDate - End date for historical average */
@MinHistExecutions INT = 1.0, /* @MinHistExecutions - Minimum number of job runs we want to consider */
@MinAvgSecsDuration INT = 1.0 /* @MinAvgSecsDuration - Threshold for minimum duration we care to monitor */
) AS
@tcartwright
tcartwright / sp_ConvertQuery2HTMLTable.sql
Last active July 7, 2023 17:32
SQL SERVER: Generates an html table from a query
USE [master]
GO
CREATE OR ALTER PROC dbo.sp_ConvertQuery2HTMLTable (@SQLQuery NVARCHAR(MAX))
AS
BEGIN
/*
Original Source: https://www.mssqltips.com/sqlservertip/5025/stored-procedure-to-generate-html-tables-for-sql-server-query-output/
Alterations:
Tim Cartwright:
@tcartwright
tcartwright / GoErrorHandlingWithTransaction.sql
Created June 13, 2023 15:08
SQL SERVER: Using transactions, and error handling with GO statements
/* Borrowed this pattern from Red-gates change scripts */
/****************************************************************/
-- TOP OF SCRIPT
/****************************************************************/
SET NOEXEC OFF
SET XACT_ABORT ON
BEGIN TRANSACTION
/****************************************************************/
-- TOP OF SCRIPT
# https://4bes.nl/2021/09/19/update-all-powershell-modules-on-a-system/
<#
TIM C: Changes:
- Added scope parameter so scope could be controlled
- altered code to always check for old versions, as this script may not have done the install, but it can still remove old versions
- changed contains and othercomparison syntax to be case insensitive
- altered logic around when the module is not found in the gallery to make the verbose output clearer
- added version parses around the version compares so string comparisons do not screw up the comparison
- added admin check when using AllUsers
@tcartwright
tcartwright / DisconnectVPN.ps1
Last active January 12, 2023 15:33
POWERSHELL: Disconnect any VPN
Clear-Host
Get-VpnConnection | Where-Object { $_.ConnectionStatus -ieq "Connected" } | ForEach-Object {
Write-Host "Disconnecting $($_.Name)"
. rasdial "$($_.Name)" /DISCONNECT
}
Write-Host "Done"
@tcartwright
tcartwright / DownloadPackage2.ps1
Last active January 12, 2023 02:32
POWERSHELL: Allows for installing of packages, even ones with circular dependencies. 2nd version of this script
function UpdateList {
param($list, $key, [version]$version)
if (-not $list.ContainsKey($key)) {
$list.Add($key, $version)
} elseif ($list[$key] -lt $version) {
Write-Host "Updating package [$key] depenency version from $($list[$key]) to $version" -ForegroundColor Yellow
$list[$key] = $version
}
@tcartwright
tcartwright / FixIndirectCheckPoints.sql
Created November 21, 2022 21:35
SQL SERVER: Fix indirect checkpoints setting
-- https://sqlperformance.com/2020/05/system-configuration/0-to-60-switching-to-indirect-checkpoints
SELECT d.name, [d].[target_recovery_time_in_seconds], CONCAT('ALTER DATABASE [', d.name, '] SET TARGET_RECOVERY_TIME = 60 SECONDS WITH NO_WAIT')
FROM sys.databases d
WHERE [d].[database_id] > 4
AND [d].[target_recovery_time_in_seconds] = 0