-
Notifications
You must be signed in to change notification settings - Fork 4
/
Management.GatherTableMetadata.sql
183 lines (163 loc) · 5.88 KB
/
Management.GatherTableMetadata.sql
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/************************************************************************************************
Metadata
By Wesley D. Brown
Date 06/27/2011
**Description**
This uses sys.extended_properties to pull back comments attached to columns and tables.
It requires that MS_Description be the name of the property.
It also pulls back all column definitions stored in the systables for for that database.
Functions:
**End Discription**
**Change Log**
Bug Fix:
**End Change Log**
************************************************************************************************/
/************************************************************************************************
* Create these tables first if they don't exist in your system.
CREATE TABLE [dbo].[TableMetadata] (
[ServerName] [varchar] (256),
[DBName] [varchar] (256),
[TableName] [varchar] (128),
[Schema] [varchar] (128),
[ColumnOrder] [int],
[ColumnName] [varchar] (128),
[ColumnType] [varchar] (128),
[ColumnLength] [int] ,
[ColumnDescription] [varchar] (2000),
[RecordedDateTime] [datetime]
)
CREATE TABLE [dbo].[TableMetadataHistory] (
[ServerName] [varchar] (256),
[DBName] [varchar] (256),
[TableName] [varchar] (128),
[Schema] [varchar] (128),
[ColumnOrder] [int],
[ColumnName] [varchar] (128),
[ColumnType] [varchar] (128),
[ColumnLength] [int] ,
[ColumnDescription] [varchar] (2000),
[RecordedDateTime] [datetime]
)
************************************************************************************************/
IF EXISTS (
SELECT 1
FROM INFORMATION_SCHEMA.ROUTINES
WHERE SPECIFIC_NAME = N'GatherTableMetadata'
)
DROP PROCEDURE GatherTableMetadata
GO
CREATE PROCEDURE GatherTableMetadata
@DatabaseList VARCHAR(MAX),
@ExcludeSystemDatabases tinyint = 1
AS
SET NOCOUNT ON
/*****************************************
* Truncate holding tables
*****************************************/
IF EXISTS (SELECT 1
FROM dbo.TableMetadata)
BEGIN
INSERT INTO dbo.TableMetadataHistory
SELECT *
FROM TableMetadata;
TRUNCATE TABLE dbo.TableMetadata;
END
DECLARE @cmd VARCHAR(8000),
@table_name VARCHAR(128),
@servername VARCHAR(256),
@dbname VARCHAR(256),
@schemaname NVARCHAR(128),
@tablename NVARCHAR(128),
@recordeddatetime datetime
CREATE TABLE #dbnames
(
name NVARCHAR(128)
)
SET @recordeddatetime = GETDATE()
SET @servername = CAST(Serverproperty('servername') AS VARCHAR(256))
IF Upper(@DatabaseList) = 'ALL'
BEGIN
IF @ExcludeSystemDatabases = 1
BEGIN
SET @DatabaseList = '';
SELECT @DatabaseList = @DatabaseList + '''' + name + ''','
FROM MASTER.dbo.sysdatabases
WHERE name NOT IN ( 'master', 'msdb', 'model', 'pubs',
'northwind', 'tempdb' );
END
ELSE
BEGIN
SELECT @DatabaseList = @DatabaseList + '''' + name + ''','
FROM MASTER.dbo.sysdatabases;
END
SET @DatabaseList = LEFT(@DatabaseList, Len(@DatabaseList) - 2) + ''''
INSERT INTO #dbnames
EXEC('select name from master.dbo.sysdatabases where name in ('+@DatabaseList+')')
END
--found at http://mangalpardeshi.blogspot.com/2009/03/how-to-split-comma-delimited-string.html
;WITH Cte AS
(
select CAST('<M>' + REPLACE( @DatabaseList, ',' , '</M><M>') + '</M>' AS XML) AS DatabaseNames
)
insert into #dbnames
SELECT
Split.a.value('.', 'VARCHAR(100)') AS DatabaseNames
FROM Cte
CROSS APPLY DatabaseNames.nodes('/M') Split(a)
IF not exists(select 1 from #dbnames)
BEGIN
insert into #dbnames select @DatabaseList
END
DECLARE db CURSOR FAST_FORWARD FOR
SELECT name
FROM #dbnames
OPEN db
FETCH NEXT FROM db INTO @dbname
WHILE ( @@FETCH_STATUS <> -1 )
BEGIN
IF ( @@FETCH_STATUS <> -2 )
BEGIN
IF (SELECT CONVERT(SYSNAME, Databasepropertyex(@dbname, 'status'))
)
=
'ONLINE'
BEGIN
set @cmd ='
insert into dbo.TableMetadata
SELECT
'''+@servername+''' AS ''ServerName'',
'''+@dbname+''' AS ''DBName'',
o.name AS ''TableName'',
Object_schema_name(o.object_id,DB_ID('''+@dbname+''')) AS ''SchemaName'',
c.column_id AS ''ColumnOrder'',
c.name AS ''ColumnName'',
s.name AS ''ColumnType'',
c.max_length AS ''ColumnLength'',
Replace(Replace(Cast(e.value AS VARCHAR(2000)), Char(13), ''''), Char(11), '''') AS ''ColumnDescription'',
'''+convert(varchar,@recordeddatetime, 121)+'''
FROM
['+@dbname+'].sys.objects o
INNER JOIN ['+@dbname+'].sys.columns c
ON o.object_id = c.object_id
INNER JOIN ['+@dbname+'].sys.types s
ON c.system_type_id = s.system_type_id
LEFT JOIN ['+@dbname+'].sys.extended_properties e
ON c.object_id = e.major_id
AND c.column_id = e.minor_id
and
e.class = 1
WHERE
o.type = ''U''
ORDER BY
Object_schema_name(o.object_id,DB_ID('''+@dbname+''')),
o.object_id,
c.column_id'
EXEC(@cmd)
END
END
FETCH NEXT FROM db INTO @dbname
END
CLOSE db
DEALLOCATE db
DROP TABLE #dbnames
SET nocount OFF