Skip to content

Instantly share code, notes, and snippets.

View EitanBlumin's full-sized avatar
🕵️‍♂️
Figuring it out

Eitan Blumin EitanBlumin

🕵️‍♂️
Figuring it out
View GitHub Profile
@EitanBlumin
EitanBlumin / Delete_Batches_From_Table_By_DateTime_Column.sql
Created December 26, 2020 17:01
Generic script to delete time based data in batches
DECLARE
@DatabaseName SYSNAME = 'MyDB',
@TableName SYSNAME = 'MyTable',
@DateTimeColumnName SYSNAME = 'MyColumn',
@ThresholdDateTime DATETIME = DATEADD(DAY, -14, GETDATE()),
@BatchSize INT = 10000,
@SleepBetweenBatches VARCHAR(17) = '00:00:00.6'
SET NOCOUNT ON;
@EitanBlumin
EitanBlumin / R_myReadXcelProc.sql
Last active December 25, 2020 04:52
R stored procedure to load Excel files, by Matteo Lorini
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/*
Based on sample by Matteo Lorini:
https://www.mssqltips.com/sqlservertip/6622/stored-procedure-in-sql-server-with-r-code/
The readxl package needs to be installed first:
@EitanBlumin
EitanBlumin / SQLDump_File_Examine.sql
Created December 8, 2020 12:38
T-SQL script to read the contents of a SQLDump file's txt file
/*
Read contents of a SQLDump file's txt file
=============================================
Author: Eitan Blumin | https://www.madeiradata.com
Date: 2020-12-08
*/
SET NOCOUNT, ARITHABORT, XACT_ABORT ON;
DECLARE @FilePath NVARCHAR(4000), @CMD NVARCHAR(MAX)
-- Use below to read the contents of latest memory dump file (mdmp) generated by the current instance:
@EitanBlumin
EitanBlumin / Powershell_Template_with_Transcript.ps1
Last active February 3, 2021 14:46
Template Powershell script with built-in transcript log management
# when creating a scheduled task to run such scripts, use the following structure example:
# powershell.exe -NoProfile -ExecutionPolicy Bypass -File "C:\Madeira\Powershell_Template_with_Transcript.ps1"
Param
(
[string]$logFileFolderPath = "C:\Madeira\log",
[string]$logFilePrefix = "my_ps_script_",
[string]$logFileDateFormat = "yyyyMMdd_HHmmss",
[int]$logFileRetentionDays = 30
)
Process {
@EitanBlumin
EitanBlumin / Redundant_Indexes.sql
Created December 6, 2020 17:15
T-SQL script to find redundant indexes within the current database, also outputs index usage stats and drop commands
SET NOCOUNT ON;
DECLARE @MinimumRowsInTable INT = 200000;
IF OBJECT_ID('tempdb..#FindOnThisDB') IS NOT NULL DROP TABLE #FindOnThisDB;
;WITH Indexes AS
(
select
sets.schema_id,
sets.table_o_id,
sets.key_column_list,
sets.include_column_list,
@EitanBlumin
EitanBlumin / Invalid_owner_for_system_schema_role_or_database.sql
Created November 25, 2020 08:27
TSQL script to check for invalid owners of system Roles, Schemas, or Databases
/*
Invalid owner for a system Role, Schema, or Database
====================================================
Author: Eitan Blumin | Madeira Data Solutions | https://www.madeiradata.com
Date: 2020-11-25
Description:
System roles and schemas must have specific owning users or roles.
For example, all system database roles such as db_owner, db_datawriter, db_datareader, etc. must be owned by dbo.
@EitanBlumin
EitanBlumin / Parse Vulnerability Assessment Result Files into HTML.sql
Last active June 3, 2022 09:32
Script to parse multiple Vulnerability Assessment Tool result files into an HTML reference list ( https://eitanblumin.com/sql-vulnerability-assessment-tool-rules-reference-list/ )
/******************************************************************
Parse SQL Vulnerability Assessment Tool Results
***********************************************
Author: Eitan Blumin | https://www.eitanblumin.com
Description:
Use this script to parse a Vulnerability Assessment Tool
results file into a relational structure and save in an HTML page.
This will output the T-SQL queries used by VAT behind the scenes
and their respective meta-data, as displayed in the VAT.
@EitanBlumin
EitanBlumin / MaxDOP_Configuration_Check.sql
Created September 16, 2020 10:09
T-SQL script to check and make sure that the configured MAXDOP is in the recommended range based on Microsoft best practice
/*
Check that the configured value for MAXDOP is in the recommended range, as described in this KB article:
https://support.microsoft.com/en-us/help/2806535/recommendations-and-guidelines-for-the-max-degree-of-parallelism-confi
If @WhatIf = 0 then MAXDOP will automatically be changed to the recommended setting.
*/
-- change this to 1 to only display findings without actually changing the config:
DECLARE @WhatIf BIT = 0;
--------------------------------------
@EitanBlumin
EitanBlumin / access_violation_dm_db_index_operational_stats.sql
Last active October 4, 2020 20:08
psst! hey, kid! wanna see an access violation error? here, run this on a SQL Server with MAXDOP != 1
/*
=================================================
Reproduce an access violation error in SQL Server
=================================================
The following script reproduces an Access Violation error
caused by a parallelism plan involving specific system table functions.
The error occurses ONLY with parallel execution plans.
Discovered by Eitan Blumin and Nathan Lifshes on 2020-09-06
The error has been reproduced in multiple SQL Server versions that were tested:
@EitanBlumin
EitanBlumin / SentryOne_AlwaysOn_Inventory_Check.sql
Created August 20, 2020 08:53
Query to run in the SentryOne database to check your inventory of AlwaysOn Availability Groups
SELECT SiteName, ReplicaName
, NumberOfAvailabilityGroups = COUNT(DISTINCT AGName)
, NumberOfPrimaries = COUNT(DISTINCT PrimaryReplica)
FROM
(
SELECT DISTINCT
ES.ObjectName AS ReplicaName, S.Name AS SiteName
, AG.Name AS AGName
, AG.PrimaryReplica
FROM [SentryOne].[AlwaysOn].[AvailabilityGroup] AS AG