您的位置:首页 > 数据库

获取表的相关信息Sqlserver

2007-02-14 14:24 246 查看
SELECT tableName, colOrder, colName, IsIdentity, pKey, type, bits, length, digit, IsNullAble, defaultValue, memo,ltrim(rtrim(tableName))+'.'+ltrim(rtrim(colName)) as col from
(
SELECT
d.name as tableName,
a.colorder as colOrder,
a.name as colName,
(case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then 1 else 0 end) as IsIdentity,--0:不是,1:是
(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) pKey,--0:不是,1:是
b.name type,--数据类型
a.length bits,--字节数
COLUMNPROPERTY(a.id,a.name,'PRECISION') as length,--长度
isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0) as digit,--小数位数
(case when a.isnullable=1 then 0 else 1 end) as IsNullAble,--允许空0:允许,1:不允许
isnull(e.text,'') defaultValue,--默认值
isnull(g.[value],'') AS memo--字段说明
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 sysproperties g on a.id=g.id and a.colid = g.smallid
) as SystemInfo
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: