<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[Heck's  Blog]]></title> 
<link>https://www.heckjj.com/index.php</link> 
<description><![CDATA[一瞬间的决定，往往可以改变很多，事实上，让自己成功的往往不是知识，是精神！ 如果你总是为自己找借口，那只好让成功推迟。执行力，今天！]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[Heck's  Blog]]></copyright>
<item>
<link>https://www.heckjj.com/post//</link>
<title><![CDATA[两个不同数据库之间复制数据效率高的方法]]></title> 
<author>Heck &lt;@hecks.tk&gt;</author>
<category><![CDATA[数据库]]></category>
<pubDate>Wed, 31 Aug 2011 14:22:52 +0000</pubDate> 
<guid>https://www.heckjj.com/post//</guid> 
<description>
<![CDATA[ 
	<span style="font-family: 微软雅黑;">在MS-SQLServer中，如何在不同的数据库之间复制表？<br/>当表目标表存在时：<br/>insert into 目的数据库..表(字段1...字段n) select&nbsp;&nbsp;段1...字段n&nbsp;&nbsp;from 源数据库..表<br/><br/>当目标表不存在时：<br/>select 段1...字段n&nbsp;&nbsp;into 目的数据库..表 from 源数据库..表<br/>--如果在不同的SQL之间: <br/>insert into openrowset(&#039;sqloledb&#039;,&#039;目的服务器名&#039;;&#039;sa&#039;;&#039;&#039;,目的数据库.dbo.表) <br/>select * from 源数据库..表<br/><br/>除了上面的方法之外或者还可以用链接服务器: </span><br/><textarea name="code" class="sql" rows="15" cols="100">--创建链接服务器 
exec sp_addlinkedserver &#039;srv_lnk&#039;,&#039;&#039;,&#039;SQLOLEDB&#039;,&#039;远程服务器名&#039; 
exec sp_addlinkedsrvlogin &#039;srv_lnk&#039;,&#039;false&#039;,null,&#039;sa&#039;,&#039;密码&#039; 
exec sp_serveroption &#039;srv_lnk&#039;,&#039;rpc out&#039;,&#039;true&#039; --这个允许调用链接服务器上的存储过程 
go 

--查询示例 
select * from srv_lnk.数据库名.dbo.表名

--导入示例 
select * into 表 from srv_lnk.数据库名.dbo.表名 

go 
--后删除链接服务器 
exec sp_dropserver &#039;srv_lnk&#039;,&#039;droplogins&#039; 

--如果是将一个数据库中的数据全部复制到另一个数据库,而且两个库结构完全一样的话,就用备份/恢复的方式: 

/*--将一个数据库完整复制成另一个数据库 

--Heck--*/ 

/*--调用示例 
exec p_CopyDb @ddbname=&#039;test&#039; 
--*/ 

if exists (select * from dbo.sysobjects where id = object_id(N&#039;[dbo].[p_CopyDb]&#039;) and OBJECTPROPERTY(id, N&#039;IsProcedure&#039;) = 1) 
drop procedure [dbo].[p_CopyDb] 
GO 

create proc p_CopyDb 
@sdbname sysname=&#039;&#039;, --定义要复制的数据库名,默认为当前数据库 
@ddbname sysname, --定义复制后生成的数据库名 
@overexist bit=1, --是否覆盖已经存在的数据库 
@killuser bit=1 --是否关闭用户使用进程,仅@overexist=1时有效 
as 
declare @sql varchar(8000),@bpath varchar(8000),@rpath varchar(8000) 

--得到要复制的数据库名 
if isnull(@sdbname,&#039;&#039;)=&#039;&#039; set @sdbname=db_name() 

--得到临时备份数据目录及文件名 
select @bpath=rtrim(reverse(filename)) from master..sysfiles where name=&#039;master&#039; 
select @bpath=substring(@bpath,charindex(&#039;&#92;&#039;,@bpath)+1,8000) 
,@bpath=reverse(substring(@bpath,charindex(&#039;&#92;&#039;,@bpath),8000))+&#039;BACKUP&#92;&#039; 
+@sdbname+&#039;_&#039;+convert(varchar,getdate(),112) 
+&#039;_&#039;+replace(convert(varchar,getdate(),108),&#039;:&#039;,&#039;&#039;) 
+&#039;.bak&#039; 

--生成数据库备份语句,进行数据库备份 
set @sql=&#039;backup database &#039;+@sdbname 
+&#039; to disk=&#039;&#039;&#039;+@bpath 
+&#039;&#039;&#039; with NOINIT&#039; 
exec(@sql) 

--根据备份文件恢复成新的数据库(完成复制工作) 
set @sql=&#039;restore database &#039;+@ddbname 
+&#039; from disk=&#039;&#039;&#039;+@bpath+&#039;&#039;&#039;&#039; 
+&#039; with file=1&#039; 
+case when @overexist=1 then &#039;,replace&#039; else &#039;&#039; end 

--得到数据库存放的默认目录 
--得到SQL安装时设置的数据文件路径 
select @rpath=rtrim(reverse(filename)) from master..sysfiles where name=&#039;master&#039; 
select @rpath=reverse(substring(@rpath,charindex(&#039;&#92;&#039;,@rpath),8000)) 

--添加移动逻辑文件的处理 
--从备份文件中获取逻辑文件名 
declare @lfn nvarchar(128),@tp char(1),@i int 

--创建临时表,保存获取的信息 
create table #tb(ln nvarchar(128),pn nvarchar(260),tp char(1),fgn nvarchar(128),
sz numeric(20,0),Msz numeric(20,0)) 
--从备份文件中获取信息 
insert into #tb exec(&#039;restore filelistonly from disk=&#039;&#039;&#039;+@bpath+&#039;&#039;&#039;&#039;) 
declare #f cursor for select ln,tp from #tb 
open #f 
fetch next from #f into @lfn,@tp 
set @i=0 
while @@fetch_status=0 
begin 
select @sql=@sql+&#039;,move &#039;&#039;&#039;+@lfn+&#039;&#039;&#039; to &#039;&#039;&#039;+@rpath+@ddbname+cast(@i as varchar) 
+case @tp when &#039;D&#039; then &#039;.mdf&#039;&#039;&#039; else &#039;.ldf&#039;&#039;&#039; end 
,@i=@i+1 
fetch next from #f into @lfn,@tp 
end 
close #f 
deallocate #f </textarea><br/><br/><textarea name="code" class="sql" rows="15" cols="100">--关闭用户进程处理 
if @overexist=1 and @killuser=1 
begin 
declare @spid varchar(20) 
declare #spid cursor for 
select spid=cast(spid as varchar(20)) from master..sysprocesses where dbid=db_id(@ddbname) 
open #spid 
fetch next from #spid into @spid 
while @@fetch_status=0 
begin 
exec(&#039;kill &#039;+@spid) 
fetch next from #spid into @spid 
end 
close #spid 
deallocate #spid 
end 

--恢复数据库 
exec(@sql) 

--删除备份的临时文件 
set @sql=&#039;del &quot;&#039;+@bpath+&#039;&quot;&#039; 
exec master..xp_cmdshell @sql,no_output 
select @sql,@bpath,@rpath 
go
</textarea><br/>--如果一定要逐个表复制用: <br/>use 源库 <br/>go <br/>exec sp_msforeachtable <br/>select * into 目的数据库..from..<br/><br/>最好的办法是用DTS(导入导出工具)做好DTS包。<br/><br/><textarea name="code" class="sql" rows="15" cols="100">--如果两个库的结构有些不同,就用: 
/*--数据库数据复制,将一个数据库中的数据复制到另一个数据库 
--如果某列在目标数据库中为标识列,将不会被复制 
--适用范围:数据库结构发生了变化,想将旧数据库进行升级 
--这样就可以根据新的数据库结构创建一个空库,然后 
--将旧数据库的所有数据复制到新库中 
--Heck--*/ 

/*--调用示例 
exec p_copydb &#039;源数据库&#039;,&#039;目标数据库&#039; 
exec p_copydb &#039;acc_五医&#039;,&#039;acc_演示数据8&#039; 
--*/ 

if exists (select * from dbo.sysobjects where id = object_id(N&#039;[dbo].[p_copydb]&#039;) and OBJECTPROPERTY(id, N&#039;IsProcedure&#039;) = 1) 
drop procedure [dbo].[p_copydb] 
GO 

create proc p_copydb 
@o_dbname sysname, --要复制数据的数据库--源数据库 
@n_dbname sysname, --接收数据的数据库--目标数据库 
@cleardb bit=0 --清空目标数据库 
as 
declare @sql nvarchar(4000) 

--禁用约束,防止复制时的数据冲突 
set @sql=&#039;declare #tbc cursor for select name,tbname=object_name(parent_obj) 
from &#039;+@n_dbname+&#039;..sysobjects where xtype in(&#039;&#039;C&#039;&#039;,&#039;&#039;F&#039;&#039;)&#039; 
exec(@sql) 
declare @name sysname,@tbname sysname 
open #tbc 
fetch next from #tbc into @name,@tbname 
while @@fetch_status=0 
begin 
set @sql=&#039;alter table &#039;+@n_dbname+&#039;..[&#039;+@tbname+&#039;] NOCHECK CONSTRAINT [&#039;+@name+&#039;]&#039; 
exec(@sql) 
fetch next from #tbc into @name,@tbname 
end 
close #tbc 

--复制数据 
declare @sql1 varchar(8000) 
set @sql=&#039;declare #tb cursor for select a.name from &#039; 
+@o_dbname+&#039;..sysobjects a inner join &#039; 
+@n_dbname+&#039;..sysobjects b on a.name=b.name 
where a.xtype=&#039;&#039;U&#039;&#039; and b.xtype=&#039;&#039;U&#039;&#039;&#039; 
exec(@sql) 
open #tb 
fetch next from #tb into @tbname 
while @@fetch_status=0 
begin 
select @sql1=&#039;&#039; 
,@sql=&#039;select @sql1=@sql1+&#039;&#039;,[&#039;&#039;+a.name+&#039;&#039;]&#039;&#039; from( 
select name from &#039;+@o_dbname+&#039;..syscolumns where id in 
(select id from &#039;+@o_dbname+&#039;..sysobjects where name=&#039;&#039;&#039;+@tbname+&#039;&#039;&#039;) 
) a inner join ( 
select name from &#039;+@n_dbname+&#039;..syscolumns where status&lt;&gt;0x80 and id in 
(select id from &#039;+@n_dbname+&#039;..sysobjects where name=&#039;&#039;&#039;+@tbname+&#039;&#039;&#039;) 
) b on a.name=b.name&#039; 
exec sp_executesql @sql,N&#039;@sql1 nvarchar(4000) out&#039;,@sql1 out 

select @sql1=substring(@sql1,2,8000) 
exec(&#039;insert into &#039;+@n_dbname+&#039;..[&#039;+@tbname+&#039;](&#039;+@sql1 
+&#039;) select &#039;+@sql1+&#039; from &#039;+@o_dbname+&#039;..[&#039;+@tbname+&#039;]&#039;) 
if @@error&lt;&gt;0 
print(&#039;insert into &#039;+@n_dbname+&#039;..[&#039;+@tbname+&#039;](&#039;+@sql1 
+&#039;) select &#039;+@sql1+&#039; from &#039;+@o_dbname+&#039;..[&#039;+@tbname+&#039;]&#039;) 
fetch next from #tb into @tbname 
end 
close #tb 
deallocate #tb 

--数据复制完成后启用约束 
open #tbc 
fetch next from #tbc into @name,@tbname 
while @@fetch_status=0 
begin 
set @sql=&#039;alter table &#039;+@n_dbname+&#039;..[&#039;+@tbname+&#039;] CHECK CONSTRAINT [&#039;+@name+&#039;]&#039; 
exec(@sql) 
fetch next from #tbc into @name,@tbname 
end 
close #tbc 
deallocate #tbc 
go</textarea><br/>Tags - <a href="https://www.heckjj.com/tags/%25E6%2595%25B0%25E6%258D%25AE%25E5%25BA%2593/" rel="tag">数据库</a> , <a href="https://www.heckjj.com/tags/%25E5%25A4%258D%25E5%2588%25B6%25E6%2595%25B0%25E6%258D%25AE/" rel="tag">复制数据</a> , <a href="https://www.heckjj.com/tags/sql/" rel="tag">sql</a>
]]>
</description>
</item><item>
<link>https://www.heckjj.com/post//#blogcomment</link>
<title><![CDATA[[评论] 两个不同数据库之间复制数据效率高的方法]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>https://www.heckjj.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>