日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用触发器即时同步两个表的实例

發布時間:2025/5/22 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用触发器即时同步两个表的实例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

即時同步兩個表的實例:

--測試環境: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

總結

以上是生活随笔為你收集整理的使用触发器即时同步两个表的实例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。