首页 > 数据库 > SQL Server > 正文

列出SQL SERVER 所有表、字段定义,类型,长度,一个值等信息,并导出到Excel 中

2024-08-31 00:47:55
字体:
来源:转载
供稿:网友

set ansi_nulls off

go

set nocount on

go


 


 



 



 



 



set language 'simplified chinese'

go

declare @tbl nvarchar(200),@fld nvarchar(200),@sql nvarchar(4000),@maxlen int,@sample nvarchar(40)


 


 



 



 



 



select d.name tablename,a.name fieldname,b.name typename,a.length length,a.isnullable is_null into #t

from  syscolumns  a,  systypes b,sysobjects d 

where  a.xtype=b.xusertype  and  a.id=d.id  and  d.xtype='u'


 


 



 



 



 



declare read_cursor cursor

for select tablename,fieldname from #t


 


 



 



 



 



select top 1 '_tablename                     ' tablename,

            'fieldname                      ' fieldname,'typename             ' typename,

            'length' length,'is_null' is_null,

            'maxlenused' as maxlenused,'sample value          ' sample,

             'comment   ' comment into #tc from #t


 


 



 



 



 



open read_cursor


 


 



 



 



 



fetch next from read_cursor into @tbl,@fld

while (@@fetch_status <> -1)  --- failes

begin

       if (@@fetch_status <> -2) -- missing

       begin

              set @sql=n'set @maxlen=(select max(len(cast('[email protected]+' as nvarchar))) from '[email protected]+')'

              --print @sql

              exec sp_executesql @sql,n'@maxlen int output',@maxlen output

              --print @maxlen

              set @sql=n'set @sample=(select top 1 cast('[email protected]+' as nvarchar) from '[email protected]+' where len(cast('[email protected]+' as nvarchar))='+convert(nvarchar(5),@maxlen)+')'

              exec sp_executesql @sql,n'@sample varchar(30) output',@sample output

              --for quickly  

              --set @sql=n'set @sample=convert(varchar(20),(select top 1 '[email protected]+' from '+

                     [email protected]+' order by 1 desc ))' 

              print @sql

              print @sample

              print @tbl

              exec sp_executesql @sql,n'@sample nvarchar(30) output',@sample output

              insert into #tc select *,ltrim(isnull(@maxlen,0)) as maxlenused,

                     convert(nchar(20),ltrim(isnull(@sample,' '))) as sample,' ' comment from #t where [email protected] and [email protected]

       end

       fetch next from read_cursor into @tbl,@fld

end


 


 



 



 



 



close read_cursor

deallocate read_cursor

go


 


 



 



 



 



set ansi_nulls on

go

set nocount off

go

select count(*)  from #t

drop table #t

go


 


 



 



 



 



select count(*)-1  from #tc


 


 



 



 



 



select * into ##tx from #tc order by tablename

drop table #tc


 


 



 



 



 



--select * from ##tx


 


 



 



 



 



declare @db nvarchar(60),@sql nvarchar(3000)

set @db=db_name()

--请修改用户名和口令 导出到excel 中

set @sql='exec master.dbo.xp_cmdshell ''bcp ..dbo.##tx out c:/'[email protected]+'_exp.xls -w -c936 -

usa -psa '''

print @sql

exec(@sql)

go

drop table ##tx

go


收集最实用的网页特效代码!

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表