1 SELECT
2
3 TableName=CASE WHEN C.column_id=1 THEN O.name ELSE N'' END,
4
5 TableDesc=ISNULL(CASE WHEN C.column_id=1 THEN PTB.[value] END,N''),
6
7 Column_id=C.column_id,
8
9 ColumnName=C.name,
10
11 PrimaryKey=ISNULL(IDX.PrimaryKey,N''),
12
13 [IDENTITY]=CASE WHEN C.is_identity=1 THEN N'√'ELSE N'' END,
14
15 Computed=CASE WHEN C.is_computed=1 THEN N'√'ELSE N'' END,
16
17 Type=T.name,
18
19 Length=C.max_length,
20
21 Precision=C.precision,
22
23 Scale=C.scale,
24
25 NullAble=CASE WHEN C.is_nullable=1 THEN N'√'ELSE N'' END,
26
27 [Default]=ISNULL(D.definition,N''),
28
29 ColumnDesc=ISNULL(PFD.[value],N''),
30
31 IndexName=ISNULL(IDX.IndexName,N''),
32
33 IndexSort=ISNULL(IDX.Sort,N''),
34
35 Create_Date=O.Create_Date,
36
37 Modify_Date=O.Modify_date
38
39 FROM sys.columns C
40
41 INNER JOIN sys.objects O
42
43 ON C.[object_id]=O.[object_id]
44
45 AND O.type='U'
46
47 AND O.is_ms_shipped=0
48
49 INNER JOIN sys.types T
50
51 ON C.user_type_id=T.user_type_id
52
53 LEFT JOIN sys.default_constraints D
54
55 ON C.[object_id]=D.parent_object_id
56
57 AND C.column_id=D.parent_column_id
58
59 AND C.default_object_id=D.[object_id]
60
61 LEFT JOIN sys.extended_properties PFD
62
63 ON PFD.class=1
64
65 AND C.[object_id]=PFD.major_id
66
67 AND C.column_id=PFD.minor_id
68
69 -- AND PFD.name='Caption' -- 字段说明对应的描述名称(一个字段可以添加多个不同name的描述)
70
71 LEFT JOIN sys.extended_properties PTB
72
73 ON PTB.class=1
74
75 AND PTB.minor_id=0
76
77 AND C.[object_id]=PTB.major_id
78
79 -- AND PFD.name='Caption' -- 表说明对应的描述名称(一个表可以添加多个不同name的描述)
80 LEFT JOIN -- 索引及主键信息
81
82 (
83
84 SELECT
85
86 IDXC.[object_id],
87
88 IDXC.column_id,
89
90 Sort=CASE INDEXKEY_PROPERTY(IDXC.[object_id],IDXC.index_id,IDXC.index_column_id,'IsDescending')
91
92 WHEN 1 THEN 'DESC' WHEN 0 THEN 'ASC' ELSE '' END,
93
94 PrimaryKey=CASE WHEN IDX.is_primary_key=1 THEN N'√'ELSE N'' END,
95
96 IndexName=IDX.Name
97
98 FROM sys.indexes IDX
99
100 INNER JOIN sys.index_columns IDXC
101
102 ON IDX.[object_id]=IDXC.[object_id]
103
104 AND IDX.index_id=IDXC.index_id
105
106 LEFT JOIN sys.key_constraints KC
107
108 ON IDX.[object_id]=KC.[parent_object_id]
109
110 AND IDX.index_id=KC.unique_index_id
111
112 INNER JOIN -- 对于一个列包含多个索引的情况,只显示第1个索引信息
113
114 (
115
116 SELECT [object_id], Column_id, index_id=MIN(index_id)
117
118 FROM sys.index_columns
119
120 GROUP BY [object_id], Column_id
121
122 ) IDXCUQ
123
124 ON IDXC.[object_id]=IDXCUQ.[object_id]
125
126 AND IDXC.Column_id=IDXCUQ.Column_id
127
128 AND IDXC.index_id=IDXCUQ.index_id
129
130 ) IDX
131
132 ON C.[object_id]=IDX.[object_id]
133
134 AND C.column_id=IDX.column_id
135
136 --WHERE O.name=N'abc' -- 如果只查询指定表,加上此条件
2
3 TableName=CASE WHEN C.column_id=1 THEN O.name ELSE N'' END,
4
5 TableDesc=ISNULL(CASE WHEN C.column_id=1 THEN PTB.[value] END,N''),
6
7 Column_id=C.column_id,
8
9 ColumnName=C.name,
10
11 PrimaryKey=ISNULL(IDX.PrimaryKey,N''),
12
13 [IDENTITY]=CASE WHEN C.is_identity=1 THEN N'√'ELSE N'' END,
14
15 Computed=CASE WHEN C.is_computed=1 THEN N'√'ELSE N'' END,
16
17 Type=T.name,
18
19 Length=C.max_length,
20
21 Precision=C.precision,
22
23 Scale=C.scale,
24
25 NullAble=CASE WHEN C.is_nullable=1 THEN N'√'ELSE N'' END,
26
27 [Default]=ISNULL(D.definition,N''),
28
29 ColumnDesc=ISNULL(PFD.[value],N''),
30
31 IndexName=ISNULL(IDX.IndexName,N''),
32
33 IndexSort=ISNULL(IDX.Sort,N''),
34
35 Create_Date=O.Create_Date,
36
37 Modify_Date=O.Modify_date
38
39 FROM sys.columns C
40
41 INNER JOIN sys.objects O
42
43 ON C.[object_id]=O.[object_id]
44
45 AND O.type='U'
46
47 AND O.is_ms_shipped=0
48
49 INNER JOIN sys.types T
50
51 ON C.user_type_id=T.user_type_id
52
53 LEFT JOIN sys.default_constraints D
54
55 ON C.[object_id]=D.parent_object_id
56
57 AND C.column_id=D.parent_column_id
58
59 AND C.default_object_id=D.[object_id]
60
61 LEFT JOIN sys.extended_properties PFD
62
63 ON PFD.class=1
64
65 AND C.[object_id]=PFD.major_id
66
67 AND C.column_id=PFD.minor_id
68
69 -- AND PFD.name='Caption' -- 字段说明对应的描述名称(一个字段可以添加多个不同name的描述)
70
71 LEFT JOIN sys.extended_properties PTB
72
73 ON PTB.class=1
74
75 AND PTB.minor_id=0
76
77 AND C.[object_id]=PTB.major_id
78
79 -- AND PFD.name='Caption' -- 表说明对应的描述名称(一个表可以添加多个不同name的描述)
80 LEFT JOIN -- 索引及主键信息
81
82 (
83
84 SELECT
85
86 IDXC.[object_id],
87
88 IDXC.column_id,
89
90 Sort=CASE INDEXKEY_PROPERTY(IDXC.[object_id],IDXC.index_id,IDXC.index_column_id,'IsDescending')
91
92 WHEN 1 THEN 'DESC' WHEN 0 THEN 'ASC' ELSE '' END,
93
94 PrimaryKey=CASE WHEN IDX.is_primary_key=1 THEN N'√'ELSE N'' END,
95
96 IndexName=IDX.Name
97
98 FROM sys.indexes IDX
99
100 INNER JOIN sys.index_columns IDXC
101
102 ON IDX.[object_id]=IDXC.[object_id]
103
104 AND IDX.index_id=IDXC.index_id
105
106 LEFT JOIN sys.key_constraints KC
107
108 ON IDX.[object_id]=KC.[parent_object_id]
109
110 AND IDX.index_id=KC.unique_index_id
111
112 INNER JOIN -- 对于一个列包含多个索引的情况,只显示第1个索引信息
113
114 (
115
116 SELECT [object_id], Column_id, index_id=MIN(index_id)
117
118 FROM sys.index_columns
119
120 GROUP BY [object_id], Column_id
121
122 ) IDXCUQ
123
124 ON IDXC.[object_id]=IDXCUQ.[object_id]
125
126 AND IDXC.Column_id=IDXCUQ.Column_id
127
128 AND IDXC.index_id=IDXCUQ.index_id
129
130 ) IDX
131
132 ON C.[object_id]=IDX.[object_id]
133
134 AND C.column_id=IDX.column_id
135
136 --WHERE O.name=N'abc' -- 如果只查询指定表,加上此条件