This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DECLARE @MailProfile sysname = N''; | |
DECLARE @AgentOperator sysname = ''; | |
DECLARE @MailRecipients nvarchar(max) = (select email_address from msdb..sysoperators WHERE [name] = @AgentOperator); | |
DECLARE @MailSubject nvarchar(max) = N'Daily users with high privileges on server ' + @@SERVERNAME; | |
DECLARE @MailQueryTable nvarchar(max) = | |
N'<h1>Daily users with high privileges on server ' + @@SERVERNAME + N' report</h1>' | |
+ N'<table border="1">' | |
+ N'<tr><th>Instance Name</th><th>Login Name</th><th>Login Type</th><th>Creation Date</th><th>Permission Type</th></tr>' | |
+ CAST(( | |
SELECT |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DECLARE @sqlstmts nvarchar(max); | |
WITH cte_sqlstmts AS | |
( | |
SELECT | |
N'exec msdb.dbo.sp_stop_job @job_name = ''' + [name] + N'''; ' AS sqlstmt | |
FROM msdb.dbo.cdc_jobs AS J1 | |
JOIN msdb.dbo.sysjobs_view AS J2 | |
ON J1.job_id = J2.job_id | |
JOIN msdb.dbo.sysjobactivity AS A1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
CASE | |
WHEN I1.[type_desc] IN ('HEAP', 'CLUSTERED') | |
THEN 'ALTER TABLE [' + S1.[name] + '].[' + O1.[name] + ']' | |
WHEN I1.[type_desc] = 'NONCLUSTERED' | |
THEN 'ALTER INDEX [' + I1.[name] + '] ON [' + S1.[name] + '].[' + O1.[name] + ']' | |
END | |
+ ' REBUILD' + | |
CASE | |
WHEN EXISTS (SELECT * FROM sys.partitions AS P2 WHERE P1.[object_id] = P2.[object_id] AND P1.[index_id] = P2.[index_id] AND P2.partition_number > 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
USE master; | |
GO | |
-- enable external scripting at instance level | |
EXEC sp_configure 'external scripts enabled', 1; | |
RECONFIGURE WITH OVERRIDE; | |
GO |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Get-NetFirewallRule | ? {$_.Name -like "HNS Container Networking - DNS (UDP-In)*" } | Remove-NetFirewallRule | |
Get-NetFirewallRule | ? {$_.Name -like "HNS Container Networking - ICS DNS (TCP-In)*" } | Remove-NetFirewallRule |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Search statistics with leading column name | |
SELECT | |
OBJECT_SCHEMA_NAME(C2.[object_id]) AS [schema_name] | |
, OBJECT_NAME(C2.[object_id]) AS [table_name] | |
, S1.[name] AS stats_name | |
, S1.auto_created | |
, P1.* | |
FROM sys.stats AS S1 | |
JOIN sys.stats_columns AS C1 ON S1.[object_id] = C1.[object_id] AND S1.stats_id = C1.stats_id | |
JOIN sys.columns AS C2 ON C1.[object_id] = C2.[object_id] AND C1.column_id = C2.column_id |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exec sp_msforeachdb 'USE [?]; exec sp_updatestats;' | |
exec sp_msforeachdb 'DBCC CHECKALLOC([?])' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 | |
-- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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)) |