倒出数据库用户表结构和一样本数据到EXCEL中,供参考!

OpenVMS 2003-01-15 03:02:59
-- ======================================================
-- Export all user tables definition and one sample value
-- jan-13-2003,Dr.Zhang
-- ======================================================
SET ANSI_NULLS OFF
GO
SET NOCOUNT ON
GO


DECLARE @tbl varchar(100),@fld varchar(100),@sql nvarchar(4000),@maxlen int,@sample varchar(20)

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('+@fld+')) FROM '+@tbl+')'
--PRINT @sql
EXEC SP_EXECUTESQL @sql,N'@maxlen int OUTPUT',@maxlen OUTPUT
--print @maxlen
SET @sql=N'SET @sample=(SELECT TOP 1 '+@fld+' FROM '+@tbl+' WHERE len('+@fld+')='+convert(varchar(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 '+@fld+' FROM '+
--@tbl+' order by 1 desc ))'
--PRINT @sql
print @sample
EXEC SP_EXECUTESQL @sql,N'@sample varchar(30) OUTPUT',@sample OUTPUT
INSERT INTO #tc SELECT *,ltrim(ISNULL(@maxlen,0)) as MaxLenUsed,
convert(char(20),ltrim(ISNULL(@sample,' '))) as Sample,' ' Comment FROM #t where TableName=@tbl and FieldName=@fld
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 varchar(30),@sql varchar(3000)
set @db=db_name()
set @sql='exec master.dbo.xp_cmdshell ''bcp ..dbo.##tx out c:\'+@db+'_exp.xls -c -Usa -P '''
print @sql
exec(@sql)
GO
DROP TABLE ##tx
GO
...全文
61 40 打赏 收藏 转发到动态 举报
写回复
用AI写文章
40 条回复
切换为时间正序
请发表友善的回复…
发表回复
lizongqi 2003-05-02
  • 打赏
  • 举报
回复
up
linda1102 2003-04-08
  • 打赏
  • 举报
回复
up
98130 2003-04-07
  • 打赏
  • 举报
回复
UP
mymmsc 2003-03-20
  • 打赏
  • 举报
回复
学习
OpenVMS 2003-01-22
  • 打赏
  • 举报
回复
输出错误的原因主要在于临时表字段定义的长度,这点要注意!
结贴!
z5wjz 2003-01-20
  • 打赏
  • 举报
回复
这么长,保存起来慢慢看.
OpenVMS 2003-01-19
  • 打赏
  • 举报
回复
TO:xfei(zzz^^^) , -Usa -Pyourpass
如用SA,执行时需要设置SQL验证,否则用你的WINDOWS账号。
OpenVMS 2003-01-19
  • 打赏
  • 举报
回复
thanx yvqq(yvqq) ,真细心,
TEXT字段可以读,IMAGE不行,所以在输出时把IMAGE类型的过虑一下
xtype <> 34
xfei 2003-01-18
  • 打赏
  • 举报
回复
我的sa有密码,怎么改?
ansi 2003-01-18
  • 打赏
  • 举报
回复
收藏
CoolAbu 2003-01-17
  • 打赏
  • 举报
回复
混分无极限。。。


CoolAbu又到此混分 ;)


("`-''-/").___..--''"`-._
`6_ 6 ) `-. ( ).`-.__.`)
(_Y_.)' ._ ) `._ `. ``-..-'
_..`--'_..-_/ /--'_.' ,'
(il),-'' (li),' ((!.-'

CoolAbu 的形象大使,欢迎盗版!
Ouyangyifeiczy 2003-01-17
  • 打赏
  • 举报
回复
xx
qxm 2003-01-17
  • 打赏
  • 举报
回复
GZ
cjf0426 2003-01-17
  • 打赏
  • 举报
回复
直接用sqlServer的导出功能导成csv就可以了
mymmsc 2003-01-17
  • 打赏
  • 举报
回复
lai le
progame 2003-01-17
  • 打赏
  • 举报
回复

呵呵,我软件中是不是要集成这个导出结构到Excel呢?
http://www.csdn.net/cnshare/soft/15/15411.shtm

不过,我是用OpenShecm实现的,所以支持Access以及其它
如果加入这个东西很easy了

但好像这个生成结构的东西已经有人做了,我就算了:)

yvqq 2003-01-17
  • 打赏
  • 举报
回复
最新版本:解决了字符集与语言的问题:
系统在导出之前自动将语言设置成了简体中文,代码设置成了936。
全用wide type字符集:
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('+@fld+' as nvarchar))) FROM '+@tbl+')'
--PRINT @sql
EXEC SP_EXECUTESQL @sql,N'@maxlen int OUTPUT',@maxlen OUTPUT
--print @maxlen
SET @sql=N'SET @sample=(SELECT TOP 1 cast('+@fld+' as nvarchar) FROM '+@tbl+' WHERE len(cast('+@fld+' 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 '+@fld+' FROM '+
--@tbl+' 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 TableName=@tbl and FieldName=@fld
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()
set @sql='exec master.dbo.xp_cmdshell ''bcp ..dbo.##tx out c:\'+@db+'_exp.xls -w -C936 -Usa -P '''
print @sql
exec(@sql)
GO
DROP TABLE ##tx
GO
yvqq 2003-01-17
  • 打赏
  • 举报
回复
改正了所有数值型字段输出错误的问题:
目前还有text,imag等字段还不能输出!
SET ANSI_NULLS OFF
GO
SET NOCOUNT ON
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('+@fld+' as nvarchar))) FROM '+@tbl+')'
--PRINT @sql
EXEC SP_EXECUTESQL @sql,N'@maxlen int OUTPUT',@maxlen OUTPUT
--print @maxlen
SET @sql=N'SET @sample=(SELECT TOP 1 cast('+@fld+' as nvarchar) FROM '+@tbl+' WHERE len(cast('+@fld+' 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 '+@fld+' FROM '+
--@tbl+' 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 TableName=@tbl and FieldName=@fld
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()
set @sql='exec master.dbo.xp_cmdshell ''bcp ..dbo.##tx out c:\'+@db+'_exp.xls -c -Usa -P '''
print @sql
exec(@sql)
GO
DROP TABLE ##tx
GO
yvqq 2003-01-17
  • 打赏
  • 举报
回复
上面程序有很多错误,放到我的数据库中根本就不能执行。
一大堆的错误!
ben988211 2003-01-17
  • 打赏
  • 举报
回复
学习
加载更多回复(20)

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧