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 / Find Unused Indexes.sql
Created January 22, 2020 07:47
Script to find unused indexes in all databases, for tables that are old (more than 30 days old) and big (more than 200k rows)
IF OBJECT_ID('tempdb..#tmp') IS NOT NULL DROP TABLE #tmp;
CREATE TABLE #tmp (DBName SYSNAME, SchemaName SYSNAME, TableName SYSNAME, IndexName SYSNAME NULL, RowsCount INT, IndexSizeKB INT, UpdatesCount INT NULL, DropCMD NVARCHAR(MAX), TableCreatedDate DATETIME NULL, LastStatsDate DATETIME);
INSERT INTO #tmp(DBName, SchemaName, TableName, IndexName, RowsCount, IndexSizeKB, DropCMD, LastStatsDate, TableCreatedDate, UpdatesCount)
EXEC sp_MSforeachdb N'
IF EXISTS (SELECT * FROM sys.databases WHERE database_id > 4 AND name = ''?'' AND state_desc = ''ONLINE'' AND DATABASEPROPERTYEX([name], ''Updateability'') = ''READ_WRITE'')
BEGIN
USE [?];
PRINT DB_NAME();
SELECT
# Source: https://www.sentryone.com/blog/powershell-gui-to-script-back-to-gui
# GUI generated using POSHGUI: https://poshgui.com/
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '600,800'
$Form.text = "Form"
$Form.TopMost = $false
@EitanBlumin
EitanBlumin / Standardize_Database_File_Sizes_and_AutoGrowth.sql
Last active September 17, 2020 01:56
Minimal standardization of all Database Files in a SQL Server instance
/*
Author: Eitan Blumin (t: @EitanBlumin | b: eitanblumin.com)
Date: 2020-01-28
Description:
This script generates commands to implement a minimal standardization of all database files in the instance.
List of implemented standards:
1. Files must not be allowed to have percentage growth (defaults to 1GB growth instead as a replacement)
2. Files must all have UNLIMITED max size
3. Log files must be at least 64MB in size
4. Log file auto-growth must be in power multiples of 2 between 64MB and 2048MB (i.e. 64,128,256,512,1024,2048) (defaults to 1GB growth instead as a replacement)
@EitanBlumin
EitanBlumin / Shrink_Database_File_in_Specified_Increments.sql
Last active March 10, 2025 18:42
Shrink a database file in specified increments down to a specific size or percentage of used space
/*
----------------------------------------------------------------------------
Shrink a Database File in Specified Increments
----------------------------------------------------------------------------
Author: Eitan Blumin (t: @EitanBlumin | b: eitanblumin.com)
Creation Date: 2020-01-05
Last Update: 2020-08-23
----------------------------------------------------------------------------
Description:
This script uses small intervals to shrink a file (in the current database)
@EitanBlumin
EitanBlumin / Deploy SSRS Reports.ps1
Last active May 12, 2025 03:18
Powershell script to deploy SSRS reports from a folder (all rds and rdl files)
Param
(
[string] $SourceFolder = "C:\SSRS\My-Reports",
[string] $TargetReportServerUri = "http://localhost:8081/ReportServer",
[PSCredential] $Credential,
[switch] $CustomAuthentication,
[string] $ApiVersion,
[string] $TargetFolder = "/My-Reports/Sample-Reports",
[switch] $Recursive,
[string] $OverrideDataSourcePathForAll, #= "/My-Reports/Data Sources/ProdDS",
@EitanBlumin
EitanBlumin / Create_Named_SQL_Server_Linked_Server.sql
Last active September 30, 2020 08:43
Create a Linked Server to a remote SQL Server, but give it a different name than its actual address
/*
Author: Eitan Blumin (t: @EitanBlumin | b: eitanblumin.com)
Description: Create a Linked Server to a remote SQL Server, but give it a different name than its actual address
*/
DECLARE
@ServerAddress [nvarchar](255) = 'MyRemoteServerAddress\SomeNamedInstanceIfYouWant,1433',
@NewServerName [nvarchar](255) = 'MyRemoteServerName',
@RemoteUser [nvarchar](128) = 'remote_user',
@RemotePassword [nvarchar](128) = 'remote_user_password',
@MapLocalLogin [nvarchar](255) = NULL -- name a local login to map to the remote login. If NULL, will map current login
@EitanBlumin
EitanBlumin / Basic SQL Server table migration using Powershell.ps1
Last active September 30, 2020 08:53
Basic SQL Server table migration using Powershell
# Author: Nathan Lifshes
# Date: 2020-01-13
Param (
[string] $SrcServer ,
[string] $SrcDatabase ,
[string] $DestServer ,
[string] $DestDatabase ,
[string] $SrcUsername = "" ,
[string] $SrcPassword = "" ,
[string] $DestUsername = "" ,
CREATE EVENT SESSION [TrackFailedLogins] ON SERVER
ADD EVENT sqlserver.error_reported(
    ACTION(sqlserver.client_app_name,sqlserver.client_hostname,sqlserver.nt_username,sqlserver.database_id,sqlserver.session_id)
WHERE (([severity]=(20) OR [severity]=(14) OR [severity]=(16))
AND ([error_number]=(18056)
OR [error_number]=(17892)
OR [error_number]=(18061)
OR [error_number]=(18452)
OR [error_number]=(11248)
OR [error_number]=(17806)
@EitanBlumin
EitanBlumin / SQL_Server_Error_Log_Based_on_Severity_with_Full_Message.sql
Last active September 17, 2020 01:52
T-SQL script to output a single row per each high severity error from the SQL Server Error Log
-- Author: Eitan Blumin (t: @EitanBlumin | b: eitanblumin.com)
-- Description: Get a single row per each high severity error from the SQL Server Error Log
DECLARE
@SampleTime DATETIME = DATEADD(MINUTE,-30,SYSDATETIME())
, @MinimumSeverity INT = 17
, @MaximumSeverity INT = 25;
IF OBJECT_ID(N'tempdb..#errors') IS NOT NULL
DROP TABLE #errors;
@EitanBlumin
EitanBlumin / generate_rename_system_named_default_constraints.sql
Created February 26, 2020 08:21
Generate rename commands for all system-named default constraints
-- Author: Eitan Blumin | https://www.eitanblumin.com
-- Date: 2020-02-26
-- Description: This is a query to generate rename commands for all system-named default constraints within the current database.
-- The constraints are renamed based on convention of "DF_{TableName}_{ColumnName}"
-- Simply run this query and then copy & paste the entire remediationCommand column to get the script(s).
SELECT
schemaName = sch.[name],
tableName = tab.[name],
columnName = col.[name],