使用触发器实现两个数据库同步
来源:互联网 发布:highcharts 3d.js下载 编辑:程序博客网 时间:2024/06/05 03:02
if object_id('TABLE_1') is not null drop table TABLE_1
CREATE TABLE TABLE_1
(
ID INT primary key,
Name1 nchar(10),
Name2 nchar(10)
)
if object_id('TABLE_2') is not null drop table TABLE_2
CREATE TABLE TABLE_2
(
ID INT primary key,
Name1 nchar(10),
Name2 nchar(10)
)
INSERT INTO Table_1 VALUES(1,'adsd','Chi')
INSERT INTO Table_1 VALUES(2,'xiao','Chi')
INSERT INTO Table_2 VALUES(1,'Lei','Chi')
INSERT INTO Table_2 VALUES(2,'Alex','Chi')
if exists (select name
from sysobjects
where name = 'tr_info'
and type = 'tr')
drop trigger tr_info
go
create trigger tr_info on TABLE_2
for insert,update,delete
as
begin
if exists(select 1 from inserted) and not exists(select 1 from deleted)
begin
insert into Table_1 select * from inserted
end
else if exists(select 1 from inserted) and exists(select 1 from deleted)
begin
update b1
set b1.Name1=U.Name1,b1.Name2=U.Name2
from Table_1 b1,deleted U
where b1.ID=U.ID
end
else
begin
delete Table_1 where ID=(select ID from deleted)
end
end
--插入测试
INSERT INTO Table_2 VALUES(3,'huguo','Chi')
select * from TABLE_1
select * from TABLE_2
--删除测试
delete from Table_2 where ID=3
select * from TABLE_1
select * from TABLE_2
--更新测试
update Table_2 set Name1='ALex2' where ID=2
select * from TABLE_1
select * from TABLE_2
CREATE TABLE TABLE_1
(
ID INT primary key,
Name1 nchar(10),
Name2 nchar(10)
)
if object_id('TABLE_2') is not null drop table TABLE_2
CREATE TABLE TABLE_2
(
ID INT primary key,
Name1 nchar(10),
Name2 nchar(10)
)
INSERT INTO Table_1 VALUES(1,'adsd','Chi')
INSERT INTO Table_1 VALUES(2,'xiao','Chi')
INSERT INTO Table_2 VALUES(1,'Lei','Chi')
INSERT INTO Table_2 VALUES(2,'Alex','Chi')
if exists (select name
from sysobjects
where name = 'tr_info'
and type = 'tr')
drop trigger tr_info
go
create trigger tr_info on TABLE_2
for insert,update,delete
as
begin
if exists(select 1 from inserted) and not exists(select 1 from deleted)
begin
insert into Table_1 select * from inserted
end
else if exists(select 1 from inserted) and exists(select 1 from deleted)
begin
update b1
set b1.Name1=U.Name1,b1.Name2=U.Name2
from Table_1 b1,deleted U
where b1.ID=U.ID
end
else
begin
delete Table_1 where ID=(select ID from deleted)
end
end
--插入测试
INSERT INTO Table_2 VALUES(3,'huguo','Chi')
select * from TABLE_1
select * from TABLE_2
--删除测试
delete from Table_2 where ID=3
select * from TABLE_1
select * from TABLE_2
--更新测试
update Table_2 set Name1='ALex2' where ID=2
select * from TABLE_1
select * from TABLE_2
- 使用触发器实现两个数据库同步
- 触发器实现同步两个数据库
- 如何利用触发器实现两个数据库间的同步
- 同一服务器,两个数据库同步(触发器)
- 触发器实现两个库之间数据同步
- MySQL创建触发器实现统一主机下两个数据库的表同步
- 通过触发器实现数据库的即时同步
- 通过触发器实现数据库的即时同步
- 通过触发器实现数据库的即时同步
- 通过触发器实现数据库的即时同步
- 通过触发器实现数据库的即时同步
- 通过触发器实现数据库的即时同步
- 通过触发器实现数据库的即时同步
- 通过触发器实现数据库的即时同步
- 通过触发器实现数据库的即时同步
- 通过触发器实现数据库的即时同步
- 实现两个Mysql数据库同步
- 实现两个数据库的同步***
- 获取窗口句柄(VC)的几种方法———转
- 更改maven打包文件的默认输出
- Ubuntu下从源码手工编译安装及配置PHP5
- 一步一步设置Joomla!开发环境
- STL 组建
- 使用触发器实现两个数据库同步
- jpa 实体4个状态
- Android自定义title
- 创建WCF第一个应用程序
- add route
- 不是书评 :《我是一只IT小小鸟》
- 表单form里的method属性post、get
- cocos2d介绍
- 软件系统性能设计之五多线程设计