从excel导入数据到sql server
--启用Ad Hoc Distributed Queries:
exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure
SELECT * INTO tablename FROM OPENROWSET('MICROSOFT.JET.OLEDB.4.0',
'Excel 5.0;HDR=YES;DATABASE=C:/test.xls',Sheet1$)
--关闭Ad Hoc Distributed Queries:
exec sp_configure 'Ad Hoc Distributed Queries',0
reconfigure
exec sp_configure 'show advanced options',0
reconfigure
从sql server导出数据到excel
如果Excel文件已经存在,而且已经按照要接收的数据创建好表头:
insert into OPENROWSET('MICROSOFT.JET.OLEDB.4.0'
,'Excel 5.0;HDR=YES;DATABASE=c:test.xls',sheet1$)
select * from 表
如果Excel文件不存在,也可以用BCP来导成类Excel的文件:
exec master..xp_cmdshell 'bcp pubs.dbo.authors out c:/temp1.xls -c -q -S"HUAWEI-CECF7A04" -U"sa" -P"pwd"''
执行SQL语句导出:
exec master..xp_cmdshell 'bcp "SELECT au_fname FROM pubs.dbo.authors"queryout c:/temp1.xls -c -S"HUAWEI-CECF7A04" -U"sa" -P"pwd"'
==SQL Server 阻止了对组件 'xp_cmdshell' 的 过程'sys.xp_cmdshell' 的访问
exec sp_configure 'show advanced options', 1
reconfigure
exec sp_configure 'xp_cmdshell', 1
reconfigure
==SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问
exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure
导入导出大全:http://www.cnblogs.com/jybuding/archive/2009/07/14/1522975.html