资料同步
时间:2011-12-19
来源:互联网
作者: good212164 发布时间: 2011-12-19
作者: ju523756055 发布时间: 2011-12-19
楼主的A库的表1,是update还是insert,还是两者都有
作者: pengxuan 发布时间: 2011-12-19
写个触发器,在更新或插入A库的表1时同时插入或更新到B库的表1
楼主的A库的表1,是update还是insert,还是两者都有
作者: good212164 发布时间: 2011-12-19
作者: fredrickhu 发布时间: 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
作者: pengxuan 发布时间: 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表
作者: dwarf471 发布时间: 2011-12-19
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28