Skip to content

Instantly share code, notes, and snippets.

View ghotz's full-sized avatar

Gianluca Hotz ghotz

View GitHub Profile
@ghotz
ghotz / databases-users-access-other-databases.sql
Created April 4, 2023 17:00
Get database users and all other databases they have access to
DROP TABLE IF EXISTS #tmp;
CREATE TABLE #tmp (
[database_name] sysname NOT NULL
, [user_name] sysname NOT NULL
, [type_desc] nvarchar(120) NOT NULL
, [user_sid] varbinary(85) NULL
, [login_name] sysname NULL
);
GO
@ghotz
ghotz / post-upgrade.sql
Created February 3, 2023 09:05
Post upgrade
exec sp_msforeachdb 'USE [?]; exec sp_updatestats;'
exec sp_msforeachdb 'DBCC CHECKALLOC([?])'
@ghotz
ghotz / check-last-page-by-object-and-rebuild.sql
Last active December 1, 2022 07:18
Get the per-object last allocated page in files and generate alter index rebuild to move it to the beginning
-- Notes
--
-- 1) you may find that the allocated pages toward the end of files belong to
-- one or more system objects, in which case you can't rebuild/reorg them, so you
-- may need to shrink the files to move at least these and then resume from the next
-- non system object
--
-- 2) rebuilding heaps is not implemented, it's just a matter to add a CASE, I'll do it
-- the first time I have to so that I can test it
--
@ghotz
ghotz / xe.classify-recompiles.sql
Created November 9, 2022 18:13
Trace recompilations and statistics usage with Extended Events
/*
IF EXISTS (SELECT * FROM sys.dm_xe_sessions WHERE [name] = 'classify_recompiles')
DROP EVENT SESSION [classify_recompiles] ON SERVER;
CREATE EVENT SESSION [classify_recompiles] ON SERVER
ADD EVENT sqlserver.sql_statement_recompile(
WHERE ([source_database_id]>(4)) -- skip system databases
)
ADD TARGET package0.histogram
(SET filtering_event_name = N'sqlserver.sql_statement_recompile', source = N'recompile_cause', source_type = (0))
@ghotz
ghotz / exiftool-snippets.md
Last active June 11, 2024 21:27
Exiftool copy & paste snippets

Exiftool snippets

General

To recurse sub-directories, add -r, specify . and filter by extension.

Filtering only some extensions:

  • photos -ext jpg -ext orf -ext ori -ext dng -ext heic -ext PEF -ext RAF -ext SRF -ext NEF -ext X3F -ext KDC
  • videos -ext mp4 -ext mov -ext mts -ext avi
@ghotz
ghotz / check-sqlclr-domains.sql
Created October 28, 2022 15:07
SQLCLR troubleshooting
select left(appdomain_name, 19) as appdomain_name, [state]
, sum(total_allocated_memory_kb) / 1024. AS total_allocated_memory_MB
, sum(survived_memory_kb) / 1024. as survived_memory_MB
from sys.dm_clr_appdomains
group by left(appdomain_name, 19), [state]
--select * from sys.dm_clr_properties
--select * from sys.dm_clr_appdomains
--select * from sys.dm_clr_loaded_assemblies
@ghotz
ghotz / SetDatabaseSingleUser.sql
Last active October 18, 2022 20:14
Handle SQL Server Single User scenarios
DROP PROCEDURE IF EXISTS dbo.SetDatabaseSingleUser
GO
CREATE PROCEDURE dbo.SetDatabaseSingleUser
@DatabaseName sysname
, @debug tinyint = 0
AS
IF @debug = 1 PRINT FORMATMESSAGE('%s %s: stored procedure starting', convert(varchar(25), getdate(), 120), QUOTENAME(OBJECT_NAME(@@PROCID)));
IF @debug = 1 PRINT FORMATMESSAGE('%s %s: parameter @DatabaseName=''%s''', convert(varchar(25), getdate(), 120), QUOTENAME(OBJECT_NAME(@@PROCID)), @DatabaseName);
SET NOCOUNT ON;
SET XACT_ABORT ON;
@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;