数学建模社区-数学中国

标题: SQL SERVER、ACCESS和EXCEL电子表格软件中的数据进行转换 [打印本页]

作者: lsan2005lsan    时间: 2005-8-8 14:29
标题: SQL SERVER、ACCESS和EXCEL电子表格软件中的数据进行转换
< ><FONT face="Times New Roman">  </FONT>熟悉<FONT face="Times New Roman">SQL SERVER 2000</FONT>的数据库管理员都知道,其<FONT face="Times New Roman">DTS</FONT>可以进行数据的导入导出,其实,我们也可以使用<FONT face="Times New Roman">Transact-SQL</FONT>语句进行导入导出操作。在<FONT face="Times New Roman"> Transact-SQL</FONT>语句中,我们主要使用<FONT face="Times New Roman">OpenDataSource</FONT>函数、<FONT face="Times New Roman">OPENROWSET </FONT>函数,关于函数的详细说明,请参考<FONT face="Times New Roman">SQL</FONT>联机帮助。利用下述方法,可以十分容易地实现<FONT face="Times New Roman">SQL SERVER</FONT>、<FONT face="Times New Roman">ACCESS</FONT>、<FONT face="Times New Roman">EXCEL</FONT>数据转换,详细说明如下:<FONT face="Times New Roman"> </FONT></P>  W8 h6 T) i! m
< ><FONT face="Times New Roman"> <p></p></FONT></P>8 F: V* i) T. }* L' Q) T) b% O. G- D
< ><FONT face="Times New Roman">    </FONT>一、<FONT face="Times New Roman">SQL SERVER </FONT>和<FONT face="Times New Roman">ACCESS</FONT>的数据导入导出</P>
* ?7 l0 j2 M6 i, Y! D6 ~5 I< ><FONT face="Times New Roman"> <p></p></FONT></P>
& ?2 Y0 Z9 O- F  f1 i< ><FONT face="Times New Roman">    </FONT>常规的数据导入导出:使用<FONT face="Times New Roman">DTS</FONT>向导迁移你的<FONT face="Times New Roman">Access</FONT>数据到<FONT face="Times New Roman">SQL Server</FONT>,你可以使用这些步骤:</P>, Q0 i* G, v2 e, P' }
< ><FONT face="Times New Roman"> <p></p></FONT></P>' F% q2 O0 R( E
< ><FONT face="Times New Roman">    1</FONT>在<FONT face="Times New Roman">SQL SERVER</FONT>企业管理器中的<FONT face="Times New Roman">Tools</FONT>(工具)菜单上,选择<FONT face="Times New Roman">Data Transformation</FONT></P>8 z4 w; M. b) W2 |: n4 p
< ><FONT face="Times New Roman"> <p></p></FONT></P>
) D8 \- h" }) M0 a) Z) t< ><FONT face="Times New Roman">    2Services</FONT>(数据转换服务),然后选择<FONT face="Times New Roman"> czdImport Data</FONT>(导入数据)。</P>8 B0 Y9 U' B3 b, H) S
< ><FONT face="Times New Roman"> <p></p></FONT></P>6 s. _5 @3 X- T. ^. w
< ><FONT face="Times New Roman">    3</FONT>在<FONT face="Times New Roman">Choose a Data Source</FONT>(选择数据源)对话框中选择<FONT face="Times New Roman">Microsoft Access as the Source</FONT>,然后键入你的。<FONT face="Times New Roman">mdb</FONT>数据库(。<FONT face="Times New Roman">mdb</FONT>文件扩展名)的文件名或通过浏览寻找该文件。</P>
( s2 |+ Y" f8 T  }: h6 I< ><FONT face="Times New Roman"> <p></p></FONT></P>
8 J+ k  @0 \1 v" ?< ><FONT face="Times New Roman">    4</FONT>在<FONT face="Times New Roman">Choose a Destination</FONT>(选择目标)对话框中,选择<FONT face="Times New Roman">Microsoft OLE</FONT> <FONT face="Times New Roman">DB Prov ider for SQL</FONT> <FONT face="Times New Roman">Server</FONT>,选择数据库服务器,然后单击必要的验证方式。</P>; K" g' `: ]: g/ x1 }7 S
< ><FONT face="Times New Roman"> <p></p></FONT></P>
" N: Z3 c1 N# i; q$ N; B8 r8 p< ><FONT face="Times New Roman">    5</FONT>在<FONT face="Times New Roman">Specify Table Copy</FONT>(指定表格复制)或<FONT face="Times New Roman">Query</FONT>(查询)对话框中,单击<FONT face="Times New Roman">Copy tables</FONT>(复制表格)。</P>
2 K$ j2 P  ?1 I3 F+ D- H; g< ><FONT face="Times New Roman"> <p></p></FONT></P>
- Z6 u7 X  [: X8 E; m: S* a+ }< ><FONT face="Times New Roman">    6</FONT>在<FONT face="Times New Roman">Select Source Tables</FONT>(选择源表格)对话框中,单击<FONT face="Times New Roman">Select All</FONT>(全部选定)。下一步,完成。</P>
; Y/ {% z" B( X< ><FONT face="Times New Roman"> <p></p></FONT></P>3 y2 F( u! V2 L
< ><FONT face="Times New Roman">    Transact-SQL</FONT>语句进行导入导出:</P>
2 \. T% H; A1 d, n9 r, e, H< ><FONT face="Times New Roman"> <p></p></FONT></P>* E$ G; Y# ^8 Q; Y
< ><FONT face="Times New Roman">    1.</FONT>在<FONT face="Times New Roman">SQL SERVER</FONT>里查询<FONT face="Times New Roman">access</FONT>数据:</P>
' U# k. z: V0 b( n< ><FONT face="Times New Roman"> <p></p></FONT></P>
) ~% z9 j4 v3 s< ><FONT face="Times New Roman">    SELECT * FROM OpenDataSource</FONT>(<FONT face="Times New Roman"> \\\'Microsoft.Jet.OLEDB.4.0\\\'</FONT>,<FONT face="Times New Roman">\\\'Data Source="c</FONT>:<FONT face="Times New Roman">\\\\DB.mdb"</FONT>;<FONT face="Times New Roman">User ID=Admin</FONT>;<FONT face="Times New Roman">assword=\\\'</FONT>)……表名</P>6 y+ n2 O8 z7 S  [( i; [& h
< ><FONT face="Times New Roman"> <p></p></FONT></P>2 S# E) ^) i: k; Z
< ><FONT face="Times New Roman">    2.</FONT>将<FONT face="Times New Roman">access</FONT>导入<FONT face="Times New Roman">SQL server</FONT>在<FONT face="Times New Roman">SQL SERVER </FONT>里运行:</P>
7 m* |' ?; \: L; b! q  E< ><FONT face="Times New Roman"> <p></p></FONT></P>
; t1 F, L. {- b' T5 ^< ><FONT face="Times New Roman">SELECT * INTO newtable FROM OPENDATASOURCE </FONT>(<FONT face="Times New Roman">\\\'Microsoft.Jet.OLEDB.4.0\\\'</FONT>,<FONT face="Times New Roman">\\\'Data Source="c</FONT>:<FONT face="Times New Roman">\\\\DB.mdb"</FONT>;<FONT face="Times New Roman">User ID=Admin</FONT>;<FONT face="Times New Roman">assword=\\\' </FONT>)……表名<FONT face="Times New Roman"> </FONT></P>
' D$ {# w8 q- X1 ^5 J1 w* F% Z/ `, E# u< ><FONT face="Times New Roman"> <p></p></FONT></P>
: w( R& ?2 }" j, e" j2 d<P ><FONT face="Times New Roman">    3.</FONT>将<FONT face="Times New Roman">SQL SERVER</FONT>表里的数据插入到<FONT face="Times New Roman">Access</FONT>表中在<FONT face="Times New Roman">SQL SERVER </FONT>里运行:</P># V# A4 p% Q0 m
<P ><FONT face="Times New Roman"> <p></p></FONT></P>
% L1 K' n, A* s% P<P ><FONT face="Times New Roman">insert into OpenDataSource</FONT>(<FONT face="Times New Roman"> \\\'Microsoft.Jet.OLEDB.4.0\\\'</FONT>,<FONT face="Times New Roman">\\\'Data Source=" c</FONT>:<FONT face="Times New Roman">\\\\DB.mdb"</FONT>;<FONT face="Times New Roman">User ID=Admin</FONT>;<FONT face="Times New Roman">Password=\\\'</FONT>)……表名<FONT face="Times New Roman"> </FONT>(列名<FONT face="Times New Roman">1</FONT>,列名<FONT face="Times New Roman">2</FONT>)<FONT face="Times New Roman"> select </FONT>列名<FONT face="Times New Roman">1</FONT>,列名<FONT face="Times New Roman">2 from sql</FONT>表<FONT face="Times New Roman"> </FONT></P>; P* q% m- }$ g& {/ W5 @! Q  U
<P ><FONT face="Times New Roman"> <p></p></FONT></P>" B( G1 ?3 \% @4 [
<P ><FONT face="Times New Roman">    </FONT>实例:<FONT face="Times New Roman">insert into OPENROWSET</FONT>(<FONT face="Times New Roman">\\\'Microsoft.Jet.OLEDB.4.0\\\'</FONT>,<FONT face="Times New Roman">\\\'C</FONT>:<FONT face="Times New Roman">\\\\db.mdb\\\'</FONT>;<FONT face="Times New Roman">\\\'admin\\\'</FONT>;<FONT face="Times New Roman">\\\'\\\'</FONT>,<FONT face="Times New Roman"> Test</FONT>)<FONT face="Times New Roman"> select id</FONT>,<FONT face="Times New Roman">name from Test</FONT></P>( l  m3 c, q$ M) O6 ~
<P ><FONT face="Times New Roman"> <p></p></FONT></P>
/ p* S+ f( J3 G; {9 o$ ^, w4 A<P ><FONT face="Times New Roman">    INSERT INTO OPENROWSET</FONT>(<FONT face="Times New Roman">\\\'Microsoft.Jet.OLEDB.4.0\\\'</FONT>,<FONT face="Times New Roman"> \\\'c</FONT>:<FONT face="Times New Roman">\\\\trade.mdb\\\'</FONT>;<FONT face="Times New Roman"> \\\'admin\\\'</FONT>;<FONT face="Times New Roman"> \\\'\\\'</FONT>,<FONT face="Times New Roman"> </FONT>表名)<FONT face="Times New Roman"> SELECT * FROM sqltablename</FONT></P>
) j' }: C2 n; K( q- H3 J1 G<P ><FONT face="Times New Roman"> <p></p></FONT></P>
$ `  n. \4 k0 j7 q% Q<P ><FONT face="Times New Roman">    </FONT>二、<FONT face="Times New Roman">SQL SERVER </FONT>和<FONT face="Times New Roman">EXCEL</FONT>的数据导入导出</P>
  j. N4 T2 H, ^) ^<P ><FONT face="Times New Roman"> <p></p></FONT></P>
' G! a+ O% _- }. k: F; d" ~( G<P ><FONT face="Times New Roman">    1</FONT>、在<FONT face="Times New Roman">SQL SERVER</FONT>里查询<FONT face="Times New Roman">Excel</FONT>数据:</P>
  Y) T' h' B1 L& G<P ><FONT face="Times New Roman"> <p></p></FONT></P>
+ I, H, S0 P0 W; W$ q3 U! L3 a$ `<P ><FONT face="Times New Roman">    SELECT * FROM OpenDataSource</FONT>(<FONT face="Times New Roman"> \\\'Microsoft.Jet.OLEDB.4.0\\\'</FONT>,<FONT face="Times New Roman">\\\'Data Source="c</FONT>:<FONT face="Times New Roman">\\\\book1.xls"</FONT>;<FONT face="Times New Roman">User ID=Admin</FONT>;<FONT face="Times New Roman">Password=</FONT>;<FONT face="Times New Roman">Extended properties=Excel 5.0\\\'</FONT>)……<FONT face="Times New Roman">[Sheet1$] </FONT></P>  }: ]% C9 f4 a! p* ~, B# Q  `
<P ><FONT face="Times New Roman"> <p></p></FONT></P>' e, I& e* ^  i3 X" O
<P ><FONT face="Times New Roman">    </FONT>下面是个查询的示例,它通过用于<FONT face="Times New Roman"> Jet </FONT>的<FONT face="Times New Roman"> OLE DB </FONT>提供程序查询<FONT face="Times New Roman"> Excel </FONT>电子表格。</P>
" j# ]; g0 S5 o. C" |% n$ M<P ><FONT face="Times New Roman"> <p></p></FONT></P>8 Z! O' H# y) F, o' M1 |
<P ><FONT face="Times New Roman">    SELECT * FROM OpenDataSource </FONT>(<FONT face="Times New Roman"> \\\'Microsoft.Jet.OLEDB.4.0\\\'</FONT>,<FONT face="Times New Roman">\\\'Data Source="c</FONT>:<FONT face="Times New Roman">\\\\Finance\\\\account.xls"</FONT>;<FONT face="Times New Roman">User ID=Admin</FONT>;<FONT face="Times New Roman">Password=</FONT>;<FONT face="Times New Roman">Extended properties=Excel 5.0\\\'</FONT>)……<FONT face="Times New Roman">xactions</FONT></P>
- h% B% a& f0 X7 t6 T; B<P ><FONT face="Times New Roman"> <p></p></FONT></P>5 n* i- U0 O+ ?* a' `& \
<P ><FONT face="Times New Roman">    2</FONT>、将<FONT face="Times New Roman">Excel</FONT>的数据导入<FONT face="Times New Roman">SQL server </FONT>:</P>* h1 R' s6 l* T
<P ><FONT face="Times New Roman"> <p></p></FONT></P>8 ^' \+ T  L) c( h8 b* K
<P ><FONT face="Times New Roman">    SELECT * into newtable FROM OpenDataSource</FONT>(<FONT face="Times New Roman"> \\\'Microsoft.Jet.OLEDB.4.0\\\'</FONT>,<FONT face="Times New Roman">\\\'Data Source="c</FONT>:<FONT face="Times New Roman">\\\\book1.xls"</FONT>;<FONT face="Times New Roman">User ID=Admin</FONT>;<FONT face="Times New Roman">Password=</FONT>;<FONT face="Times New Roman">Extended properties=Excel 5.0\\\'</FONT>)……<FONT face="Times New Roman">[Sheet1$] </FONT></P>, Q8 }$ N* p$ @# Q& C
<P ><FONT face="Times New Roman"> <p></p></FONT></P>
% S! T% W. x& C9 G  f. ?# G<P ><FONT face="Times New Roman">    </FONT>实例:<FONT face="Times New Roman">SELECT * into newtable FROM OpenDataSource</FONT>(<FONT face="Times New Roman"> \\\'Microsoft.Jet.OLEDB.4.0\\\'</FONT>,<FONT face="Times New Roman">\\\'Data Source="c</FONT>:<FONT face="Times New Roman">\\\\Finance\\\\account.xls"</FONT>;<FONT face="Times New Roman">User ID=Admin</FONT>;<FONT face="Times New Roman">Password=</FONT>;<FONT face="Times New Roman">Extended properties=Excel 5.0\\\'</FONT>)……<FONT face="Times New Roman">xactions</FONT></P>
( Z" q8 Y* H" o8 Z. Z6 j<P ><FONT face="Times New Roman"> <p></p></FONT></P>
/ j0 |8 H5 p0 P/ T<P ><FONT face="Times New Roman">    3</FONT>、将<FONT face="Times New Roman">SQL SERVER</FONT>中查询到的数据导成一个<FONT face="Times New Roman">Excel</FONT>文件<FONT face="Times New Roman">T-SQL</FONT>代码:</P>
& \! L% C" l4 \1 q- t<P ><FONT face="Times New Roman"> <p></p></FONT></P>8 |# e5 D7 w+ q( h0 u
<P ><FONT face="Times New Roman">    EXEC master</FONT>……<FONT face="Times New Roman">xp_cmdshell \\\'bcp </FONT>库名。<FONT face="Times New Roman">dbo.</FONT>表名<FONT face="Times New Roman">out c</FONT>:<FONT face="Times New Roman">\\\\Temp.xls -c -q -S"servername" -U"sa" -P""\\\'</FONT>参数:<FONT face="Times New Roman">S </FONT>是<FONT face="Times New Roman">SQL</FONT>服务器名;<FONT face="Times New Roman">U</FONT>是用户;<FONT face="Times New Roman">P</FONT>是密码说明:还可以导出文本文件等多种格式<FONT face="Times New Roman"> </FONT></P>7 A4 [% r+ m+ T
<P ><FONT face="Times New Roman"> <p></p></FONT></P>* f  }: K4 ~* |2 n8 M, Z: v9 V
<P ><FONT face="Times New Roman">    </FONT>实例:<FONT face="Times New Roman">EXEC master</FONT>……<FONT face="Times New Roman">xp_cmdshell \\\'bcp saletesttmp.dbo.CusAccount out c</FONT>:<FONT face="Times New Roman">\\\\temp1.xls -c -q -S"pmserver" -U"sa" -P"sa"\\\' </FONT></P>0 K( T! S1 F% r+ g: t
<P ><FONT face="Times New Roman"> <p></p></FONT></P>
" b4 e' M7 \! Y0 L: E/ B<P ><FONT face="Times New Roman">    EXEC master</FONT>……<FONT face="Times New Roman">xp_cmdshell \\\'bcp "SELECT au_fname</FONT>,<FONT face="Times New Roman"> au_lname FROM pubs</FONT>……<FONT face="Times New Roman">authors ORDER BY au_lname" queryout C</FONT>:<FONT face="Times New Roman">\\\\ authors.xls -c -Sservername -Usa -Ppassword\\\'</FONT></P>
; q) f# q. |2 ~/ w' q- y<P ><FONT face="Times New Roman"> <p></p></FONT></P>) t: Z  ]" _7 |( k  v  V& t
<P ><FONT face="Times New Roman">    </FONT>在<FONT face="Times New Roman">VB6</FONT>中应用<FONT face="Times New Roman">ADO</FONT>导出<FONT face="Times New Roman">EXCEL</FONT>文件代码:<FONT face="Times New Roman">Dim cn As New ADODB.Connection cn.open "Driver={SQL Server}</FONT>;<FONT face="Times New Roman">Server=WEBSVR</FONT>;<FONT face="Times New Roman">DataBase=WebMis</FONT>;<FONT face="Times New Roman">UID=sa</FONT>;<FONT face="Times New Roman">WD=123</FONT>;<FONT face="Times New Roman">" cn.execute "master</FONT>……<FONT face="Times New Roman">xp_cmdshell \\\'bcp "SELECT col1</FONT>,<FONT face="Times New Roman"> col2 FROM </FONT>库名。<FONT face="Times New Roman">dbo.</FONT>表名<FONT face="Times New Roman">" queryout E</FONT>:<FONT face="Times New Roman">\\\\DT.xls -c -Sservername -Usa -Ppassword\\\'" </FONT></P>( Z  @% ?5 c4 u9 B
<P ><FONT face="Times New Roman"> <p></p></FONT></P>! D8 o& f6 u: z) `
<P ><FONT face="Times New Roman">    4</FONT>、在<FONT face="Times New Roman">SQL SERVER</FONT>里往<FONT face="Times New Roman">Excel</FONT>插入数据:</P>+ z6 f9 ^/ n. o; W5 h
<P ><FONT face="Times New Roman"> <p></p></FONT></P>
# [: I+ R+ `' W! p5 l$ m6 H<P ><FONT face="Times New Roman">    insert into OpenDataSource</FONT>(<FONT face="Times New Roman"> \\\'Microsoft.Jet.OLEDB.4.0\\\'</FONT>,<FONT face="Times New Roman">\\\'Data Source="c</FONT>:<FONT face="Times New Roman">\\\\Temp.xls"</FONT>;<FONT face="Times New Roman">User ID=Admin</FONT>;<FONT face="Times New Roman">Password=</FONT>;<FONT face="Times New Roman">Extended properties=Excel 5.0\\\'</FONT>)……<FONT face="Times New Roman">table1 </FONT>(<FONT face="Times New Roman">A1</FONT>,<FONT face="Times New Roman">A2</FONT>,<FONT face="Times New Roman">A3</FONT>)<FONT face="Times New Roman"> values </FONT>(<FONT face="Times New Roman">1</FONT>,<FONT face="Times New Roman">2</FONT>,<FONT face="Times New Roman">3</FONT>)<FONT face="Times New Roman"> </FONT></P>4 ~/ |3 d$ f. {, T$ w
<P ><FONT face="Times New Roman"> <p></p></FONT></P>
4 F& e( V% M2 Q; b2 w9 ^  k<P ><FONT face="Times New Roman">    T-SQL</FONT>代码:<FONT face="Times New Roman">INSERT INTO OPENDATASOURCE</FONT>(<FONT face="Times New Roman">\\\'Microsoft.JET.OLEDB.4.0\\\'</FONT>,<FONT face="Times New Roman">\\\'Extended Properties=Excel 8.0</FONT>;<FONT face="Times New Roman">Data source=C</FONT>:<FONT face="Times New Roman">\\\\training\\\\inventur.xls\\\'</FONT>)……<FONT face="Times New Roman">[Filiale1$] </FONT>(<FONT face="Times New Roman">bestand</FONT>,<FONT face="Times New Roman"> produkt</FONT>)<FONT face="Times New Roman"> VALUES </FONT>(<FONT face="Times New Roman">20</FONT>,<FONT face="Times New Roman"> \\\'Test\\\'</FONT>)</P>% `0 j2 X0 n3 h9 C9 }
<P ><FONT face="Times New Roman"> <p></p></FONT></P>    总结:利用以上语句,我们可以方便地将SQL SERVER、ACCESS和EXCEL电子表格软件中的数据进行转换,为我们提供了极大方便!




欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5