如何从SQL Server 中取得字段说明
SQL Server 2000
你可以在企业管理器中增加字段说明,也可以使用下面的代码:
如果你只关心某一张表,那么上面的TSQL中的注释部份对你就非常有帮助。反过来就会给你所有表中的所有字段。
如果你只需要所有有说明的表,你可以把out join 改成 inner join
SQL Server 2005
在SQL Server 2005 中 sysproperties 表已被废弃,所以上面的代码都不能用。幸运的是他们还是增加了一个系统表给我们 sys.extended_properties,这张表和 sysproperties基本上相似。
和SQL Server 2000一样,你可以使用注释部份来返回某一张表。
Microsoft Access
在Access中,你可以使用下面的ASP代码来得到某一个字段的说明
SQL Server 2000
你可以在企业管理器中增加字段说明,也可以使用下面的代码:
EXEC sp_addextendedproperty
'MS_Description',
'some description',
'user',
dbo,
'table',
table_name,
'column',
column_name
现在,你就可以得到通过下面的代码得到字段说明:'MS_Description',
'some description',
'user',
dbo,
'table',
table_name,
'column',
column_name
SELECT
[Table Name] = i_s.TABLE_NAME,
[Column Name] = i_s.COLUMN_NAME,
[Description] = s.value
FROM
INFORMATION_SCHEMA.COLUMNS i_s
LEFT OUTER JOIN
sysproperties s
ON
s.id = OBJECT_ID(i_s.TABLE_SCHEMA+'.'+i_s.TABLE_NAME)
AND s.smallid = i_s.ORDINAL_POSITION
AND s.name = 'MS_Description'
WHERE
OBJECTPROPERTY(OBJECT_ID(i_s.TABLE_SCHEMA+'.'+i_s.TABLE_NAME), 'IsMsShipped')=0
-- AND i_s.TABLE_NAME = 'table_name'
ORDER BY
i_s.TABLE_NAME, i_s.ORDINAL_POSITION
[Table Name] = i_s.TABLE_NAME,
[Column Name] = i_s.COLUMN_NAME,
[Description] = s.value
FROM
INFORMATION_SCHEMA.COLUMNS i_s
LEFT OUTER JOIN
sysproperties s
ON
s.id = OBJECT_ID(i_s.TABLE_SCHEMA+'.'+i_s.TABLE_NAME)
AND s.smallid = i_s.ORDINAL_POSITION
AND s.name = 'MS_Description'
WHERE
OBJECTPROPERTY(OBJECT_ID(i_s.TABLE_SCHEMA+'.'+i_s.TABLE_NAME), 'IsMsShipped')=0
-- AND i_s.TABLE_NAME = 'table_name'
ORDER BY
i_s.TABLE_NAME, i_s.ORDINAL_POSITION
如果你只关心某一张表,那么上面的TSQL中的注释部份对你就非常有帮助。反过来就会给你所有表中的所有字段。
如果你只需要所有有说明的表,你可以把out join 改成 inner join
SELECT
[Table Name] = i_s.TABLE_NAME,
[Column Name] = i_s.COLUMN_NAME,
[Description] = s.value
FROM
INFORMATION_SCHEMA.COLUMNS i_s
INNER JOIN
sysproperties s
ON
s.id = OBJECT_ID(i_s.TABLE_SCHEMA+'.'+i_s.TABLE_NAME)
AND s.smallid = i_s.ORDINAL_POSITION
AND s.name = 'MS_Description'
WHERE
OBJECTPROPERTY(OBJECT_ID(i_s.TABLE_SCHEMA+'.'+i_s.TABLE_NAME), 'IsMsShipped')=0
ORDER BY
i_s.TABLE_NAME, i_s.ORDINAL_POSITION
[Table Name] = i_s.TABLE_NAME,
[Column Name] = i_s.COLUMN_NAME,
[Description] = s.value
FROM
INFORMATION_SCHEMA.COLUMNS i_s
INNER JOIN
sysproperties s
ON
s.id = OBJECT_ID(i_s.TABLE_SCHEMA+'.'+i_s.TABLE_NAME)
AND s.smallid = i_s.ORDINAL_POSITION
AND s.name = 'MS_Description'
WHERE
OBJECTPROPERTY(OBJECT_ID(i_s.TABLE_SCHEMA+'.'+i_s.TABLE_NAME), 'IsMsShipped')=0
ORDER BY
i_s.TABLE_NAME, i_s.ORDINAL_POSITION
SQL Server 2005
在SQL Server 2005 中 sysproperties 表已被废弃,所以上面的代码都不能用。幸运的是他们还是增加了一个系统表给我们 sys.extended_properties,这张表和 sysproperties基本上相似。
SELECT
[Table Name] = OBJECT_NAME(c.object_id),
[Column Name] = c.name,
[Description] = ex.value
FROM
sys.columns c
LEFT OUTER JOIN
sys.extended_properties ex
ON
ex.major_id = c.object_id
AND ex.minor_id = c.column_id
AND ex.name = 'MS_Description'
WHERE
OBJECTPROPERTY(c.object_id, 'IsMsShipped')=0
-- AND OBJECT_NAME(c.object_id) = 'your_table'
ORDER
BY OBJECT_NAME(c.object_id), c.column_id
[Table Name] = OBJECT_NAME(c.object_id),
[Column Name] = c.name,
[Description] = ex.value
FROM
sys.columns c
LEFT OUTER JOIN
sys.extended_properties ex
ON
ex.major_id = c.object_id
AND ex.minor_id = c.column_id
AND ex.name = 'MS_Description'
WHERE
OBJECTPROPERTY(c.object_id, 'IsMsShipped')=0
-- AND OBJECT_NAME(c.object_id) = 'your_table'
ORDER
BY OBJECT_NAME(c.object_id), c.column_id
和SQL Server 2000一样,你可以使用注释部份来返回某一张表。
Microsoft Access
在Access中,你可以使用下面的ASP代码来得到某一个字段的说明
<%
on error resume next
Set Catalog = CreateObject("ADOX.Catalog")
Catalog.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=<path>\<file>.mdb"
dsc = Catalog.Tables("table_name").Columns("column_name").Properties("Description").Value
if err.number <> 0 then
Response.Write "<" & err.description & ">"
else
Response.Write "Description = " & dsc
end if
Set Catalog = nothing
%>
on error resume next
Set Catalog = CreateObject("ADOX.Catalog")
Catalog.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=<path>\<file>.mdb"
dsc = Catalog.Tables("table_name").Columns("column_name").Properties("Description").Value
if err.number <> 0 then
Response.Write "<" & err.description & ">"
else
Response.Write "Description = " & dsc
end if
Set Catalog = nothing
%>