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

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

如何通過觸發(fā)器實現(xiàn)數(shù)據(jù)庫的即時同步

2024-07-21 02:42:32
字體:
來源:轉載
供稿:網(wǎng)友
問題:如何通過觸發(fā)器實現(xiàn)數(shù)據(jù)庫的即時同步?

解答:具體的解決方法請參考下文。

即時同步兩個表的示例如下:

測試環(huán)境如下:SQL Sever 2000數(shù)據(jù)庫,遠程主機名:xz,用戶名:sa,密碼:無,數(shù)據(jù)庫名:test

創(chuàng)建測試表,請注意不能用標識列做主鍵,因為不能進行正常的更新。

然后在本機上創(chuàng)建測試表,注意遠程主機上也要做同樣的建表操作,只是不寫觸發(fā)器。

if exists (select * from dbo.sysobjects where id = object_id(N'[test]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)

drop table [test]

create table test(id int not null constraint PK_test primary key

,name varchar(10))

go

--創(chuàng)建同步的觸發(fā)器

create trigger t_test on test

for insert,update,delete

as

set XACT_ABORT on

--啟動遠程服務器的MSDTC服務

exec master..xp_cmdshell 'isql /S"xz" /U"sa" /P"" /q"exec master..xp_cmdshell ''net start msdtc'',no_output"',no_output

--啟動本機的MSDTC服務

exec master..xp_cmdshell 'net start msdtc',no_output

--進行分布事務處理,假如表用標識列做主鍵,你可以使用下面的這個方法

BEGIN DISTRIBUTED TRANSACTION

delete from openrowset('sqloledb','xz';'sa';'',test.dbo.test)

where id in(select id from deleted)

insert into openrowset('sqloledb','xz';'sa';'',test.dbo.test)

select * from inserted

commit tran

go

--插入數(shù)據(jù)測試

insert into test

select 1,'aa'

union all select 2,'bb'

union all select 3,'c'

union all select 4,'dd'

union all select 5,'ab'

union all select 6,'bc'

union all select 7,'ddd'

--刪除數(shù)據(jù)測試

delete from test where id in(1,4,6)

--更新數(shù)據(jù)測試

update test set name=name+'_123' where id in(3,5)

--最后顯示測試的結果

select * from test a full join

openrowset('sqloledb','xz';'sa';'',test.dbo.test) b on a.id=b.id


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 泽普县| 英超| 武宣县| 垣曲县| 望江县| 叶城县| 钦州市| 黄平县| 佛学| 盐边县| 德惠市| 运城市| 南康市| 榆中县| 梅河口市| 法库县| 乡城县| 东乡族自治县| 酒泉市| 江永县| 新泰市| 甘德县| 长沙县| 双辽市| 石景山区| 怀柔区| 松江区| 霍林郭勒市| 垫江县| 吴旗县| 彰武县| 大化| 任丘市| 铁力市| 普兰店市| 县级市| 安义县| 班玛县| 天柱县| 长乐市| 津市市|