配色: 字号:
Excel导入SQL资料
2013-01-03 | 阅:  转:  |  分享 
  
insertintoexceltosqltemp(t1,t2,t3,t4,t5)selectfromopenrowset(''Microsoft.Jet.OLEDB.4.0'',''Excel8.0;Database=D:\dagl2012\aaaa.xls'',''select档号,全宗号,文件编号,正题名,责任者from[sheet1$]'')



/selectintoexceltosqltemp1fromopenrowset(''Microsoft.Jet.OLEDB.4.0'',''Excel8.0;Database=D:\dagl2012\aaaa.xls'',[sheet1$])/





insertinto中openrowset的应用

不同服务器中不同的数据库之间数据交换(mysqltomssql):sqlInsert="insertintoHealthArchives0.dbo.YSProduct(ProductId,ProductName)select"&_"fromopenrowset(''msdasql'',''driver={mysqlodbc3.51driver};server=192.168.0.22;port=3306;database=ec;user=root;password=;stmt=setnamesgbk;option=2049'',''selectgoods_id,goods_namefromecs_goodsorderbygoods_idasclimit0,2'')"不同服务器中数据库之间数据交换(mssqltomssql):sqlInsert="insertintoHealthArchives0.dbo.YSProductselect"&_''"fromopenrowset(''SQLOLEDB'',''192.168.0.16'';''sa'';''password'',''selectid,provincefromHealthArchives0.dbo.a_Province'')"???上面俩个是批量导入

SQLSERVER使用OpenRowset,、OpenDataSource函数导入、导出数据到Excel的几种方法(整理)

一、如我在D盘下有D:\物件编码.xls文件,有工作表名Sheet是中文命名为‘办公用品编码’,‘零件编码’。

??????selectinto#tempfromopendatasource(''Microsoft.Jet.OLEDB.4.0'',''Excel8.0;Database=D:\物件编码.xls'')...[办公用品编码$]

??????selectinto#tempfromopendatasource(''Microsoft.Jet.OLEDB.4.0'',''Excel8.0;Database=D:\物件编码.xls'')...[零件编码$]

二、如你的Excel文件是工作表是默认的Sheet命名为Sheet1,Sheet2等。

?????selectinto#tempfromopendatasource(''Microsoft.Jet.OLEDB.4.0'',''Excel8.0;Database=D:\物件编码.xls'')...[Sheet1$]

?????selectinto#tempfromopendatasource(''Microsoft.Jet.OLEDB.4.0'',''Excel8.0;Database=D:\物件编码.xls'')...[Sheet2$]

三、引用Limpire(昨夜小楼)以下方法,也行

?????OpenRowSet和OpenDataSource都能用读取用数字命名的Sheet,只不过要加单引号界定,其它不规则命名的Sheet也一样。

假设C:\Text.xls有个Sheet名字是“3”:??????selectfromopendatasource(''Microsoft.Jet.OLEDB.4.0'',''Excel8.0;Database=C:\Test.xls'')...[''3$'']--ORselectfromopenrowset(''Microsoft.Jet.OLEDB.4.0'',''Excel8.0;Database=C:\Test.xls'',[''3$''])--ORselectfromopenrowset(''Microsoft.Jet.OLEDB.4.0'',''Excel8.0;Database=C:\Test.xls'',''selectfrom[''''3$'''']'')--OpenRowSet(,,''query'')可以不加单引号界定:selectfromopenrowset(''Microsoft.Jet.OLEDB.4.0'',''Excel8.0;Database=C:\Test.xls'',''selectfrom[3$]'')

四、本人的方法:

导出EXCEL时,对应字段数类型都要相同,EXCEL字段的名称stano,sname和数据库的要一样,才能导出成功。

insertintoopenrowset(''MICROSOFT.JET.OLEDB.4.0'',''excel8.0;HDR=YES;database=D:\FName.xls'',sheet1$)selectstano,snamefromstainfo

以上在SQLSERVER2000上测试均可以运行,是在本地查询分析器中调试。

献花(0)
+1
(本文系绿茶图书吧首藏)