需求说明:培训记录处录入“外出培训”记录,保存后同步外出培训合同至“合同模块”培训记录表PX_Record创建触发器步骤一、新建触发器:[insert_htandAL]步骤二、当PeiXun_Record表
需求说明:培训记录处录入“外出培训”记录,保存后同步外出培训合同至“合同模块”
培训记录表PX_Record创建触发器
步骤一、新建触发器:[insert_htandAL]
步骤二、当PeiXun_Record表有INSERT,DELETE,UPDATE操作,则在触发器中执行INSERT,DELETE,UPDATE 操作将相关改变同步至合同表Emp_HT,必须用PX_Record表中Pxr_ID字段做唯一标识过滤
执行代码如下:
USE [XXXXX]
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[insert_htandAL]
ON [dbo].[PX_Record]
for INSERT,DELETE,UPDATE
AS
BEGIN
--01新增操作
if(exists(select 1 from inserted) and not exists(select 1 from deleted))
insert into Emp_HT (Comp_Code,Ht_code,Ht_empid,Ht_class,Ht_StartDate,Ht_EndDate,Ht_OldID,Ht_Num,Ht_Status,Ht_IsZhiXing,Ht_ContinueDesc,Ht_StopDesc
,Ht_EndDesc,Ht_desc,Ht_memo,createby,createtime,updateby,updatetime,Ht_Company,Ht_Year,G_htzdr,Ht_DocPath,Ht_Template,Ht_TemplatePath,G_pxid)
select a.Comp_Code,Emp_code,Pxr_empid,'02',G_sxDate,G_shxDate,null,1,0,1,null,null
,null,null,Pxr_memo,a.createby,a.createtime,a.updateby,a.updatetime,null,G_qdnx,null,null,null,null,Pxr_ID
from inserted a
inner join Emp_Base b on a.Pxr_empid=b.Emp_id
where isnull(pxr_class,0)=1 and G_ifpxxy='是'
END
--02删除操作
if(not exists(select 1 from inserted) and exists(select 1 from deleted))
begin
delete from Emp_HT
where G_pxid in(select Pxr_ID from deleted)
end
--03更新操作
if(exists(select 1 from inserted) and exists(select 1 from deleted))
begin
update Emp_HT set
Ht_StartDate=a.G_sxDate ,
Ht_EndDate=a.G_shxDate,
Ht_Year=a.G_qdnx,
updateby=a.updateby,
updatetime=a.updatetime
from inserted a
where G_pxid=a.Pxr_ID and isnull(a.pxr_class,0)=1 and a.G_ifpxxy='是'
end
备注说明:
触发器简介:
触发器是一种特殊的存储过程,它的执行不是由程序调用,也不是手动执行,而是由事件来触发。触发器是当对某一个表进行操作。例如:update、insert、delete这些操作的时候,系统会自动调用执行该表上对应的触发器。
-- 查询已存在的触发器
select * from sys.triggers;
select * from sys.objects where type = 'TR';
select * from sysobjects where xtype='TR'
-- 查看触发器触发事件对象
select a.type_desc,b.* from sys.trigger_events a
inner join sys.triggers b on a.object_id = b.object_id
where b.name = 'insert_hetongandAskLeave';
-- 查询触发器的 T-sql 文本
exec sp_helptext 'insert_hetongandAskLeave';
--禁用触发器
disable trigger trigger_Stu_InsteadOf on Student; -- trigger_Stu_InsteadOf 触发器名称
--启用触发器
enable trigger trigger_Stu_InsteadOf on Student; -- trigger_Stu_InsteadOf 触发器名称
--结束END--
本文标题: 触发器实现两表之间的INSERT,DELETE,UPDATE
本文链接: https://lsjlt.com/news/39067.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-23
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0