使用触发器实现两个数据库同步
2011-09-29 16:55
573 查看
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创建触发器实现统一主机下两个数据库的表同步
- 如何利用触发器实现两个数据库间的同步
- 使用PLSQL Developer来实现不同数据库的表结构以及表数据同步
- 通过触发器实现数据库的即时同步
- 两个一样的oracle 数据库怎么实现数据定期同步?
- 使用Red Gate Sql Compare 数据库同步工具进行SQL Server的两个数据库的结构比较、同步
- oracle_fdw的使用:从PostgreSQL中访问Oracle数据库,实现数据库数据的同步
- 使用Red Gate Sql Data Compare 数据库同步工具进行SQL Server的两个数据库的数据比较、同步
- 项目中遇到通过使用路由策略实现主从数据库访问数据不同步的问题
- oracle中使用触发器实现数据同步
- sql server之触发器调用C#CLR存储过程实现两个表的数据同步
- asp.net中使用 Repeater控件拖拽实现排序并同步数据库字段排序
- solr 7+tomcat 8 + mysql实现solr 7基本使用(安装、集成中文分词器、定时同步数据库数据以及项目集成)
- 如何通过触发器实现数据库的即时同步
- 数据库(SQL)之trigger(触发器)的使用以及视图(view)的基本实现
- 使用触发器即时同步两个表的实例
- 如何实现两个数据库同步?
- 通过触发器实现数据库的即时同步
- 快照、刷新-[置顶] Oracle如何实现两个数据库的同步(用实体化视图实现)(oracle快照实例)-by小雨