Excel sql server 导入/导出数据

本文详细介绍如何使用SQLServer进行AdHocDistributedQueries配置,实现与Excel文件的数据导入导出操作。包括启用和禁用AdHocDistributedQueries及xp_cmdshell组件,通过SQL语句和BCP命令完成数据交换。

从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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值