-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDecommission and Cleanups.sql
More file actions
executable file
·108 lines (96 loc) · 3.46 KB
/
Decommission and Cleanups.sql
File metadata and controls
executable file
·108 lines (96 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**********************************************************************************************/
-- DROP all functions from the database !!! WARNING !!!
SELECT 'DROP FUNCTION [' + SCHEMA_NAME(o.schema_id) + '].[' + o.name + ']'
FROM sys.sql_modules m
INNER JOIN sys.objects o
ON m.object_id=o.object_id
WHERE type_desc like '%function%'
/**********************************************************************************************/
-- DROP all procedures from the database !!! WARNING !!!
SELECT 'DROP PROCEDURE [' + SCHEMA_NAME(p.schema_id) + '].[' + p.NAME + ']'
FROM sys.procedures p
/**********************************************************************************************/
/*
The following query shows DBs which have had no usage since the
last restart, without relying on query plans being held in the cache,
as it shows user IO against the indexes (and heaps). This is sort of
along the lines of using virtual file stats, but the DMV used here
excludes IO activity from backups. No need to keep a profiler trace
running, no triggers or auditing required. Of course, if you restart
your SQL server frequently (or you attach/shutdown databases often)
this might not be the way to go:
*/
select [name] from sys.databases
where database_id > 4
AND [name] NOT IN
(select DB_NAME(database_id)
from sys.dm_db_index_usage_stats
where coalesce(last_user_seek, last_user_scan, last_user_lookup,'1/1/1970') >
(select login_time from sysprocesses where spid = 1))
/**********************************************************************************************/
-- OFFLINE all databases !!! WARNING !!!
SELECT '
USE [master]
;' + CHAR(13)+CHAR(10) + '
ALTER DATABASE [' + name + '] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
;' + CHAR(13)+CHAR(10) + '
USE [master]
;' + CHAR(13)+CHAR(10) + '
ALTER DATABASE [' + name + '] SET OFFLINE WITH ROLLBACK IMMEDIATE
;' + CHAR(13)+CHAR(10)
FROM sys.databases WHERE name NOT IN
(
'msdb',
'model',
'master',
'tempdb',
'msdb',
'distribution'
)
/**********************************************************************************************/
-- DETACH all databases !!! WARNING !!!
/*
SELECT '
USE [master]
;' + CHAR(13)+CHAR(10) + '
ALTER DATABASE [' + name + '] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
;' + CHAR(13)+CHAR(10) + '
USE [master]
;' + CHAR(13)+CHAR(10) + '
EXEC master.dbo.sp_detach_db @dbname = N''' + name + '''
;' + CHAR(13)+CHAR(10)
FROM sys.databases WHERE name NOT IN
(
'msdb',
'model',
'master',
'tempdb',
'msdb',
'distribution'
)
*/
/**********************************************************************************************/
-- Dropping all user-defined stored procs
SELECT 'DROP PROCEDURE [' + SCHEMA_NAME(o.schema_id) + '].[' + o.NAME + ']'
FROM sys.objects o WHERE type = 'p'
/**********************************************************************************************/
-- Dropping all user-defined functions
SELECT 'DROP FUNCTION [' + SCHEMA_NAME(o.schema_id) + '].[' + o.NAME + ']'
FROM sys.objects o where type in ( 'FN', 'IF', 'TF' )
/**********************************************************************************************/
-- Dropping all non-system users from the database.
declare @sql nvarchar(max)
set @sql = ''
SELECT @sql = @sql+
'
print ''Dropping '+name+'''
DROP USER [' + ''+name+'' + ']
'
FROM
sys.database_principals
WHERE
name NOT IN('dbo','guest','INFORMATION_SCHEMA','sys','public')
AND TYPE <> 'R'
order by
name
execute (@sql)