如何從SQL Server 中取得欄位說明
SQL Server 2000
你可以在企業管理器中增加欄位說明,也可以使用下面的代碼:EXEC sp_addextendedproperty
'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
如果你只關心某一張表,那麼上面的TSQL中的注釋部份對你就非常有協助。反過來就會給你所有表中的所有欄位。
如果你只需要所有有說明的表,你可以把out join 改成 inner joinSELECT
[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
和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
%>
作者: 陸島工作室