Skip to content

Instantly share code, notes, and snippets.

View JustinMcNamara74's full-sized avatar

Justin McNamara JustinMcNamara74

View GitHub Profile
@JosiahSiegel
JosiahSiegel / fill_factor.sql
Created June 23, 2015 19:10
#MSSQL #Research Recommended Fill Factor
-- http://lassesen.com/msdn/Optimizing_Fill_Factors_for_SQLServer.pdf
SELECT
'[' + s.name + '].[' + t.name + ']' AS TableName,
'[' + i.name + ']' AS IndexName,
SUM(c.Max_length) AS [KeySize],
CASE
WHEN SUM(c.Max_length) <= 900 AND
SUM(c.Max_length) > 848 THEN '97'
WHEN SUM(c.Max_length) <= 848 AND
SUM(c.Max_length) > 403 THEN '96'
@JosiahSiegel
JosiahSiegel / set_fill_factor.sql
Last active October 15, 2015 18:39
#MSSQL Set Fill Factor for table
DECLARE @Table varchar(255) = 'TableName'
DECLARE @Test char(1) = 'N'
DECLARE @FillFactorOverride varchar(3) = NULL
-- SET @Test = 'Y' to only output computed fill factor values. No database modifications will occur.
-- Set @FillFactorOverride to value other than NULL to override computed fill factor value
-- Example: @FillFactorOverride = '90'
-- =================================
DECLARE @CursorTable varchar(255)
DECLARE @MyIndex varchar(255)
DECLARE @MyFillFactor varchar(3)
@JosiahSiegel
JosiahSiegel / link_user.sql
Last active May 26, 2020 14:09
#MSSQL #Users Link database user with login
@JosiahSiegel
JosiahSiegel / read_error_log.sql
Last active January 18, 2016 14:00
#MSSQL #Research #Users Read error log
DECLARE @temp_error_log TABLE
(
date datetime,
process varchar(50) null,
text varchar(MAX) null
)
INSERT INTO @temp_error_log
EXEC sp_readerrorlog 0, 1--, 'Login failed'
@JosiahSiegel
JosiahSiegel / find_user_login.sql
Created July 22, 2015 14:46
#MSSQL #Research #Users Find matching login for user
SELECT sp.name AS login_name
FROM sys.server_principals sp
JOIN sys.database_principals dp ON (sp.sid = dp.sid)
WHERE dp.name = 'Joe'
@JosiahSiegel
JosiahSiegel / last_user_login.sql
Last active October 15, 2015 18:35
#MSSQL #Research #Users Check error log for last user login
DECLARE @temp_error_log TABLE
(
date datetime,
process varchar(50) null,
text varchar(MAX) null
)
INSERT INTO @temp_error_log
EXEC sp_readerrorlog 0, 1, 'Login'
[user]
email = [email protected]
name = That Guy
[push]
default = matching
[alias]
tree = log --graph --decorate --pretty=oneline --abbrev-commit
[hub]
protocol = https
[core]
@JosiahSiegel
JosiahSiegel / log_sp.sql
Created August 31, 2015 20:53
#MSSQL Log stored procedure run times and errors
/*
LOG STORED PROCEDURE RUN TIMES AND ERROR MESSAGES
Example:
====================
-- Place at beginning of your stored procedure
DECLARE @Parameters VARCHAR(200) = NULL;
SET @Parameters = 'Account: ' + ISNULL(@Account,'') + ' UserID: ' + ISNULL(@UserID,'');
DECLARE @StartTime DATETIME = GETDATE();
EXEC Call_ssProcedureLog @ObjectID = @@PROCID, @StartDate = @StartTime, @AdditionalInfo = @Parameters;
@JosiahSiegel
JosiahSiegel / kill_job_spids.sql
Created September 1, 2015 20:17
#MSSQL Auto KILL SPIDs based upon job name
DECLARE @spid INT
DECLARE @kill_spid NVARCHAR(100)
DECLARE running_jobs CURSOR FOR
SELECT
req.session_id
FROM sys.dm_exec_requests req
LEFT JOIN sys.dm_exec_sessions ses ON ses.session_id = req.session_id
LEFT JOIN msdb.dbo.sysjobs jobs ON SUBSTRING(ISNULL(ses.[program_name],''),CHARINDEX('0x', ISNULL(ses.[program_name],'')) + 18, 16) = SUBSTRING(REPLACE(ISNULL(jobs.[job_id],''), '-',''),17,16)
@JosiahSiegel
JosiahSiegel / emergency_rollback.bat
Last active April 26, 2017 12:59
#MSSQL #Users Prevent login access to particular applications
REM Required: "-A" for DAC connection. Optional "-E" for trusted connection (instead of "-U -P").
sqlcmd -S 127.0.0.1 -U MyAdmin -P Admin123 -q "exit(DROP TRIGGER [Restrict_Application_Access_Login_Trigger] ON ALL SERVER)" -A