不同数据库之间导入导出功能介绍

1. 在sql server数据库之间进行数据导入导出
 (1).使用select into导出数据

  在sql server中使用最广泛的就是通过select into语句导出数据,select into语句同时具备两个功能:根据select后跟的字段以及into后面跟的表名建立空表(如果select后是*, 空表的结构和from所指的表的结构相同);将select查出的数据插入到这个空表中。在使用select into语句时,into后跟的表必须在数据库不存在,否则出错,下面是一个使用select into的例子。

  假设有一个表table1,字段为f1(int)、f2(varchar(50))。

  select * into table2 from table1

  这条sql语的在建立table2表后,将table1的数据全部插入到table1中的,还可以将*改为f1或f2以便向适当的字段中插入数据。

  select into不仅可以在同一个数据中建立表,也可以在不同的sql server数据库中建立表。

  use db1

  select * into db2.dbo.table2 from table1

  以上语句在数据库db2中建立了一个所有者是dbo的表table2,在向db2建表时当前登录的用户必须有在db2建表的权限才能建立table2。使用select into要注意的一点是select into不可以和compute一起使用,因为compute返回的是一组记录集,这将会引起二意性(即不知道根据哪个表建立空表)。

(2).使用insert into 和 update插入和更新数据

  select into只能将数据复制到一个空表中,而insert into可以将一个表或视图中的数据插入到另外一个表中。

  insert into table1 select * from table2

  或 insert into db2.dbo.table1 select * from table2

  但以上的insert into语句可能会产生一个主键冲突错误(如果table1中的某个字段是主键,恰巧table2中的这个字段有的值和table1的这个字段的值相同)。因此,上面的语句可以修改为

  insert into table1 — 假设字段f1为主键

  select * from table2 where not exists(select table1.f1 from table1 where table1.f1=table2.f1 )

  以上语句的功能是将table2中f1在table1中不存在的记录插入到table1中。

  要想更新table1可以使用update语句

  update table1 set table1.f1=table2.f1, table1.f2=table2.f2 from table2 where table1.f1=table2.f1

  将以上两条insert into和update语句组合起来在一起运行,就可以实现记录在table1中不存在时插入,存在时更新的功能,但要注意要将update放在 insert into前面,否则update更新的记录数将是table1和table2记录数的总和。

 2. 使用opendatasource和openrowset在不同类型的数据库之间导入导出数据
  在异构的数据库之间进行数据传输,可以使用sql server提供的两个系统函数opendatasource和openrowset。

  opendatasource可以打开任何支持ole db的数据库,并且可以将opendatasource做为select、update、insert和delete后所跟的表名。如

select * from opendatasource( ‘sqloledb’,’datasource=192.168.1.113;user id=sa;password=123456′).pubs.dbo.authors

select * from opendatasource(sqloledb, data source=192.168.18.252;user id=sa;password=test).pubs.dbo.authors

  这条语句的功能是查询192.168.18.252这台机器中sql server数据库pubs中的authors表。从这条语句可以看出,opendatasource有两个参数,第一个参数是 provider_name,表示用于访问数据源的 ole db 提供程序的 progid 的名称。provider_name 的数据类型为 char,没有默认值。第二个参数是连接字符串,根据ole db provider不同而不同(如果不清楚自己所使用的ole db provider的连接字符串,可以使用delphi、visual studio等开发工具中的ado控件自动生成相应的连接字符串)。

  openrowset函数和opendatasource函数类似,只是它可以在打开数据库的同时对数据库中的表进行查询,如以下语句

  openrowset(msdasql.1, driver=microsoft visual foxpro driver; sourcedb=c:\db; sourcetype=dbf, select * from [b.dbf])

  最后一个参数查询foxpro表b.dbf,读者可以通过where条件对b.dbf进行过滤。如果将insert into、select into和opendatasource或openrowset一起使用,就可以使sql server数据库和其它类型的数据库之间进行数据导入导出。下面介绍如何使用这两个函数在sql server数据库和其它类型的数据库之间进行数据导入导出。

  (1).sql server数据库和sql server数据库之间的数据导入导出。

  导入数据

  select * intoauthors1 from opendatasource( sqloledb, data source=192.168.18.252;user id=sa;password=abc).pubs.dbo.authors

  导出数据

  insert into opendatasource(sqloledb,data source=192.168.18.252;user id=sa;password=abc).test.dbo.authors select * from pubs.dbo.authors

  在这条语句中opendatasource(…)可以理解为sql server的一个服务,.pubs.dbo.authors是这个服务管理的一个数据库的一个表authors。使用insert into时opendatasource(…)后跟的表必须存在。

  也可以将以上的opendatasource换成openrowset

  insert into openrowset(sqloledb,192.168.18.252;sa;abc, select * from test.dbo.kk) select * from pubs.dbo.authors

  使用openrowset要注意一点,192.168.18.252;sa;abc中间是”;”,而不是”,”。opendatasource和openrowset都不接受参数变量。

  (2). sql server数据库和access数据库之间的数据导入导出。

  导入数据

  select * into access from opendatasource( microsoft.jet.oledb.4.0, provider=microsoft.jet.oledb.4.0;data source=c:\data.mdb;persist security info=false)…table1

  或者使用openrowset

  select * from openrowset(microsoft.jet.oledb.4.0, c:\data.mdb;admin;,select * from table1)

  导出数据

  insert into opendatasource(microsoft.jet.oledb.4.0,provider=microsoft.jet.oledb.4.0;data source=c:\data.mdb;persist security info=false)…table1 select * from access

  打开access数据库的ole db provider叫microsoft.jet.oledb.4.0,需要注意的是操作非sql server数据库在opendatasource(…)后面引用数据库中的表时使用”…”,而不是“.”。

  (3). sql server数据库和文本文件之间的数据导入导出。

  导入数据

  select * into text1 from opendatasource(microsoft.jet.oledb.4.0,text;database=c:\)…[data#txt]

  导出数据

  insert into opendatasource(microsoft.jet.oledb.4.0,text;database=c:\)…[data#txt] select * from text1

  或者使用openrowset

  insert into openrowset(microsoft.jet.oledb.4.0,text;database=c:\, [data#txt]) select * from text1

  如果要插入部分字段,可使用

  insert into openrowset(microsoft.jet.oledb.4.0,text;database=c:\, select aa from [data#txt]) select aa from text1

  这条sql语句的功能是将c盘根目录的data.txt文件导入到text1表中,在这里文件名中的“.”要使用“#”代替。在向文本导出时,不仅文本文件要存在,而且第一行必须和要导出表的字段一至。

(4). sql server数据库和dbase数据库之间的数据导入导出。

  导入数据

  select * into dbase from openrowset(microsoft.jet.oledb.4.0 , dbase iii;hdr=no;imex=2;database=c:\,select * from [b.dbf])

  导出数据

  insert into openrowset(microsoft.jet.oledb.4.0 , dbase iii;hdr=no;imex=2;database=c:\,select * from [b.dbf]) select * from dbase

  openrowset(…)中的b.dbf使用[…]括起来,是为了当dbf文件名有空格等字符时不会出错,如果没有这些特殊字符,可以将[…]去掉

  (5). sql server数据库和foxpro数据库之间的数据导入导出。

  导入数据

  select * into foxpro from openrowset(msdasql.1, driver=microsoft visual foxpro driver;sourcedb=c:\; sourcetype=dbf, select * from [a.dbf])

  导出数据

  insert into openrowset(msdasql.1 , driver=microsoft visual foxpro driver; sourcedb=c:\db;sourcetype=dbf,select * from a.dbf) select * from foxpro

  在此处a.dbf不能使用[…]括起来,否则出错(这是由driver决定的)。

  (6). sql server数据库和excel文件之间的数据导入导出

  导入数据

  select * into excel from opendatasource(microsoft.jet.oledb.4.0,excel 5.0;database=c:\book1.xls )…[sheet1$]

  导出数据

  insert into opendatasource(microsoft.jet.oledb.4.0,excel 5.0;database=c:\book1.xls )…[sheet1$] select * from excel

  在book1.xls的sheet1中必须有和excel表相对应的字段,否则会出错。

  以上讨论了几种常用的数据库和sql server数据库之间如何使用transact-sql进行数据导入导出。在sql server中还提供了将其它类型的数据库注册到sql server中的功能,这样就可以和使用sql server数据库表一样使用这些被注册数据库中的表了。

  exec sp_addlinkedserver access,ole db provider for jet, microsoft.jet.oledb.4.0, c:\data.mdb

  以上sql使用存储过程sp_addlinkedserver注册了一个access数据库,我们可以在sql server中使用如下语句查询在data.mdb中的table1。

  select * from access…table1

  这样就可很方便地查询access数据库中的表了,如果要导入table1,可以使用select * into table2 from access…table1。如果想删除注册的数据库连接,使用如下语句。

  exec sp_dropserver access

  使用transact-sql不仅可以向sql server数据库导入导出数据,而且还可以使任意两种类型数据库之间互相导入导出数据。以access和excel为例进行说明。

  insert into opendatasource(microsoft.jet.oledb.4.0,excel 5.0;database=c:\book1.xls )…[sheet1$] select * from openrowset(microsoft.jet.oledb.4.0, c:\data.mdb;admin;,select * from table1)

  以上sql语句将access数据库的table1表的数据插入到excel文件book1.xls中的sheet1表单中。

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐