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' でクライアントを挿入すると、Income はチェックされますが、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 UPDATEテーブルを変更しinsertedてから、更新を続行します。INSTEAD 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 に答える