方法一。。
直接看操作步骤
在数据库上点击右键,然后选择“任务”,选择“导入数据”,我们就看到弹出淡入数据的对话框
Excel 上面的字段命名最好跟要导入到最终的那个表的字段相同。假设终表为A表。组装好 如下图:
按照操作步骤走下去,最终会生成一个新的表(临时表B表)。
我们可以借助工具,MSSQL表数据导出成Insert语句的工具 即:将查询出来的这些数据都生成insert into语句。
最终在A表中执行该insert into语句就可以将excel中的数据最终放入数据库中。
方法二:
通过语句 直接导入
insert into hr_staff(uuid,dept,staff_name,gender,nation,birthday,political,native_city,identy,
position,pos_level,work_date,pos_begin,gradschool,professional,degree,education,remark)
select * from OpenRowSet('Microsoft.ACE.OLEDB.12.0', 'Excel 8.0;HDR=Yes;IMEX=1;Database=D:\13.xls', 'select * from [sheet1$]')
看excel的写法 excel上的字段要跟上面的insert into 中的字段一致
用这个语句会遇到一大堆问题,问题我们跟着提示一步步解决。
问题一:
OLE DB 访问接口 'Microsoft.Jet.OLEDB.4.0' 配置为在单线程单元模式下运行,所以该访问接口无法用于分布式查询
解决方法:
a.
执行以上sql语句的数据库必须是本地数据库,如果为远程的数据库就会报上面的错误。
b.
在64SQL Engine中已经不提供jet.oledb.4.0的驱动了
,
解决方法:下载一个ACE.Oledb.12.0 for X64位的驱动,并把连接字符串Microsoft.jet.Oledb.4.0 更改为 Microsoft.ACE.OLEDB.12.0
问题二:
--开启导入功能
exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure
--允许在进程中使用ACE.OLEDB.12
EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'AllowInProcess', 1
--允许动态参数
EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'DynamicParameters', 1
exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure
--允许在进程中使用ACE.OLEDB.12
EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'AllowInProcess', 1
--允许动态参数
EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'DynamicParameters', 1