Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server: Extract Table Meta-Data (description, fields and their data types)

I am trying to find a way to extract information about my tables in SQL Server (2008).
The data I need needs to include the description of the table (filled from the Description property in the Properties Window), a list of fields of that table and their respective data types.

Is there any way I can extract such meta-data? I presume I have to use some sys sp but I'n not sure which one.

like image 789
Andreas Grech Avatar asked May 20 '09 11:05

Andreas Grech


People also ask

Which SQL command gives meta data of a table?

T-SQL DB_NAME() metadata function.

How do I get a list of fields from a table in SQL?

To get the column name of a table we use sp_help with the name of the object or table name. sp_columns returns all the column names of the object. The following query will return the table's column names: sp_columns @table_name = 'News'


1 Answers

To get the description data, you unfortunately have to use sysobjects/syscolumns to get the ids:

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 

You can do it with info-schema, but you'd have to concatenate etc to call OBJECT_ID() - so what would be the point?

like image 97
Marc Gravell Avatar answered Sep 19 '22 15:09

Marc Gravell