十年網(wǎng)站開(kāi)發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶(hù) + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專(zhuān)業(yè)推廣+無(wú)憂(yōu)售后,網(wǎng)站問(wèn)題一站解決
下面我們開(kāi)始介紹用存儲(chǔ)過(guò)程實(shí)現(xiàn)SQL Server數(shù)據(jù)庫(kù)的同步方法,首先說(shuō)明,我們使用的存儲(chǔ)過(guò)程是需要運(yùn)行在服務(wù)器上的,如果換庫(kù),不需要修改任何東西。接下來(lái)我們就逐步演示這一過(guò)程:

建立存儲(chǔ)所有表名的表:
- if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].p_bakup_tatle_all') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
- drop procedure [dbo].p_bakup_tatle_all
- GO
- create proc p_bakup_tatle_all
- as
- if exists (select * from dbo.sysobjects where id = object_id(N'table_all ') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
- drop table table_all
- CREATE TABLE [dbo].[Table_all](
- [id] [int] IDENTITY(1,1) NOT NULL,
- [name] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
- CONSTRAINT [PK_Table_all] PRIMARY KEY CLUSTERED
- (
- [id] ASC
- )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
- ) ON [PRIMARY]
將所有表名存放在表table_all中:
- insert into table_all(name) select name from sysobjects where xtype='U'
- GO
備份服務(wù)器上的存儲(chǔ)過(guò)程,若換庫(kù),需要修改InfoCenter兩處,還有連接服務(wù)的地址。
創(chuàng)建數(shù)據(jù)同步的存儲(chǔ)過(guò)程:
- if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].p_bakup_tatle_all') and OBJECTPROPERTY(id, N'IsProcedure') =
- 1)
- drop procedure [dbo].p_bakup_tatle_all
- GO
- create proc p_bakup_tatle_all
- as
創(chuàng)建鏈接服務(wù)器:
- exec sp_addlinkedserver 'ITSV ', ' ', 'SQLOLEDB ', '61.135.203.103'
- exec sp_addlinkedsrvlogin 'ITSV ', 'false ',null, 'sa', 'comsky103@2011.com'
查詢(xún)示例 select * from ITSV.test.dbo.[users]。
導(dǎo)入將服務(wù)器上的表以及數(shù)據(jù)保存在本地:
- if exists (select * from dbo.sysobjects where id = object_id(N'table_all ') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
- drop table table_all
- select * into table_all from ITSV.InfoCenter.dbo.table_all
- DECLARE @Name varchar(50),@count int,@i int
- set @i=1
- select @countcount=count(*) from table_all
- while @i<=@count
- begin
- select @Name=name from table_all where id=@i
- if exists(select name from sysobjects where name=''+@name+'' and type='u')
- exec('drop table ['+@Name+']')
- exec('select * into ['+@Name+'] from ITSV.InfoCenter.dbo.['+@Name+']' )
- PRINT @Name
- set @i=@i+1
- PRINT @i
以后不再使用時(shí)刪除鏈接服務(wù)器:
- exec sp_dropserver 'ITSV ', 'droplogins'
- go
以上就是用存儲(chǔ)過(guò)程實(shí)現(xiàn)SQL Server數(shù)據(jù)庫(kù)同步的全部過(guò)程,本文就介紹到這里,謝謝大家的支持!