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 / SimpleMaintenance.sql
Created February 15, 2024 21:00
SQL SERVER: Simple maintenance scripts
/*****************************************************************************************************************************************/
/**** SHRINK LOG FILES *******************************************************************************************************************/
/*****************************************************************************************************************************************/
DECLARE @sql VARCHAR(max) = ''
SELECT @sql += CONCAT('USE [', DB_NAME([mf].[database_id]), ']', CHAR(13), CHAR(10), 'DBCC SHRINKFILE (''', [mf].[name], ''', 1) WITH NO_INFOMSGS;', CHAR(13), CHAR(10))
FROM sys.[master_files] AS [mf]
WHERE [mf].[type_desc] = 'LOG'
@tcartwright
tcartwright / GitPopCommit.bat
Created January 16, 2024 22:51
GIT: Pop last commit off
@rem remove last commit and destroy it
git reset --hard HEAD~1
@rem remove last commit and edit it
git reset HEAD~1
@tcartwright
tcartwright / NugetCacheClear.bat
Created January 5, 2024 21:01
NUGET: Clears all local caches
@REM https://docs.nuget.org/consume/command-line-reference
@call "%ProgramFiles%\Microsoft Visual Studio\2022\Professional\Common7\Tools\VsDevCmd.bat"
nuget locals all -clear
@tcartwright
tcartwright / MoveGitProject.cmd
Last active January 3, 2024 19:35
GIT: Moves a project from one repo to another preserving history
@REM Make sure you are in the root folder of the git repo that you wish to move the code to
git checkout master
git remote add r1remote **url-of-repo1**
git fetch r1remote
git merge r1remote/master --allow-unrelated-histories
git remote rm r1remote
@tcartwright
tcartwright / MyContextReadOnly.cs
Last active January 3, 2024 19:33
C#: Read only configuration context for sql server
public partial class MyContextReadOnly : MyContext
{
private readonly string _connectionString = null;
public MyContextReadOnly(DbContextOptions<MyContextReadOnly> options) : base(options)
{
var sqlServerOptionsExtension = options.FindExtension<SqlServerOptionsExtension>()!;
_connectionString = sqlServerOptionsExtension?.ConnectionString!;
}
@tcartwright
tcartwright / RunTimeStringInterprolation.cs
Last active December 1, 2023 15:25
C#: Run time interprolation
public class NamedParameter
{
public string Name { get; set; } = null!;
public object Value { get; set; }
public NamedParameter(string name, object value)
{
if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentNullException(nameof(name)); }
if (value == null) { throw new ArgumentNullException(nameof(value)); }
Name = name;
Value = value;
@tcartwright
tcartwright / TVCCreate.sql
Created September 18, 2023 17:36
SQL SERVER: TVC: Create table variable from query
/**
Copyright (c) Red Gate Software Ltd 2021
All rights Reserved. Use of this code is subject to the terms of a license agreement with Red Gate Software Limited.
This is a temporary batch for creating such things as table variables, temporary tables or anything else that needs a column list.
For more info, see https://www.red-gate.com/hub/product-learning/sql-prompt/building-reusable-table-build-scripts-using-sql-prompt
**/
@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'