﻿{"id":287,"date":"2010-06-15T13:47:57","date_gmt":"2010-06-15T05:47:57","guid":{"rendered":"http:\/\/nick.workao.org\/?p=287"},"modified":"2010-06-15T13:47:57","modified_gmt":"2010-06-15T05:47:57","slug":"sql-server%e4%b8%8eaccess%e3%80%81excel%e7%9a%84%e6%95%b0%e6%8d%ae%e8%bd%ac%e6%8d%a2%e5%af%bc%e5%85%a5%e5%af%bc%e5%87%ba","status":"publish","type":"post","link":"http:\/\/nick.txtcc.com\/index.php\/win\/287","title":{"rendered":"SQL Server\u4e0eAccess\u3001Excel\u7684\u6570\u636e\u8f6c\u6362(\u5bfc\u5165\u5bfc\u51fa)"},"content":{"rendered":"<p>\u719f\u6089SQL SERVER  2000\u7684\u6570\u636e\u5e93\u7ba1\u7406\u5458\u90fd\u77e5\u9053\uff0c\u5176DTS\u53ef\u4ee5\u8fdb\u884c\u6570\u636e\u7684\u5bfc\u5165\u5bfc\u51fa\uff0c\u5176\u5b9e\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528Transact-SQL\u8bed\u53e5\u8fdb\u884c\u5bfc\u5165\u5bfc\u51fa\u64cd\u4f5c\u3002\u5728 Transact-SQL\u8bed\u53e5\u4e2d\uff0c\u6211\u4eec\u4e3b\u8981\u4f7f\u7528OpenDataSource\u51fd\u6570\u3001OPENROWSET  \u51fd\u6570\uff0c\u5173\u4e8e\u51fd\u6570\u7684\u8be6\u7ec6\u8bf4\u660e\uff0c\u8bf7\u53c2\u8003SQL\u8054\u673a\u5e2e\u52a9\u3002 \u5229\u7528\u4e0b\u8ff0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5341\u5206\u5bb9\u6613\u5730\u5b9e\u73b0SQL  SERVER\u3001ACCESS\u3001EXCEL\u6570\u636e\u8f6c\u6362\uff0c\u8be6\u7ec6\u8bf4\u660e\u5982\u4e0b\uff1a\n\u4e00\u3001SQL SERVER \u548cACCESS\u7684\u6570\u636e\u5bfc\u5165\u5bfc\u51fa\n\u5e38\u89c4\u7684\u6570\u636e\u5bfc\u5165\u5bfc\u51fa\uff1a\n\u4f7f\u7528DTS\u5411\u5bfc\u8fc1\u79fb\u4f60\u7684Access\u6570\u636e\u5230SQL Server\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u6b65\u9aa4:\n1\u5728SQL SERVER\u4f01\u4e1a\u7ba1\u7406\u5668\u4e2d\u7684Tools\uff08\u5de5\u5177\uff09\u83dc\u5355\u4e0a\uff0c\u9009\u62e9Data Transformation\n2Services\uff08\u6570\u636e\u8f6c\u6362\u670d\u52a1\uff09\uff0c\u7136\u540e\u9009\u62e9 czdImport Data\uff08\u5bfc\u5165\u6570\u636e\uff09\u3002\n3\u5728Choose a Data Source\uff08\u9009\u62e9\u6570\u636e\u6e90\uff09\u5bf9\u8bdd\u6846\u4e2d\u9009\u62e9Microsoft Access as the  Source\uff0c\u7136\u540e\u952e\u5165\u4f60\u7684.mdb\u6570\u636e\u5e93\uff08.mdb\u6587\u4ef6\u6269\u5c55\u540d\uff09\u7684\u6587\u4ef6\u540d\u6216\u901a\u8fc7\u6d4f\u89c8\u5bfb\u627e\u8be5\u6587\u4ef6\u3002\n4\u5728Choose a Destination\uff08\u9009\u62e9\u76ee\u6807\uff09\u5bf9\u8bdd\u6846\u4e2d\uff0c\u9009\u62e9Microsoft OLE\u3000DB Prov ider for  SQL\u3000Server\uff0c\u9009\u62e9\u6570\u636e\u5e93\u670d\u52a1\u5668\uff0c\u7136\u540e\u5355\u51fb\u5fc5\u8981\u7684\u9a8c\u8bc1\u65b9\u5f0f\u3002\n5\u5728Specify Table Copy\uff08\u6307\u5b9a\u8868\u683c\u590d\u5236\uff09\u6216Query\uff08\u67e5\u8be2\uff09\u5bf9\u8bdd\u6846\u4e2d\uff0c\u5355\u51fbCopy tables\uff08\u590d\u5236\u8868\u683c\uff09\u3002\n6\u5728Select Source Tables\uff08\u9009\u62e9\u6e90\u8868\u683c\uff09\u5bf9\u8bdd\u6846\u4e2d\uff0c\u5355\u51fbSelect All\uff08\u5168\u90e8\u9009\u5b9a\uff09\u3002\u4e0b\u4e00\u6b65\uff0c\u5b8c\u6210\u3002\nTransact-SQL\u8bed\u53e5\u8fdb\u884c\u5bfc\u5165\u5bfc\u51fa\uff1a<\/p>\n<ol>\n<li>\u5728SQL SERVER\u91cc\u67e5\u8be2access\u6570\u636e:\nSelect * FROM OpenDataSource\uff08 &#8216;Microsoft.Jet.OLEDB.4.0&#8217;,&#8217;Data  Source=&#8221;c:\\DB.mdb&#8221;;User ID=Admin;Password=&#8217;\uff09&#8230;\u8868\u540d<\/li>\n<li>\u5c06access\u5bfc\u5165SQL server\n\u5728SQL SERVER \u91cc\u8fd0\u884c:\nSelect * INTO newtable FROM OPENDATASOURCE \uff08&#8217;Microsoft.Jet.OLEDB.4.0&#8242;,\n&#8216;Data Source=&#8221;c:\\DB.mdb&#8221;;User ID=Admin;Password=&#8217; \uff09&#8230;\u8868\u540d<\/li>\n<li>\u5c06SQL SERVER\u8868\u91cc\u7684\u6570\u636e\u63d2\u5165\u5230Access\u8868\u4e2d\n\u5728SQL SERVER \u91cc\u8fd0\u884c\uff1a\ninsert into OpenDataSource\uff08 &#8216;Microsoft.Jet.OLEDB.4.0&#8217;, &#8216;Data Source=&#8221;  c:\\DB.mdb&#8221;;User ID=Admin;Password=&#8217;\uff09&#8230;\u8868\u540d\n\uff08\u5217\u540d1,\u5217\u540d2\uff09\nselect \u5217\u540d1,\u5217\u540d2 from sql\u8868<\/li>\n<\/ol>\n<p>\u5b9e\u4f8b\uff1a\ninsert into OPENROWSET\uff08&#8217;Microsoft.Jet.OLEDB.4.0&#8242;,  &#8216;C:\\db.mdb&#8217;;&#8217;admin&#8217;;&#8221;, Test\uff09select id,name from Test Insert INTO  OPENROWSET\uff08&#8217;Microsoft.Jet.OLEDB.4.0&#8242;, &#8216;c:\\trade.mdb&#8217;; &#8216;admin&#8217;; &#8221;, \u8868\u540d\uff09\nSelect * FROM sqltablename<\/p>\n<p>\u4e8c\u3001 SQL SERVER \u548cEXCEL\u7684\u6570\u636e\u5bfc\u5165\u5bfc\u51fa\n1\u3001\u5728SQL SERVER\u91cc\u67e5\u8be2Excel\u6570\u636e:\nSelect * FROM OpenDataSource(&#8216;Microsoft.Jet.OLEDB.4.0&#8217;,&#8217;Data  Source=&#8221;D:\\xiangqu\\abc.xls&#8221;;User ID=Admin;Password=;Extended  properties=&#8221;Excel 8.0;HDR=Yes;IMEX=1;&#8221;&#8216;)&#8230;[\u552f\u4e00\u6599\u4ef6$]<\/p>\n<p>\u6ce8\uff1a\n1.\u8fd9\u6761\u8bed\u53e5\u662f\u5728SQLServer\u67e5\u8be2\u5206\u6790\u5668\u4e2d\u6267\u884c\uff0c\u5e76\u4e14\u8981\u9009\u62e9\u597d\u6570\u636e\u5e93\uff0c\u5426\u5219\u4f1a\u628a\u8981\u5bfc\u5165\u7684\u6570\u636e\u5f80\u522b\u7684\u6570\u636e\u5e93\u4e2d\u5bfc\u4e86\u3002\n2.Table08\u662f\u6570\u636e\u5bfc\u5165\u540e\u5728SQLServer\u4e2d\u7684\u8868\u540d\uff0c\u5c5e\u4e8e\u65b0\u5efa\uff0c\u6240\u4ee5\u8bf7\u786e\u8ba4\u5728\u5bfc\u5165\u6570\u636e\u524d\u6570\u636e\u5e93\u4e2d\u6ca1\u6709\u8be5\u8868\u540d\uff0c\u5426\u5219\u4f1a\u63d0\u793a\u5df2\u5b58\u5728\u540c\u4e00\u8868\u540d\u3002\n3.Data Source\uff0c\u4e0d\u8981\u8fde\u5728\u4e00\u8d77\u5199\uff0c\u4e2d\u95f4\u6709\u4e00\u7a7a\u683c\u3002\n4.E:\\1.xls\uff0c\u4e3aExcel\u6240\u5728\u7684\u7edd\u5bf9\u8def\u5f84\u548c\u6570\u636e\u5e93\u540d\u3002\n5.Excel 5.0\uff0c\u6839\u636e\u4e0d\u540c\u7684Excel\u7248\u672c\u51995.0\u62168.0\u6216\u5176\u5b83\u3002\n6.IMEX=1\uff0c\u662f\u8f6c\u6362\u6210\u6587\u672c\u8f93\u5165\u7684\u610f\u601d\uff0c\u975e\u5e38\u91cd\u8981\uff0c\u5982\u679c\u6ca1\u6709\uff0c\u5c31\u8ddf\u4f60\u76f4\u63a5\u5bfc\u5165\u6548\u679c\u4e00\u6837\u3002\n7.Sheet1\u662f\u8868\u540d\uff0c\u5343\u4e07\u522b\u770b\u5230\u8bed\u53e5\u4e2d\u6709$\u5c31\u5728\u8868\u540d\u540e\u52a0\u4e0a$\uff0c\u56e0\u4e3a$\u662f\u8bed\u53e5\u8981\u52a0\u7684\uff0c\u522b\u753b\u86c7\u6dfb\u8db3\u3002\n\u4e0b\u9762\u662f\u4e2a\u67e5\u8be2\u7684\u793a\u4f8b\uff0c\u5b83\u901a\u8fc7\u7528\u4e8e Jet \u7684 OLE DB \u63d0\u4f9b\u7a0b\u5e8f\u67e5\u8be2 Excel \u7535\u5b50\u8868\u683c\u3002\nSelect <em> FROM OpenDataSource \uff08 &#8216;Microsoft.Jet.OLEDB.4.0&#8217;,&#8217;Data  Source=&#8221;c:\\Finance\\account.xls&#8221;;User ID=Admin;Password=;Extended  properties=Excel 5.0&#8217;\uff09&#8230;xactions\n2\u3001\u5c06Excel\u7684\u6570\u636e\u5bfc\u5165SQL server :\nSelect <\/em> into newtable FROM OpenDataSource\uff08  &#8216;Microsoft.Jet.OLEDB.4.0&#8217;,&#8217;Data Source=&#8221;c:\\book1.xls&#8221;;User  ID=Admin;Password=;Extended properties=Excel 5.0&#8217;\uff09&#8230;[Sheet1$]\n\u5b9e\u4f8b:\nSelect * into newtable FROM OpenDataSource\uff08  &#8216;Microsoft.Jet.OLEDB.4.0&#8217;,&#8217;Data Source=&#8221;c:\\Finance\\account.xls&#8221;;User  ID=Admin;Password=;Extended properties=Excel 5.0&#8217;\uff09&#8230;xactions<\/p>\n<p>3\u3001\u5c06SQL SERVER\u4e2d\u67e5\u8be2\u5230\u7684\u6570\u636e\u5bfc\u6210\u4e00\u4e2aExcel\u6587\u4ef6\nT-SQL\u4ee3\u7801\uff1a\nEXEC master..xp_cmdshell &#8216;bcp \u5e93\u540d.dbo.\u8868\u540dout c:\\Temp.xls -c -q  -S&#8221;servername&#8221; -U&#8221;sa&#8221; -P&#8221;&#8221;&#8216;\n\u53c2\u6570\uff1aS \u662fSQL\u670d\u52a1\u5668\u540d\uff1bU\u662f\u7528\u6237\uff1bP\u662f\u5bc6\u7801\n\u8bf4\u660e\uff1a\u8fd8\u53ef\u4ee5\u5bfc\u51fa\u6587\u672c\u6587\u4ef6\u7b49\u591a\u79cd\u683c\u5f0f\n\u5b9e\u4f8b:EXEC master..xp_cmdshell &#8216;bcp saletesttmp.dbo.CusAccount out  c:\\temp1.xls -c -q -S&#8221;pmserver&#8221; -U&#8221;sa&#8221; -P&#8221;sa&#8221;&#8216;\nEXEC master..xp_cmdshell &#8216;bcp &#8220;Select au_fname, au_lname FROM  pubs..authors orDER BY au_lname&#8221; queryout C:\\ authors.xls -c  -Sservername -Usa -Ppassword&#8217;\n\u5728VB6\u4e2d\u5e94\u7528ADO\u5bfc\u51faEXCEL\u6587\u4ef6\u4ee3\u7801\uff1a\nDim cn As New ADODB.Connection\ncn.open &#8220;Driver={SQL  Server};Server=WEBSVR;DataBase=WebMis;UID=sa;WD=123;&#8221;cn.execute  &#8220;master..xp_cmdshell &#8216;bcp &#8220;Select col1, col2 FROM \u5e93\u540d.dbo.\u8868\u540d&#8221; queryout  E:\\DT.xls -c -Sservername -Usa -Ppassword'&#8221;<\/p>\n<p>4\u3001\u5728SQL SERVER\u91cc\u5f80Excel\u63d2\u5165\u6570\u636e:\ninsert into OpenDataSource\uff08 &#8216;Microsoft.Jet.OLEDB.4.0&#8242;,&#8217;Data  Source=&#8221;c:\\Temp.xls&#8221;;User ID=Admin;Password=;Extended properties=Excel  5.0&#8217;\uff09&#8230;table1 \uff08A1,A2,A3\uff09 values \uff081,2,3\uff09\nT-SQL\u4ee3\u7801\uff1a\nInsert INTO OPENDATASOURCE\uff08&#8217;Microsoft.JET.OLEDB.4.0&#8217;,\n&#8216;Extended Properties=Excel 8.0;Data  source=C:\\training\\inventur.xls&#8217;\uff09&#8230;[Filiale1$]\n\uff08bestand, produkt\uff09 VALUES \uff0820, &#8216;Test&#8217;\uff09<\/p>\n<p>\u603b\u7ed3\uff1a\u5229\u7528\u4ee5\u4e0a\u8bed\u53e5\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06SQL  SERVER\u3001ACCESS\u548cEXCEL\u7535\u5b50\u8868\u683c\u8f6f\u4ef6\u4e2d\u7684\u6570\u636e\u8fdb\u884c\u8f6c\u6362\uff0c\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u6781\u5927\u65b9\u4fbf\uff01<\/p>\n<p>PS:SQL Server 2005 \u4f1a\u9047\u5230\u7684\u95ee\u9898:<\/p>\n<p>SQL Server blocked access to STATEMENT &#8216;OpenRowset\/OpenDatasource&#8217; of  component &#8216;Ad Hoc Distributed Queries&#8217; because this component is turned  off as part of the security configuration for this server. A system  administrator can enable the use of &#8216;Ad Hoc Distributed Queries&#8217; by  using sp_configure. For more information about enabling &#8216;Ad Hoc  Distributed Queries&#8217;, see &#8220;Surface Area Configuration&#8221; in SQL Server  Books Online. \u5f02\u5e38,<\/p>\n<p>&#8211;\u89e3\u6c7a\u8fa6\u6cd5\uff1a\n&#8211;\u4f7f\u7528\u524d: (\u958b\u555fAd Hoc Distributed Queries)\nEXEC sp_configure &#8216;show advanced options&#8217;, 1\nGO\nRECONFIGURE\nGO\nEXEC sp_configure &#8216;Ad Hoc Distributed Queries&#8217;, 1\nGO\nRECONFIGURE\nGO\nSelect * INTO usertable FROM\nOPENDATASOURCE (&#8216;Microsoft.Jet.OLEDB.4.0&#8217;,\n&#8216;Data Source=&#8221;D:\\DB.mdb&#8221;;User ID=;Password=&#8217; )\n&#8230;usertable\n&#8211;\u4f7f\u7528\u5f8c: (\u95dc\u9589Ad Hoc Distributed Queries)\nexec sp_configure &#8216;Ad Hoc Distributed Queries&#8217;,0\nreconfigure\nexec sp_configure &#8216;show advanced options&#8217;,0\nreconfigure<\/p>","protected":false},"excerpt":{"rendered":"<p>\u719f\u6089SQL SERVER 2000\u7684\u6570\u636e\u5e93\u7ba1\u7406\u5458\u90fd\u77e5\u9053\uff0c\u5176DTS\u53ef\u4ee5\u8fdb\u884c\u6570\u636e\u7684\u5bfc\u5165\u5bfc\u51fa\uff0c\u5176\u5b9e\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528Transact-SQL\u8bed\u53e5\u8fdb\u884c\u5bfc\u5165\u5bfc\u51fa\u64cd\u4f5c\u3002\u5728 Transact-SQL\u8bed\u53e5\u4e2d\uff0c\u6211\u4eec\u4e3b\u8981\u4f7f&#46;&#46;&#46;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[122,102,103],"class_list":["post-287","post","type-post","status-publish","format-standard","hentry","category-win","tag-access","tag-excel","tag-sql-server"],"_links":{"self":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/287","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/comments?post=287"}],"version-history":[{"count":2,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/287\/revisions"}],"predecessor-version":[{"id":289,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/287\/revisions\/289"}],"wp:attachment":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/media?parent=287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/categories?post=287"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/tags?post=287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}