挖土

Coding for fun.
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

SQL Server表结构打印

Posted on 2006-07-26 11:12  挖土.  阅读(934)  评论(0编辑  收藏  举报

declare @t table(表名 varchar(100),字段名 varchar(100),数据类型 varchar(100),长度 varchar(100),描述 sql_variant )
declare @table varchar(100),@counter int,@tablecount int,@talben varchar(100),@a varchar(100),@b varchar(100),@c varchar(100),@d varchar(100),@e varchar(100)
select @tablecount=count(name) from sysobjects where xtype='u'
declare tablen cursor for
select name from sysobjects where xtype ='U'
open tablen
set @counter=1
while @counter<@tablecount
begin
  fetch next from tablen
  into @talben
  insert @t SELECT  表名=case when a.colorder=1 then d.name else '' end,
字段名=a.name,
类型=b.name,
长度=COLUMNPROPERTY(a.id,a.name,'PRECISION'),
字段描述=isnull(g.[value],'')
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 
left join sysproperties f on d.id=f.id and f.smallid=0
  where d.id=object_id(@talben)
  order by a.id,a.colorder
  --insert @t select @talben

set @counter=@counter+1
end
close tablen
deallocate tablen
select * from @t
用前台控件显示再打印。