skaleem1
asked on
Query the sys tables from SQL Server 2005
I want to query the following from the sys tables for all tables in a SQL Server 2005 Database:
Table name, column name, Datatype, Extended description
Can someone help me write the query?
ASKER
How can I get the extended description for each column?
SELECT C.TABLE_NAME,C.COLUMN_NAME , C.DATA_TYPE, COALESCE(CONVERT(VARCHAR(1 0),CHARACT ER_MAXIMUM _LENGTH), CONVERT(VARCHAR(5),NUMERIC _PRECISION )+','+CONV ERT(VARCHA R(5),NUMER IC_SCALE), CONVERT(VARCHAR(10),DATETI ME_PRECISI ON)) AS EXTDESC
FROM INFORMATION_SCHEMA.COLUMNS C
i didn't quite understand what you meant by extended description but i hope this helps.
FROM INFORMATION_SCHEMA.COLUMNS
i didn't quite understand what you meant by extended description but i hope this helps.
ASKER
By extended description I mean the custom description you can add for each column of a table when you go to the table design mode (enter the description in the lower half pane named Column properties - right hand side column, for the description property)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Select sys.objects.Name as Tablename, sys.columns.name as ColumnName, sys.systypes.name as DataType from sys.objects
inner join sys.columns on sys.objects.object_id = sys.columns.object_id
inner join sys.systypes on sys.columns.system_type_id = sys.systypes.xtype
where sys.objects.type = 'u'
inner join sys.columns on sys.objects.object_id = sys.columns.object_id
inner join sys.systypes on sys.columns.system_type_id
where sys.objects.type = 'u'
ASKER
This is what I came up with finally:
SELECT [Table] = OBJECT_NAME(c.object_id),
[Column] = c.name, t.name AS [DataType],
[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'
JOIN sys.types AS t ON c.user_type_id=t.user_type _id
WHERE
OBJECTPROPERTY(c.object_id , 'IsMsShipped')=0
SELECT [Table] = OBJECT_NAME(c.object_id),
[Column] = c.name, t.name AS [DataType],
[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'
JOIN sys.types AS t ON c.user_type_id=t.user_type
WHERE
OBJECTPROPERTY(c.object_id
ASKER
Thanks, yours is the one I finally utilized.
Please try following
select * From Information_Schema.Columns
if you need only Table info then use
select * From Information_Schema.Tables
Thanks