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

首頁 > 開發 > 綜合 > 正文

通過觸發器實現數據庫的即時同步

2024-07-21 02:08:43
字體:
來源:轉載
供稿:網友

--即時同步兩個表的實例:

--測試環境:sql2000,遠程主機名:xz,用戶名:sa,密碼:無,數據庫名:test

--創建測試表,不能用標識列做主鍵,因為不能進行正常更新
--在本機上創建測試表,遠程主機上也要做同樣的建表操作,只是不寫觸發器
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

--創建同步的觸發器
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

--插入數據測試
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'

--刪除數據測試
delete from test where id in(1,4,6)

--更新數據測試
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


 
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 子长县| 安平县| 江陵县| 项城市| 舞阳县| 罗定市| 大荔县| 黄冈市| 彭州市| 府谷县| 定州市| 北京市| 岢岚县| 枣阳市| 大同县| 银川市| 耒阳市| 安岳县| 中阳县| 土默特右旗| 青河县| 绥中县| 朝阳县| 望都县| 三都| 丰原市| 那曲县| 江孜县| 沁阳市| 云霄县| 丰台区| 章丘市| 鹿泉市| 海南省| 克什克腾旗| 那坡县| 环江| 扶绥县| 分宜县| 鲁山县| 汕头市|