如何用SQL語句在兩個資料庫間複製儲存過程

2021-08-29 19:22:35 字數 2990 閱讀 4237

--1.在目標伺服器上建立如下物件(被同步的伺服器)

if exists (select * from dbo.sysobjects where id = object_id(n'[sys_syscomments_bak]') and objectproperty(id, n'isusertable') = 1)

drop table [sys_syscomments_bak]

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

drop procedure [dbo].[p_process_object]

go --建立輔助處理的表

create table sys_syscomments_bak(name sysname,xtype char(2),number smallint,colid smallint,status smallint,ctext varbinary(8000))

go exec sp_configure 'allow updates',1 reconfigure with override

go --建立處理的儲存過程

create proc p_process_object

as set xact_abort on

exec sp_configure 'allow updates',1 reconfigure with override

begin tran

--先刪除系統表中的舊記錄

delete a

from syscomments c,sysobjects o,sys_syscomments_bak ob

where c.id=o.id

and o.name=ob.name and o.xtype=ob.xtype

--再插入新記錄到系統表中

insert syscomments([id],[number],[colid],[status],[ctext])

select o.[id],ob.[number],ob.[colid],ob.[status],ob.[ctext]

from sysobjects o,sys_syscomments_bak ob

where o.name=ob.name and o.xtype=ob.xtype

commit tran

--重新編譯所有的物件

declare tb cursor local for

select case

when xtype='v' then 'exec sp_refreshview '

else 'sp_recompile' end

+'['+replace(object_name(id),n']',n']]')+']'''

from sys_syscomments_bak

declare @s nvarchar(4000)

open tb

fetch tb into @s

while @@fetch_status=0

begin

exec(@s)

fetch tb into @s

end

close tb

deallocate tb

exec sp_configure 'allow updates',0 reconfigure with override

go exec sp_configure 'allow updates',0 reconfigure with override

go --2.在源伺服器(提供被同步物件的伺服器)

--先建立鏈結伺服器,鏈結到目標伺服器

if exists(select * from master..sysservers where srvname='srv_lnk')

exec sp_dropserver 'srv_lnk','droplogins'

exec sp_addlinkedserver 'srv_lnk','','sqloledb','目標伺服器ip位址'

exec sp_addlinkedsrvlogin 'srv_lnk','false',null,'登入的使用者名稱','登入密碼'

exec sp_serveroption 'srv_lnk','rpc out','true'

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

drop procedure [dbo].[p_copyobject]

go --再建立如下的處理過程來實現同步

create proc p_copyobject

as --複製本機物件到目標伺服器,注意修改庫名為遠端目標伺服器的庫名

delete from srv_lnk.庫名.dbo.sys_syscomments_bak

insert srv_lnk.庫名.dbo.sys_syscomments_bak

(name,xtype,number,colid,status,ctext)

select o.name,o.xtype,c.number,c.colid,c.status,c.ctext

from syscomments c,sysobjects o

where c.id=o.id

and o.status>=0

and o.xtype in('v','p','fn','if','tf','tr')

--呼叫遠端的儲存過程完成最終的複製任務

exec srv_lnk.庫名.dbo.sys_syscomments_bak

go

如何用SQL語句在兩個資料庫間複製儲存過程

1.在目標伺服器上建立如下物件 被同步的伺服器 if exists select from dbo.sysobjects where id object id n sys syscomments bak and objectproperty id,n isusertable 1 drop table...

如何用SQL語句在兩個資料庫間複製儲存過程

1.在目標伺服器上建立如下物件 被同步的伺服器 if exists select from dbo.sysobjects where id object id n sys syscomments bak and objectproperty id,n isusertable 1 drop table...

SQL語句 在兩個資料庫間複製表結構和資料資料庫

一 如果兩個資料庫在同一臺伺服器上 1 複製表結構和資料 select into databaseb.dbo.tableb from databasea.dbo.tablea 2 僅僅複製表結構 select into databaseb.dbo.tableb from databasea.dbo....