获取SQL Server表字段的各种属性实例代码

复制代码 代码如下:

— sql server 2000

select a.name as 字段名, case when exists

(select 1

from sysobjects

where xtype = ‘pk’ and parent_obj = a.id and name in

(select name

from sysindexes

where indid in

(select indid

from sysindexkeys

where id = a.id and colid = a.colid)))

then ‘1’ else ‘0’ end as 主键, case when columnproperty(a.id, a.name,

‘isidentity’) = 1 then ‘1’ else ‘0’ end as 标识, b.name as 类型,

a.length as 占用字节数, columnproperty(a.id, a.name, ‘precision’) as 长度,

a.xscale as 小数, a.isnullable as 可空, isnull(e.text, ”) as 默认值, isnull(g.[value],

”) as 字段说明

from syscolumns a left outer join

systypes b on a.xusertype = b.xusertype inner join

sysobjects d on a.id = d.id and d.xtype = ‘u’ and

d.name <> ‘dtproperties’ left outer join

syscomments e on a.cdefault = e.id left outer join

sysproperties g on a.id = g.id and a.colid = g.smallid left outer join

sysproperties f on d.id = f.id and f.smallid = 0

where (d.name = ‘表名称’)

–2。sql server 2005

select case when exists

(select 1

from sysobjects

where xtype = ‘pk’ and parent_obj = a.id and name in

(select name

from sysindexes

where indid in

(select indid

from sysindexkeys

where id = a.id and colid = a.colid))) then ‘1’ else ‘0’ end as ‘key’, case when columnproperty(a.id, a.name,

‘isidentity’) = 1 then ‘1’ else ‘0’ end as ‘identity’, a.name as colname, c.name as typename, a.length as ‘byte’, columnproperty(a.id, a.name,

‘precision’) as ‘length’, a.xscale, a.isnullable, isnull(e.text, ”) as ‘default’, isnull(p.value, ”) as ‘comment’

from sys.syscolumns as a inner join

sys.sysobjects as b on a.id = b.id inner join

sys.systypes as c on a.xtype = c.xtype left outer join

sys.syscomments as e on a.cdefault = e.id left outer join

sys.extended_properties as p on a.id = p.major_id and a.colid = p.minor_id

where (b.name = ‘keyfactory’) and (c.status <> ‘1’)

–b.name = ‘keyfactory’,’keyfactory’为你想要查找的数据表。

–2、sql server 2005

select case when exists

(select 1

from sysobjects

where xtype = ‘pk’ and parent_obj = a.id and name in

(select name

from sysindexes

where indid in

(select indid

from sysindexkeys

where id = a.id and colid = a.colid))) then ‘1’ else ‘0’ end as ‘key’, case when columnproperty(a.id, a.name,

‘isidentity’) = 1 then ‘1’ else ‘0’ end as ‘identity’, a.name as colname, c.name as typename, a.length as ‘byte’, columnproperty(a.id, a.name,

‘precision’) as ‘length’, a.xscale, a.isnullable, isnull(e.text, ”) as ‘default’, isnull(p.value, ”) as ‘comment’

from sys.syscolumns as a inner join

sys.sysobjects as b on a.id = b.id inner join

sys.systypes as c on a.xtype = c.xtype left outer join

sys.syscomments as e on a.cdefault = e.id left outer join

sys.extended_properties as p on a.id = p.major_id and a.colid = p.minor_id

where (b.name = ‘keyfactory’) and (c.status <> ‘1’)

–b.name = ‘keyfactory’,’keyfactory’为你想要查找的数据表。

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐