日期:2014-05-18 浏览次数:20456 次
--如果从SQL数据库中,导出数据到Excel,如果Excel文件已经存在,而且已经按照要接收的数据创建好表头,就可以简单的用: insert into OPENROWSET('MICROSOFT.JET.OLEDB.4.0' ,'Excel 5.0;HDR=YES;DATABASE=c:\表1.xls',sheet1$) select * from 表1
use wsgj go declare @table_name varchar(50) --创建一个变量用来存储表名 declare cur cursor for select name from sysobjects where xtype='U' --创建游标并搜索所有表名 open cur fetch next from cur into @table_name --循环体 while @@fetch_status=0 begin EXEC ('insert into OPENROWSET("MICROSOFT.JET.OLEDB.4.0","Excel 5.0;HDR=YES;DATABASE=C:\[' + @table_name + '].xls",sheet1$) select * from '+@table_name) fetch next from cur into @table_name end close cur deallocate cur
EXEC ('insert into OPENROWSET(''MICROSOFT.JET.OLEDB.4.0'' ,''Excel 5.0;HDR=YES;DATABASE=C:\test\' + @table_name + '.xls'',sheet1$) select * from '+@table_name)