资料同步

好帅的一条鱼 2011-12-19 10:14:26
将A数据库的 表1 在更新资料的时候,同步在B数据库的表1.;两个表字段是一模一样的,大家帮忙给点资料。谢谢!
...全文
60 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
dwarf471 2011-12-19
  • 打赏
  • 举报
回复

create trigger [dbo].[TableA_Trigger] on [dbo].[TableA] for insert,update,delete
as
begin
declare @operatetype char(1);
Begin
IF EXISTS(SELECT 1 FROM inserted) AND NOT EXISTS(SELECT 1 FROM deleted) set @operatetype ='1';
else IF NOT EXISTS(SELECT 1 FROM inserted) AND EXISTS(SELECT 1 FROM deleted) set @operatetype ='2';
else set @operatetype ='3';
end
if(@operatetype ='2')
begin
INSERT INTO [TableA_LOG] ([a],[b],[Operatetype],[OperateTime])
select [a],[b],@operatetype,getdate()
from deleted
end
else
begin
INSERT INTO [TableA_LOG] ([a],[b],[Operatetype],[OperateTime])
select [a],[b],@operatetype,getdate()
from inserted
end
end
GO


在A表上新建触发器,增删改时插入记录到A的LOG表
pengxuan 2011-12-19
  • 打赏
  • 举报
回复

--在A库和B库上都建一个如下的tb表
if object_id('tb') is not null
drop table tb
go
create table tb
(
id int,
name varchar(10)
)
go
--在A库的tb上建插入触发器
if object_id('tr_tb_forinsert') is not null
drop trigger tr_tb_forinsert
go
create trigger tr_tb_forinsert on tb
for insert
as
insert into B.dbo.tb select * from inserted
go
--在A库的tb上建更新触发器
if object_id('tr_tb_forupdate') is not null
drop trigger tr_tb_forupdate
go
create trigger tr_tb_forupdate on tb
for update
as
update B.dbo.tb set id=t2.id,name=t2.name from B.dbo.tb t1 inner join inserted t2 on t1.id=t2.id
go
--在A库执行插入和更新语句,触发器会自动执行B库tb表的插入和更新
insert into tb select 1,'张三'
update tb set name='李四' where id=1
--小F-- 2011-12-19
  • 打赏
  • 举报
回复
同步复制。去看看相关资料
好帅的一条鱼 2011-12-19
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 pengxuan 的回复:]
写个触发器,在更新或插入A库的表1时同时插入或更新到B库的表1
楼主的A库的表1,是update还是insert,还是两者都有
[/Quote]两者都有的
pengxuan 2011-12-19
  • 打赏
  • 举报
回复
写个触发器,在更新或插入A库的表1时同时插入或更新到B库的表1
楼主的A库的表1,是update还是insert,还是两者都有
jmx123456789 2011-12-19
  • 打赏
  • 举报
回复
触发器 事务

34,587

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧