国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

數(shù)據(jù)庫數(shù)據(jù)復制

2024-07-21 02:11:59
字體:
供稿:網(wǎng)友

/*--數(shù)據(jù)庫數(shù)據(jù)復制

 將一個數(shù)據(jù)庫中的數(shù)據(jù)復制到另一個數(shù)據(jù)庫
 如果某列在目標數(shù)據(jù)庫中為標識列,將不會被復制

 適用范圍:數(shù)據(jù)庫結(jié)構(gòu)發(fā)生了變化,想將舊數(shù)據(jù)庫進行升級
  這樣就可以根據(jù)新的數(shù)據(jù)庫結(jié)構(gòu)創(chuàng)建一個空庫,然后
  將舊數(shù)據(jù)庫的所有數(shù)據(jù)復制到新庫中
--*/

/*--調(diào)用示例

 exec p_copydb '源數(shù)據(jù)庫','目標數(shù)據(jù)庫'
 exec p_copydb 'acc_五醫(yī)','acc_演示數(shù)據(jù)8'
--*/

if exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[p_copydb]') and objectproperty(id, n'isprocedure') = 1)
drop procedure [dbo].[p_copydb]
go

create proc p_copydb
@o_dbname sysname,  --要復制數(shù)據(jù)的數(shù)據(jù)庫--源數(shù)據(jù)庫
@n_dbname sysname,  --接收數(shù)據(jù)的數(shù)據(jù)庫--目標數(shù)據(jù)庫
@cleardb bit=0   --清空目標數(shù)據(jù)庫
as
declare @sql nvarchar(4000)

--禁用約束,防止復制時的數(shù)據(jù)沖突
set @sql='declare #tbc cursor for select name,tbname=object_name(parent_obj)
 from '[email protected]_dbname+'..sysobjects where xtype in(''c'',''f'')'
exec(@sql)
declare @name sysname,@tbname sysname
open #tbc
fetch next from #tbc into @name,@tbname
while @@fetch_status=0
begin
 set @sql='alter table '[email protected]_dbname+'..['[email protected]+'] nocheck constraint ['[email protected]+']'
 exec(@sql)
 fetch next from #tbc into @name,@tbname
end
close #tbc

--復制數(shù)據(jù)
declare @sql1 varchar(8000)
set @sql='declare #tb cursor for select a.name from '
 [email protected]_dbname+'..sysobjects a inner join '
 [email protected]_dbname+'..sysobjects b on a.name=b.name
  where a.xtype=''u'' and b.xtype=''u'''
exec(@sql)
open #tb
fetch next from #tb into @tbname
while @@fetch_status=0
begin
 select @sql1=''
  ,@sql='select @[email protected]+'',[''+a.name+'']'' from(
  select name from '[email protected]_dbname+'..syscolumns where id in
  (select id from '[email protected]_dbname+'..sysobjects where name='''[email protected]+''')
 ) a inner join (
  select name from '[email protected]_dbname+'..syscolumns where status<>0x80 and id in
  (select id from '[email protected]_dbname+'..sysobjects where name='''[email protected]+''')
 ) b on a.name=b.name'
 exec sp_executesql @sql,n'@sql1 nvarchar(4000) out',@sql1 out

 select @sql1=substring(@sql1,2,8000)
 exec('insert into '[email protected]_dbname+'..['[email protected]+']('[email protected]
  +') select '[email protected]+' from '[email protected]_dbname+'..['[email protected]+']')
 if @@error<>0
  print('insert into '[email protected]_dbname+'..['[email protected]+']('[email protected]
   +') select '[email protected]+' from '[email protected]_dbname+'..['[email protected]+']')
 fetch next from #tb into @tbname
end
close #tb
deallocate #tb

--數(shù)據(jù)復制完成后啟用約束
open #tbc
fetch next from #tbc into @name,@tbname
while @@fetch_status=0
begin
 set @sql='alter table '[email protected]_dbname+'..['[email protected]+'] check constraint ['[email protected]+']'
 exec(@sql)
 fetch next from #tbc into @name,@tbname
end
close #tbc
deallocate #tbc
go




發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 颍上县| 呼伦贝尔市| 乐亭县| 新蔡县| 柳州市| 饶平县| 孝昌县| 香港 | 大邑县| 吉水县| 柳林县| 安康市| 南汇区| 萨迦县| 文安县| 黔南| 会宁县| 阳曲县| 华池县| 恭城| 安丘市| 青田县| 阿合奇县| 南通市| 噶尔县| 平顺县| 雷波县| 健康| 大安市| 桃江县| 平乐县| 开平市| 富平县| 寻甸| 长泰县| 丰镇市| 个旧市| 宜阳县| 五河县| 瓮安县| 安吉县|