Skip to content

Instantly share code, notes, and snippets.

View JustinMcNamara74's full-sized avatar

Justin McNamara JustinMcNamara74

View GitHub Profile
@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;
[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 / 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'
@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 / 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 / link_user.sql
Last active May 26, 2020 14:09
#MSSQL #Users Link database user with login
@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 / 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 / query_user.sql
Last active February 16, 2018 13:00
#LDAP Query user by sAMAccountName
SELECT $displayName, $cn FROM ROOTDSE
WHERE $objectClass='user' AND $objectCategory='Person'
AND $sAMAccountName='x0000000' --AND $cn='';
@JosiahSiegel
JosiahSiegel / role_permissions.sql
Last active January 2, 2025 17:41
#MSSQL #Users #Research Admins, owners, users, roles, role members, and database permissions
-- server 'sysadmin'
SELECT name as 'sysadmin',type_desc,is_disabled
FROM master.sys.server_principals
WHERE IS_SRVROLEMEMBER ('sysadmin',name) = 1
ORDER BY name
-- database owner
select suser_sname(owner_sid) as 'owner'
from sys.databases
where name = DB_NAME()