查看表字段的相关的系统信息

SELECT (case when a.colorder=1 then d.name else '' end) as tableName,--如果表名相同就返回空
a.name as fieldName,
(case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '1'else '0' end) as autoval,
(case when (SELECT count(*) FROM sysobjects--查询主键
WHERE (name in
(SELECT name FROM sysindexes
WHERE (id = a.id) AND (indid in
(SELECT indid FROM sysindexkeys
WHERE (id = a.id) AND (colid in
(SELECT colid FROM syscolumns
WHERE (id = a.id) AND (name = a.name))
)))))
AND (xtype = 'PK'))>0 then '1' else '0' end) as isKey,--查询主键END
b.name as FieldType,
COLUMNPROPERTY(a.id,a.name,'PRECISION') as fieldLength,
isnull(g.[value],'') AS Description
FROM syscolumns a left join systypes b
on a.xtype=b.xusertype
inner join sysobjects d
on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
left join syscomments e
on a.cdefault=e.id
left join sys.extended_properties g
on a.id=g.major_id AND a.colid = g.minor_id
where d.name='DI_TreeType' order by a.id,a.colorder





SELECT distinct c.column_name, c .data_type, c.is_nullable, tc.constraint_type, c.numeric_precision, c.numeric_scale, c.character_maximum_length, c.table_name, c.ordinal_position, tc.constraint_name,h.value as content
from information_schema.columns c
left outer join (
information_schema.constraint_column_usage ccu
join information_schema.table_constraints tc on (
tc.table_schema = ccu.table_schema
and tc.constraint_name = ccu.constraint_name
and tc.constraint_type <> 'CHECK'
)
) on (
c.table_schema = ccu.table_schema and ccu.table_schema = 'dbo'
and c.table_name = ccu.table_name
and c.column_name = ccu.column_name
)
left join (select a.name,value,d.name as tableName from syscolumns a left join systypes b
on a.xtype=b.xusertype
inner join sysobjects d
on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
left join syscomments e
on a.cdefault=e.id
left join sys.extended_properties g
on a.id=g.major_id AND a.colid = g.minor_id ) as h on c.COLUMN_NAME=h.name and c.TABLE_NAME=h.tableName

where c.table_name = 'DI_TreeType'
and c.table_schema ='dbo'
order by c.table_name, c.ordinal_position

posted @ 2012-10-18 09:31  易独  阅读(203)  评论(0编辑  收藏  举报
乐巴儿 一个有声音的公众号
长按,识别二维码,加关注