Skip to content

Instantly share code, notes, and snippets.

View ghotz's full-sized avatar

Gianluca Hotz ghotz

View GitHub Profile
@ghotz
ghotz / fix-alerts-delay-response-sqlauth.ps1
Last active October 15, 2022 21:52
Change SQL Server Agent Alert Delay Between Responses from 0 to 60
$cred = Get-Credential;
Get-DbaRegisteredServer -Group "Monitored\SQLAuth" | % {
Write-Host "connecting to $($_.ServerName)";
$srv = new-object Microsoft.sqlserver.management.smo.server $_.ServerName;
$srv.ConnectionContext.LoginSecure = $false;
$srv.ConnectionContext.Login = $Cred.UserName;
$srv.ConnectionContext.set_SecurePassword($cred.Password);
$srv.JobServer.Alerts | % {
if ($_.DelayBetweenResponses -eq 0) { $_.DelayBetweenResponses = 60; $_.Alter(); }
}
@ghotz
ghotz / present-time-interval.sql
Created October 13, 2022 15:46
Show different ways to represent the duration of a time interval starting form elapsed milliseconds
-- Show different ways to represent the duration of a time interval starting form elapsed milliseconds
-- adapted from https://stackoverflow.com/a/26347259
DECLARE @x bigint = (24*60*60*1000)-2; -- milliseconds
SELECT
CONVERT(varchar, @x / 1000 / 86400) + ':' + CONVERT(varchar, DATEADD(ms, (@x % 86400) * 1000, 0), 108) AS [DD:HH:MM:SS]
, CONVERT(varchar, @x / 1000 / 86400) + ':' + CONVERT(varchar, DATEADD(ms, @x, 0), 114) AS [DD:HH:MM:SS.MS]
, CASE WHEN CONVERT(varchar, @x / 1000 / 86400) > 0 THEN CONVERT(varchar, @x / 1000 / 86400) + ':' ELSE '' END + CONVERT(varchar, DATEADD(ms, (@x % 86400) * 1000, 0), 108) AS [{DD:}HH:MM:SS]
, CASE WHEN CONVERT(varchar, @x / 1000 / 86400) > 0 THEN CONVERT(varchar, @x / 1000 / 86400) + ':' ELSE '' END + CONVERT(varchar, DATEADD(ms, @x, 0), 114) AS [{DD:}HH:MM:SS.MS]
, CONVERT(varchar, @x / 1000 / 86400) + ' day(s) ' + CONVERT(varchar, DATEADD(ms, (@x % 86400) * 1000, 0), 108) AS [DD day(s) HH:MM:SS]
, CONVERT(varchar, @x / 1000 / 86400) + ' day(s) ' +
@ghotz
ghotz / search-column-name-all-dbs.sql
Created October 7, 2022 14:08
Search schema catalog for column names (for each database template)
-------------------------------------------------------------------------------
-- search schema catalog for column names
-- example template showing how to execute for each database
-------------------------------------------------------------------------------
DECLARE @Databases TABLE (DatabaseName sysname primary key);
DECLARE @DatabaseName sysname;
DECLARE @sqlstmt_replaced nvarchar(max);
-- temp table to hold merged results instead of multiple results sets
DROP TABLE IF EXISTS #tmp;
@ghotz
ghotz / filter-relog-counter-by-db.ps1
Last active October 7, 2022 05:13
Filter Relog counter files by SQL Server database names
# extract counters first with relog.exe .\myfile.blg -q > counters.txt
$currentPath = $PSScriptRoot # AzureDevOps, Powershell
if (!$currentPath) { $currentPath = Split-Path $pseditor.GetEditorContext().CurrentFile.Path -ErrorAction SilentlyContinue } # VSCode
if (!$currentPath) { $currentPath = Split-Path $psISE.CurrentFile.FullPath -ErrorAction SilentlyContinue } # PsISE
if ($currentPath) { Set-Location $currentPath }
#$Databases = ("databasename");
$Databases = @(Get-Content (Join-Path $currentPath "databases.txt"));
$LinesRead = 0;
@ghotz
ghotz / shuffling-update-example.sql
Created October 4, 2022 06:08
Demonstrate how to shuffle column values in SQL Server
USE AdventureWorks2017;
GO
SELECT * FROM Person.Person;
BEGIN TRANSACTION;
WITH cte AS
(
SELECT
@ghotz
ghotz / hasing-with-salting-examples.sql
Created October 4, 2022 06:02
Random hashing with salting examples (both preserving and not preserving equality of values in cryptovalues)
USE AdventureWorks2017;
GO
SELECT
LastName
, CRYPT_GEN_RANDOM(4) AS crypt_gen_rand_value -- only available SQL Server >= 2012
, CAST(NEWID() AS VARBINARY(4)) AS newid_value
, LEFT(CONVERT(nvarchar(128), HASHBYTES('SHA2_512', LastName), 2), 20) AS hashbytes_value
, LEFT(CONVERT(nvarchar(128), HASHBYTES('SHA2_512', CAST(LastName AS VARBINARY) + CRYPT_GEN_RANDOM(4)), 2), 20) AS hashbytes_crypt_gen_rand_append_value
, LEFT(CONVERT(nvarchar(128), HASHBYTES('SHA2_512', CAST(LastName AS VARBINARY) + CAST(NEWID() AS VARBINARY(4))), 2) , 20) AS hashbytes_newsid_append_value
@ghotz
ghotz / session-db-tempdb-usage.sql
Last active April 29, 2024 09:36
Session tempdb and log space usage information
WITH cte_sessions AS
(
SELECT
exs.session_id
, exs.database_id
, exs.last_request_start_time
, exs.last_request_end_time
, exs.memory_usage
, ssu.user_objects_alloc_page_count +
ssu.internal_objects_alloc_page_count AS tempdb_page_allocations
@ghotz
ghotz / lsn-hex-dec-conversions.sql
Created August 18, 2022 15:10
Converts LSN from hex to decimal and vice-versa
-- convert Hex LSN to decimal (e.g. from fb_dblog() to backup sets)
-- credit https://stackoverflow.com/questions/70213942/convert-hex-current-lsn-to-decimal-converter
WITH s as (
select Replace('00000016:000001a5:0001',':','.') lsn
)
select
Concat (
Convert(bigint,Convert(varbinary,Concat('0x',ParseName(lsn,3)),1)) * 1000000,
Convert(int,Convert(varbinary,Concat('0x',ParseName(lsn,2)),1)) * 10000,
Convert(int,Convert(varbinary,Concat('0x',ParseName(lsn,1)),1))
@ghotz
ghotz / tempdb-space-usage-adlldbs.sql
Created August 17, 2022 08:27
Get tempdb usage stats for all databases
DROP TABLE IF EXISTS #tmp
CREATE TABLE #tmp
(
[database_name] [NVARCHAR](128) NULL,
[database_id] [INT] NULL,
[file_id] [SMALLINT] NULL,
[filegroup_id] [SMALLINT] NULL,
[total_page_count] [BIGINT] NULL,
[allocated_extent_page_count] [BIGINT] NULL,
[unallocated_extent_page_count] [BIGINT] NULL,
@ghotz
ghotz / install-gmsa.ps1
Created July 21, 2022 09:35
SQL Server Installation snippets
Install-WindowsFeature RSAT-AD-PowerShell
Install-ADServiceAccount gMSAsqlservice
Test-ADServiceAccount gMSAsqlservice