需求說明:培訓記錄處錄入“外出培訓”記錄,保存后同步外出培訓合同至“合同模塊”
培訓記錄表PX_Record創建觸發器
步驟一、新建觸發器:[insert_htandAL]
步驟二、當PeiXun_Record表有INSERT,DELETE,UPDATE操作,則在觸發器中執行INSERT,DELETE,UPDATE 操作將相關改變同步至合同表Emp_HT,必須用PX_Record表中Pxr_ID字段做唯一標識過濾
執行代碼如下:
?
USE [XXXXX]
GO
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 觸發器名稱
?
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。