Sql操作Excel与Access

 

2009-08-03 23:49

      在各种数据库之间的数据交互,旧系统与新系统的数据同步,如果是常规数据数据库还好,万一是那些非专业的,比如(Excel ,Xml之类的)操作起来还真有点麻烦。以下将演示Excel 数据导入到 Access

1。Excel查询语句,注意执行时请保证目标文件已经关闭,不然会报错

select * from openrowset('microsoft.jet.oledb.4.0','excel 8.0;database=p:\test.xls;hdr=yes;imex=1','select * from [Sheet1$]')

2。执行是不是报错了?操作被拒绝了,执行以下存储过程开启吧,不用就了关闭

--开启
exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure
--关闭
exec sp_configure 'show advanced options',0
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',0
reconfigure

3.--访问Access

select Id,col_name,col_sex,col_major,col_Id,col_addr,col_money,col_award 
from OPENROWSET('Microsoft.Jet.OLEDB.4.0','G:\项目\AfterWork\StudentSystem\StudentSystem\bin\Debug\db_StudentSyste_2009.mdb';'admin';'',tbStudentInfo)

4.当操作过于复杂时,开启外部数据源的代码就显得有些麻烦了,用以下方法简化一下(测试的例子并不复杂,以作说明)

declare @Str varchar(8000)
set @Str=' OPENROWSET(''Microsoft.Jet.OLEDB.4.0'',''G:\项目\AfterWork\StudentSystem\StudentSystem\bin\Debug\db_StudentSyste_2009.mdb'';''admin'';'''',tbStudentInfo)'
set @Str='delete from ' +@Str +' where ID>1265'
exec (@Str)
print @Str

5.数据互导,要成功了

insert into OPENROWSET('Microsoft.Jet.OLEDB.4.0','M:\项目\AfterWork\StudentSystem\StudentSystem\bin\Debug\db_StudentSyste_2009.mdb';'admin';'',tbStudentInfo)
(col_time) 
select 序号 from 
openrowset('microsoft.jet.oledb.4.0','excel 8.0;database=p:\abc.xls;hdr=yes;imex=1','select * from [Sheet1$]')

6.这篇文章以后再修改一下,xml的Sql操作暂时还有遇到,有时间再补上。

整体过程没有什么困难的,要注意的是操作过程中Excel模板不能处于打开状态,否则操作将无法成功执行

posted @ 2009-08-05 23:58  -飛天蟲  阅读(443)  评论(0编辑  收藏  举报