QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 5057|回复: 1
打印 上一主题 下一主题

[分享]SQL SERVER、ACCESS和EXCEL电子表格软件中的数据进行转换

[复制链接]
字体大小: 正常 放大

2

主题

0

听众

19

积分

升级  14.74%

该用户从未签到

新人进步奖

跳转到指定楼层
1#
发表于 2005-8-8 14:22 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
< >熟悉<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>/ q8 q& Y' Z" G( O8 r
< ><FONT face="Times New Roman"> <p></p></FONT></P>
2 J9 A! _. I2 U3 X/ J2 B& ^0 {< ><FONT face="Times New Roman">    </FONT>一、<FONT face="Times New Roman">SQL SERVER </FONT>和<FONT face="Times New Roman">ACCESS</FONT>的数据导入导出</P>
! Y; @+ e# e+ m0 Q& ^8 W4 J  V< ><FONT face="Times New Roman"> <p></p></FONT></P>
7 l& `5 j; ?' F* c9 P1 P2 c< ><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>' p) V+ L- Z3 W5 G4 \8 z9 G
< ><FONT face="Times New Roman"> <p></p></FONT></P>: E" x: _# F5 S% z
< ><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>/ W8 _& t! `0 B8 Z
< ><FONT face="Times New Roman"> <p></p></FONT></P>
! ~: {/ V9 C) S6 R: Z1 h< ><FONT face="Times New Roman">    2Services</FONT>(数据转换服务),然后选择<FONT face="Times New Roman"> czdImport Data</FONT>(导入数据)。</P>
5 H- G& O$ K. K2 z) E; t< ><FONT face="Times New Roman"> <p></p></FONT></P>
" i/ K! y* M, M, x  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># \0 p/ a4 h5 C& W8 V: ]
< ><FONT face="Times New Roman"> <p></p></FONT></P>
# R- c  h8 {6 b) `& U< ><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>
5 ^: u$ |7 w0 {0 h; j< ><FONT face="Times New Roman"> <p></p></FONT></P>
7 o2 J. V1 f8 L( z< ><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>
) v' h4 \0 z4 [: r+ n8 [< ><FONT face="Times New Roman"> <p></p></FONT></P>" |! o9 E) s2 y. Y% r( `( p
< ><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>7 w0 [8 C+ i2 d# O8 S
< ><FONT face="Times New Roman"> <p></p></FONT></P>
* ]/ t: }2 R& ]0 s. [< ><FONT face="Times New Roman">    Transact-SQL</FONT>语句进行导入导出:</P>5 K# d1 z1 R( Y
< ><FONT face="Times New Roman"> <p></p></FONT></P>
: T5 u" Y7 ~2 X" c4 J3 R< ><FONT face="Times New Roman">    1.</FONT>在<FONT face="Times New Roman">SQL SERVER</FONT>里查询<FONT face="Times New Roman">access</FONT>数据:</P>/ C) c' |( v7 _& I0 j% t
< ><FONT face="Times New Roman"> <p></p></FONT></P>
1 b. g! j" d) K# C: U: |2 d. k< ><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>
: m7 Y5 j+ o4 o& T< ><FONT face="Times New Roman"> <p></p></FONT></P>6 X$ w4 q8 G8 S2 B  F! H  G( I! R
< ><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>0 s0 M' q  [6 j5 Z7 P
< ><FONT face="Times New Roman"> <p></p></FONT></P>- E; V/ S: T' v5 X4 `3 @$ M
< ><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>
( c2 E+ E7 V* g! K< ><FONT face="Times New Roman"> <p></p></FONT></P>
! W  G7 Z. T) @* m7 i' H5 e<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>
  {- f! [  h; G5 W+ z( n<P ><FONT face="Times New Roman"> <p></p></FONT></P>
1 e* l# D  s3 p6 u<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>6 A, r- u% j, d- @7 O
<P ><FONT face="Times New Roman"> <p></p></FONT></P>1 C+ y$ G5 H1 q7 P" V& U. i! O6 t
<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>
4 }4 k! |, G( }& k) a<P ><FONT face="Times New Roman"> <p></p></FONT></P>
6 g9 p2 O. m% [/ j<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* {7 X. l7 B6 K<P ><FONT face="Times New Roman"> <p></p></FONT></P>( i9 k: ]- l  m+ A
<P ><FONT face="Times New Roman">    </FONT>二、<FONT face="Times New Roman">SQL SERVER </FONT>和<FONT face="Times New Roman">EXCEL</FONT>的数据导入导出</P>1 J5 P/ W" W3 q$ l7 p, n* k# Z
<P ><FONT face="Times New Roman"> <p></p></FONT></P>2 |$ j  G. I( Z4 B* k2 e# t- |
<P ><FONT face="Times New Roman">    1</FONT>、在<FONT face="Times New Roman">SQL SERVER</FONT>里查询<FONT face="Times New Roman">Excel</FONT>数据:</P>
0 G' @: H/ T3 w. }$ u<P ><FONT face="Times New Roman"> <p></p></FONT></P>6 L% v" L+ g6 M4 n' b+ c6 W' F, d6 _
<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>$ b5 L8 d; H$ ]0 C, q
<P ><FONT face="Times New Roman"> <p></p></FONT></P>( e$ h% D' _! Q- F
<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>
$ m; p+ z) l3 B7 b7 a' U/ }, q" l* A<P ><FONT face="Times New Roman"> <p></p></FONT></P>
  ^! ?& K6 Q/ i0 i8 \& }<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>
6 I5 J3 u( o/ m; c2 }7 J* Q<P ><FONT face="Times New Roman"> <p></p></FONT></P>
; A, _+ D* Y8 I( C" I+ H3 Q! o<P ><FONT face="Times New Roman">    2</FONT>、将<FONT face="Times New Roman">Excel</FONT>的数据导入<FONT face="Times New Roman">SQL server </FONT>:</P>
% q9 \3 E* p) O9 y<P ><FONT face="Times New Roman"> <p></p></FONT></P>/ i8 q  _* w; h7 ?- a# t5 B
<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>$ e4 P( E* {2 d& Z$ m) M. b0 v( L
<P ><FONT face="Times New Roman"> <p></p></FONT></P>' X" H% g. l/ o4 v7 i2 A
<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>
! y9 q" x) T, Y5 _5 w<P ><FONT face="Times New Roman"> <p></p></FONT></P>7 t, }# B* h% x& A
<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>5 E8 m2 Q% Z5 h  R  K9 H! E+ f* A
<P ><FONT face="Times New Roman"> <p></p></FONT></P>
. L. f5 S" F; j9 p8 {- S<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 N- i1 f9 y$ _<P ><FONT face="Times New Roman"> <p></p></FONT></P>! R; I5 v1 U3 X5 v$ S
<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>: H/ p% s  ~  [& x' Q, K
<P ><FONT face="Times New Roman"> <p></p></FONT></P>0 n" b- p1 I7 b, T, s3 u1 X
<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>+ }% D# d; S6 Y# F4 c
<P ><FONT face="Times New Roman"> <p></p></FONT></P>
; x" U; Q2 J/ Z. o. J# |<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>0 o0 I+ J; t( o" G, {3 m
<P ><FONT face="Times New Roman"> <p></p></FONT></P>  P& J* ?8 f; L  t) A& |9 O8 ~
<P ><FONT face="Times New Roman">    4</FONT>、在<FONT face="Times New Roman">SQL SERVER</FONT>里往<FONT face="Times New Roman">Excel</FONT>插入数据:</P>
! O4 l0 H# p1 b) Y<P ><FONT face="Times New Roman"> <p></p></FONT></P>* H# \) x! o" k9 a1 ^8 m  }
<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>
& N+ n& r0 b2 E; O+ v% I<P ><FONT face="Times New Roman"> <p></p></FONT></P>6 G2 l4 G0 q) t2 ?" i8 S/ c
<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 T) i' }, H4 g5 W3 Y: a& y
<P ><FONT face="Times New Roman"> <p></p></FONT></P>
$ v$ J. I6 R1 O( n2 q3 c" g8 M<P ><FONT face="Times New Roman">    </FONT>总结:利用以上语句,我们可以方便地将<FONT face="Times New Roman">SQL SERVER</FONT>、<FONT face="Times New Roman">ACCESS</FONT>和<FONT face="Times New Roman">EXCEL</FONT>电子表格软件中的数据进行转换,为我们提供了极大方便!</P>
zan
转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
lzh0601        

2

主题

3

听众

89

积分

升级  88.42%

该用户从未签到

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册地址

qq
收缩
  • 电话咨询

  • 04714969085
fastpost

关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

手机版|Archiver| |繁體中文 手机客户端  

蒙公网安备 15010502000194号

Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

GMT+8, 2026-4-12 08:04 , Processed in 0.472030 second(s), 63 queries .

回顶部