SQL两个数据库 数据导入

本文介绍如何在SQL Server中通过创建链接服务器、使用openrowset等方法实现不同数据库间的数据迁移,包括具体步骤与示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

向基础数据库bj5288_base中UserEmail导入用户的EmailAddress数据<br>数据源来自营销数据库bj5288_marketing的UserEmail表<br>执行方法:<br>insert into <span style="color: rgb(255, 0, 255);">bj5288_base..UserEmail</span>(EmailAddress) select EmailAddress from <span style="color: rgb(255, 0, 255);">bj5288_marketing..UserEmail </span><br>注意:<span style="color: rgb(255, 0, 0);">bj5288_base..UserEmail和bj5288_marketing..UserEmail之间是两个小点<br><br><br><br style="color: rgb(0, 0, 0);"><span style="color: rgb(0, 0, 0);">以下部分来转载于优快云:</span><br style="color: rgb(0, 0, 0);"><span style="color: rgb(0, 0, 0);">http://topic.youkuaiyun.com/u/20071114/16/E198F78B-3BAE-47DF-A697-AB4029A44032.html</span><br></span>
<pre><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">创建链接服务器</span><span style="color: rgb(0, 128, 128);"><br></span><span style="color: rgb(0, 0, 255);">exec</span><span style="color: rgb(0, 0, 0);"> sp_addlinkedserver </span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">ITSV</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(255, 0, 0);">''</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">SQLOLEDB</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">远程服务器名或ip地址</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"><br></span><span style="color: rgb(0, 0, 255);">exec</span><span style="color: rgb(0, 0, 0);"> sp_addlinkedsrvlogin </span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">ITSV</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">false</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">用户名</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">密码</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"><br><br></span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">查询示例</span><span style="color: rgb(0, 128, 128);"><br></span><span style="color: rgb(0, 0, 255);">select</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">*</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> ITSV.数据库名.dbo.表名<br><br></span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">导入示例</span><span style="color: rgb(0, 128, 128);"><br></span><span style="color: rgb(0, 0, 255);">select</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">*</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">into</span><span style="color: rgb(0, 0, 0);"> 表 </span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> ITSV.数据库名.dbo.表名<br><br></span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">以后不再使用时删除链接服务器</span><span style="color: rgb(0, 128, 128);"><br></span><span style="color: rgb(0, 0, 255);">exec</span><span style="color: rgb(0, 0, 0);"> sp_dropserver </span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">ITSV</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">droplogins</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"><br><br></span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">连接远程/局域网数据(openrowset/openquery/opendatasource)</span><span style="color: rgb(0, 128, 128);"><br>--</span><span style="color: rgb(0, 128, 128);">1、openrowset</span><span style="color: rgb(0, 128, 128);"><br></span><span style="color: rgb(0, 0, 0);"><br></span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">查询示例</span><span style="color: rgb(0, 128, 128);"><br></span><span style="color: rgb(0, 0, 255);">select</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">*</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 255);">openrowset</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">SQLOLEDB</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">sql服务器名</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">用户名</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">密码</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,数据库名.dbo.表名)<br><br></span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">生成本地表</span><span style="color: rgb(0, 128, 128);"><br></span><span style="color: rgb(0, 0, 255);">select</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">*</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">into</span><span style="color: rgb(0, 0, 0);"> 表 </span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 255);">openrowset</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">SQLOLEDB</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">sql服务器名</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">用户名</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">密码</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,数据库名.dbo.表名)<br><br></span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">把本地表导入远程表</span><span style="color: rgb(0, 128, 128);"><br></span><span style="color: rgb(0, 0, 255);">insert</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 255);">openrowset</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">SQLOLEDB</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">sql服务器名</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">用户名</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">密码</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,数据库名.dbo.表名)<br></span><span style="color: rgb(0, 0, 255);">select</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">*</span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> 本地表<br><br></span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">更新本地表</span><span style="color: rgb(0, 128, 128);"><br></span><span style="color: rgb(0, 0, 255);">update</span><span style="color: rgb(0, 0, 0);"> b<br></span><span style="color: rgb(0, 0, 255);">set</span><span style="color: rgb(0, 0, 0);"> b.列A</span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(0, 0, 0);">a.列A<br></span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 255);">openrowset</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">SQLOLEDB</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">sql服务器名</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">用户名</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">密码</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,数据库名.dbo.表名)</span><span style="color: rgb(0, 0, 255);">as</span><span style="color: rgb(0, 0, 0);"> a </span><span style="color: rgb(0, 0, 255);">inner</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">join</span><span style="color: rgb(0, 0, 0);"> 本地表 b<br></span><span style="color: rgb(0, 0, 255);">on</span><span style="color: rgb(0, 0, 0);"> a.column1</span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(0, 0, 0);">b.column1<br><br></span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">openquery用法需要创建一个连接</span><span style="color: rgb(0, 128, 128);"><br></span><span style="color: rgb(0, 0, 0);"><br></span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">首先创建一个连接创建链接服务器</span><span style="color: rgb(0, 128, 128);"><br></span><span style="color: rgb(0, 0, 255);">exec</span><span style="color: rgb(0, 0, 0);"> sp_addlinkedserver </span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">ITSV</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(255, 0, 0);">''</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">SQLOLEDB</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">远程服务器名或ip地址</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"><br></span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">查询</span><span style="color: rgb(0, 128, 128);"><br></span><span style="color: rgb(0, 0, 255);">select</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">*</span><span style="color: rgb(0, 0, 0);"><br></span><span style="color: rgb(0, 0, 255);">FROM</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 255);">openquery</span><span style="color: rgb(0, 0, 0);">(ITSV, </span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">SELECT * FROM 数据库.dbo.表名</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br></span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">把本地表导入远程表</span><span style="color: rgb(0, 128, 128);"><br></span><span style="color: rgb(0, 0, 255);">insert</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 255);">openquery</span><span style="color: rgb(0, 0, 0);">(ITSV, </span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">SELECT * FROM 数据库.dbo.表名</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br></span><span style="color: rgb(0, 0, 255);">select</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">*</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> 本地表<br></span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">更新本地表</span><span style="color: rgb(0, 128, 128);"><br></span><span style="color: rgb(0, 0, 255);">update</span><span style="color: rgb(0, 0, 0);"> b<br></span><span style="color: rgb(0, 0, 255);">set</span><span style="color: rgb(0, 0, 0);"> b.列B</span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(0, 0, 0);">a.列B<br></span><span style="color: rgb(0, 0, 255);">FROM</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 255);">openquery</span><span style="color: rgb(0, 0, 0);">(ITSV, </span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">SELECT * FROM 数据库.dbo.表名</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 255);">as</span><span style="color: rgb(0, 0, 0);"> a <br></span><span style="color: rgb(0, 0, 255);">inner</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">join</span><span style="color: rgb(0, 0, 0);"> 本地表 b </span><span style="color: rgb(0, 0, 255);">on</span><span style="color: rgb(0, 0, 0);"> a.列A</span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(0, 0, 0);">b.列A<br><br></span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">3、opendatasource/openrowset</span><span style="color: rgb(0, 128, 128);"><br></span><span style="color: rgb(0, 0, 255);">SELECT</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">*</span><span style="color: rgb(0, 0, 0);"><br></span><span style="color: rgb(0, 0, 255);">FROM</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">opendatasource</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">SQLOLEDB</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">Data Source=ip/ServerName;User ID=登陆名;Password=密码</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"> ).test.dbo.roy_ta<br></span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">把本地表导入远程表</span><span style="color: rgb(0, 128, 128);"><br></span><span style="color: rgb(0, 0, 255);">insert</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">opendatasource</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">SQLOLEDB</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">Data Source=ip/ServerName;User ID=登陆名;Password=密码</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">).数据库.dbo.表名<br></span><span style="color: rgb(0, 0, 255);">select</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">*</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> 本地表 <br>**************************************************************************************************************************************<br></span>使用ADO将本地ACCESS数据库中的A表所有数据追加到SQL Server中的B表<br><span style="color: rgb(0, 0, 0);">**************************************************************************************************************************************<br></span>'这是 分布式查询(Distribute Query)的范畴 <br> dim cn as adodb.connection <br><br> cn.open "Provider=SQLOLEDB.1;Persist Security Info=True;User <br>ID=sa;Initial Catalog=pubs;Data Source=master" <br> cn.execute "insert <br> into sqlTbl_a <br> SELECT a.* <br> FROM <br>OPENROWSET('Microsoft.Jet.OLEDB.4.0', <br><br>'c:/MSOffice/Access/Samples/northwind.mdb';'admin';'mypwd', Orders) " <br><br><br><br> '参阅 Ms Sql7 中 有 OPENROWSET (T-SQL)例 : <br><br> SELECT a.* <br> FROM <br>OPENROWSET('Microsoft.Jet.OLEDB.4.0', <br><br>'c:/MSOffice/Access/Samples/northwind.mdb';'admin';'mypwd', Orders) <br><br><br> insert into sqlTbl_a <br> SELECT <br>a.* <br> FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0', <br><br> 'c:/MSOffice/Access/Samples/northwind.mdb';'admin';'mypwd', Orders) </pre>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值