To import data from excel to DB

本文介绍了几种将Excel数据导入数据库的方法:使用OPENDATASOURCE、通过链接服务器、利用VBA脚本及SQL Server导入导出向导。还特别提到了在使用OPENDATASOURCE时可能遇到的安全配置问题及其解决办法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Here,I introduce some methods to import data from excel to DB,

1  By OPENDATASOURCE

SELECT
* FROM OPENDATASOURCE(
    'Microsoft.Jet.OLEDB.4.0',
    'Excel 8.0;DataBase=D:/TEST.xls')...[sheet1$]

Note: Sometimes, error message will occor when executing above script like this:
Msg 15281, Level 16, State 1, Line 1
SQL Server blocked access to STATEMENT 'OpenRowset/OpenDatasource' of component 'Ad Hoc Distributed Queries' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'Ad Hoc Distributed Queries' by using sp_configure. For more information about enabling 'Ad Hoc Distributed Queries', see "Surface Area Configuration" in SQL Server Books Online.

The solution issetting Ad Hoc Distributed Queries to 1,you can refer to my post: http://www.cnblogs.com/Burgess/archive/2008/09/24/1298195.html


2   By Linked server

EXEC sp_addlinkedserver  --add linked server
    @server = N'MyExcel',
    @srvproduct = N'Jet 4.0',
    @provider = N'Microsoft.Jet.OLEDB.4.0',
    @datasrc = N'd:/TEST.xls',
    @provstr = N'Excel 8.0'
GO

Note:You can also add linked server by below method:

exec sp_addlinkedsrvlogin 'MyExcel','false' --login without account (Optional)
go

select * from MyExcel...sheet1$  --query data
go

 

 3  By VBA

 

Code


 4   By SQL Server Import and Export Wizard
Detailed oprating steps is abbreviated here.

5  Other methods:
Please refer to http://support.microsoft.com/default.aspx/kb/321686

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值