汇出数据表字段(&属性)清单

以下语句可将SQL数据库中,所有的表及字段(属性:表说明、字段名、字段类型、字段顺序、长度、是否为空、字段说明等),汇出至一个临时表。以备用。

 

Select 1 as Type,A.Name as tName,'表名' as Col1,'中文标题' as Col2,'说明' as Col3,

'' as Col4,'' as Col5,'' as Col6,'' as Col7,'' as Col8,'' as Col9 Into #t

From SysObjects A Where A.xtype='U'

Union

Select 2 as Type,A.Name as tName,A.Name as Col1,null as Col2,C.Value as Col3,

'' as Col4,'' as Col5,'' as Col6,'' as Col7,'' as Col8,'' as Col9

From SysObjects A Left join sysproperties C on A.id=C.id and C.smallid=0 and C.Type=3

Where A.xtype='U'

Union

Select 3 as Type,A.Name as tName,'字段' as Col1,'中文标题' as Col2,'顺序' as Col3,

'字段类型' as Col4,'字段长度' as Col5,'允许为空' as Col6,'字段说明' as Col7,'自动增量' as Col8,

'主键' as Col9

From SysObjects A Where A.xtype='U'

Union

Select 4 as Type,A.Name as tName,B.Name as Col1,'' as Col2,Convert(Nvarchar,B.ColOrder) as Col3,

D.Name+Case When D.Name in ('Numeric','Decimal') then '('+Convert(Nvarchar,B.xprec)+','+Convert(Nvarchar,B.xscale)+')'

else '' end as Col4,Convert(Nvarchar,B.Length) as Col5,

Case When B.isnullable=1 then '' else '' end as Col6,

C.Value as Col7,Case When B.autoval is not null then '' else '' end as Col8,

Case When S.tName is null then '' else '' end as Col9

From SysObjects A Inner join SysColumns B on A.id=b.id

Left join sysproperties C on A.id=C.id and B.colid=C.smallid and C.type=4

Left join systypes D on B.xtype=D.xtype and B.xUserType=D.xusertype

Left join (Select A.Name as tName,B.Name as ColName

From SysObjects A Inner join SysColumns B on A.id=b.id

Inner join SysObjects D on A.id=D.Parent_Obj and D.Xtype='PK'

Inner join sysindexes C on A.id=C.id and D.Name=C.Name

Inner join sysindexkeys S on C.id=S.id and C.indid=S.indid and B.colid=S.colid

Where A.Xtype='U') S on A.Name=S.tName and B.Name=S.ColName

Where A.xtype='U'

Order By tName,Type,Col3

 

Select Type,tName,Col1,Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9 From #t Order By tName,Type,Case When Type=4 then Convert(int,Col3) else 0 End

 

drop table #t

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值