2 q535175584 q535175584 于 2014.09.18 11:07 提问

修改触发器 将删除后再插入 改成修改 大神求救

USE [IBR_App_HR]
GO
/****** Object: Trigger [dbo].[Trigger_SA_Employee] Script Date: 09/18/2014 10:47:23 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER trigger [dbo].[Trigger_SA_Employee]
on [dbo].[HR_HM_Employee]
for insert,update,delete
as
if exists
(select A.Id,A.Name,A.CompanyCode,A.Code,A.EntryTime,A.DimissionTime from inserted A,deleted b where A.Id = B.Id)
begin
if exists (select dbo.HR_SA_Employee.Id from dbo.HR_SA_Employee,inserted where dbo.HR_SA_Employee.Id = inserted.Id)
delete dbo.HR_SA_Employee from dbo.HR_SA_Employee,inserted where dbo.HR_SA_Employee.Id = inserted.Id;
insert into dbo.HR_SA_Employee (Id,Name,CompanyCode,Code,EntryTime,DimissionTime,Remark,Status)

(select Id, Name,CompanyCode,Code,EntryTime,DimissionTime as DimissionTime,'Modify',Status from inserted)
end

else
begin
if exists (select Id from inserted)
begin
insert into dbo.HR_SA_Employee (Id,Name,CompanyCode,Code,EntryTime,DimissionTime,Remark,Status)

(select Id, Name,CompanyCode,Code,EntryTime,DimissionTime ,'Insert',Status from inserted);
end
else
begin
update dbo.HR_SA_Employee set Remark = 'Delete' from dbo.HR_SA_Employee,deleted where dbo.HR_SA_Employee.id = deleted.id;
end
end

有两个表 HR_HM_Employee 这个是员工表 HR_SA_Employee 这个是薪资表 我需要员工表里面的Id,Name,CompanyCode,Code,EntryTime,DimissionTime,Remark,Status 这些字段修改后 HR_SA_Employee 这个薪资表里面的这些字段也修改 现在的触发器是 删掉薪资表里面的数据 再插入一条 我现在是要修改 不是删除后再插入 大神求救啊

1个回答

fanxue820
fanxue820   2014.09.19 12:59

你可以把触发器改成更新触发器啊,当员工表中的信息修改时就更新薪资表啊

Csdn user default icon
上传中...
上传图片
插入图片