问题
Possible Duplicate:
SQL Server: Extract Table Meta-Data (description, fields and their data types)
I just like to ask if there is a way to programmatically retrieve table and column descriptions from SQL Server 2008? These are the descriptions that you can enter when using the UI.
And if so, is there a way to programmatically update them as well?
Thanks.
回答1:
If you want to retrieve the "descriptions" you set using MSSMS's UI you need to query sys.extended_properties.
Start by doing select * from sys.extended_properties
and read some records in the value column to know what I mean (you'll find previously set descriptions there). You will later need to do some joins with sysobjects (I guess) in order to know which description belongs to which table and you'll be ready to go. Search in google for more info if you're confused =)
Also, try this:
select * from sys.extended_properties
inner join sysobjects on sys.extended_properties.major_id = sysobjects.id
The 'value' column and the second 'name' column (which belongs to sysobjects) are what you're looking for.
Hope that helps.
回答2:
To get the description data, you need to fiddle with sysobjects/syscolumns
SELECT u.name + '.' + t.name AS [table],
td.value AS [table_desc],
c.name AS [column],
cd.value AS [column_desc]
FROM sysobjects t
INNER JOIN sysusers u
ON u.uid = t.uid
LEFT OUTER JOIN sys.extended_properties td
ON td.major_id = t.id
AND td.minor_id = 0
AND td.name = 'MS_Description'
INNER JOIN syscolumns c
ON c.id = t.id
LEFT OUTER JOIN sys.extended_properties cd
ON cd.major_id = c.id
AND cd.minor_id = c.colid
AND cd.name = 'MS_Description'
WHERE t.type = 'u'
ORDER BY t.name, c.colorder
some info can be found out at
select * from INFORMATION_SCHEMA.TABLES
select * from INFORMATION_SCHEMA.COLUMNS
来源:https://stackoverflow.com/questions/11654576/sql-server-meta-data-table-and-column-descrption