0

现在触发器看起来像这样,它部分工作

我的触发器看起来像这样

   ALTER  trigger [dbo].[tr_EligebilityCheck]
on [dbo].[Clients]
 for INSERT,update
as 

BEGIN
UPDATE Clients 
SET 
StatusID = 5
WHERE 
ClientID IN (Select ClientID
            from Clients c 
            join inserted --ADD THIS JOIN
            on inserted.ClientID = c.ClientID
            join IncomeEligibility i 
            on c.HshldSize = i.HshldSize
            where StatusID in (1,2) 
            and  WIC = 0
            and (c.CategCode = 'SR' 
                and ((MonthlyYearly = 'month' and c.AnnualHshldIncome >= i.SeniorMo) 
                or (c.AnnualHshldIncome >= i.SeniorYr and MonthlyYearly ='year'))
                and DATEDIFF(YEAR,DOB,GETDATE()) < 60)
            or
                (c.CategCode = 'CH' 
                and ((MonthlyYearly = 'month' and c.AnnualHshldIncome >= i.WomanChildMo) 
    or (c.AnnualHshldIncome >= i.WomanChildYr and MonthlyYearly ='year'))
                and DATEDIFF(YEAR,DOB,GETDATE()) > 6))

update Clients
set StatusID = 4 
where WIC =1
from Clients --ADD THIS FROM STATEMENT
join inserted --ADD THIS JOIN
on inserted.ClientID = Clients.ClientID

END

当我使用 CategCode = 'SR' 插入客户端时,它仅检查 DOB 并在客户端小于 60 岁时触发,但如果客户端年龄较大,则不检查

and ((MonthlyYearly = 'month' and c.AnnualHshldIncome >= i.SeniorMo) 
                    or (c.AnnualHshldIncome >= i.SeniorYr and MonthlyYearly ='year'))

如果我插入带有 CategCode = 'CH' 的客户端,它会检查收入但没有检查 DOB。

4

1 回答 1

1

只要您对主键有正确的引用,我不明白您为什么需要INSTEAD OF触发器。似乎没有任何情况会阻止插入或更新,对吗?您只想确定 StatusID 的值。没有理由必须在更新之前完成。

我相信您更新太多行的原因是您没有将触发器限制为仅inserted表中的那些行。尝试向触发器添加联接,如下所示:

ALTER  trigger [dbo].[tr_EligebilityCheck]
on [dbo].[Clients]
 for INSERT,update
as 

BEGIN
UPDATE Clients 
SET 
StatusID = 5
WHERE 
ClientID IN (Select ClientID
            from Clients c 
            join inserted --ADD THIS JOIN
            on inserted.ClientID = c.ClientID
            join IncomeEligibility i 
            on c.HshldSize = i.HshldSize
            where StatusID in (1,2) 
            and  WIC = 0
            and (c.CategCode = 'SR' 
                and ((MonthlyYearly = 'month' and c.AnnualHshldIncome >= i.SeniorMo) 
                or (c.AnnualHshldIncome >= i.SeniorYr and MonthlyYearly ='year'))
                and DATEDIFF(YEAR,DOB,GETDATE()) < 60)
            or
                (c.CategCode = 'CH' 
                and ((MonthlyYearly = 'month' and c.AnnualHshldIncome >= i.WomanChildMo) 
    or (c.AnnualHshldIncome >= i.WomanChildYr and MonthlyYearly ='year'))
                and DATEDIFF(YEAR,DOB,GETDATE()) > 6))

update Clients
set StatusID = 4 
where WIC =1
from Clients --ADD THIS FROM STATEMENT
join inserted --ADD THIS JOIN
on inserted.ClientID = Clients.ClientID

END

如果您确实想使用触发器,请注意以下INSTEAD OF几点:INSTEAD OF与. 更改表,然后继续更新。完全取消插入或更新,这意味着您需要明确地重新编写它。我有一个下面的例子。BEFORE UPDATEBEFORE UPDATEinsertedINSTEAD OF

此外,如果您想使用INSTEAD OF触发器,则需要单独的INSERTUPDATE触发器,或者您需要将查询编写为MERGE语句。我将INSERT在下面的示例中使用:

ALTER  trigger [dbo].[tr_EligebilityCheck]
on [dbo].[Clients]
INSTEAD OF INSERT
as 

BEGIN

--First, set StatusID in the inserted table
UPDATE inserted
SET 
StatusID = 5
WHERE 
ClientID IN (Select ClientID
            from Clients c 
            join IncomeEligibility i 
            on c.HshldSize = i.HshldSize
            where StatusID in (1,2) 
            and  WIC = 0
            and (c.CategCode = 'SR' 
                and ((MonthlyYearly = 'month' and c.AnnualHshldIncome >= i.SeniorMo) 
                or (c.AnnualHshldIncome >= i.SeniorYr and MonthlyYearly ='year'))
                and DATEDIFF(YEAR,DOB,GETDATE()) < 60)
            or
                (c.CategCode = 'CH' 
                and ((MonthlyYearly = 'month' and c.AnnualHshldIncome >= i.WomanChildMo) 
    or (c.AnnualHshldIncome >= i.WomanChildYr and MonthlyYearly ='year'))
                and DATEDIFF(YEAR,DOB,GETDATE()) > 6));

update inserted
set StatusID = 4 
where WIC =1;

--Once the inserted table looks right, proceed with the insert
--You need to explicitly write an insert statement, or nothing will happen
INSERT INTO [dbo].[Clients]
  <column_list>
SELECT <column_list>
FROM inserted;
于 2013-03-06T03:27:37.810 回答