2013-03-05 22 views
1

感謝您關注此問題。我正在使用MSSQL Server Express。 我想寫一個觸發器,根據他們的年齡和收入更新客戶的資格。我用年齡寫下了這部分,但我完全陷入資格部分。我聲明,可以選擇客戶誰是沒有資格根據自己的收入,週期性(月/年),家庭規模插入並更新時卡住觸發器

Select ClientID,c.HshldSize,c.MonthlyYearly,c.AnnualHshldIncome,i.SeniorMo, 
     StatusID,i.HshldSize 
from Clients c 
join IncomeEligibility i on c.HshldSize = i.HshldSize 
where c.HshldSize= i.HshldSize and c.AnnualHshldIncome >= i.SeniorMo 
     and StatusID in (1,2) 
     and c.CategCode = 'SR' and MonthlyYearly ='month' 

這種選擇顯示所有的客戶誰迴應沒有資格 例

ClientID HshldSize MonthlyYearly AnnualHshldIncome SeniorMo StatusID HshldSize 
    28  1    month   1095    977 2   1 
    51  1    month   1253    977 1   1 
    63  1    month   1300    977 1   1 
    73  1    month   1200    977 1   1 
    96  1    month   1300    977 1   1 
    101  1    month   1255    977 1   1 
    160  2    month   1800    1513 1   2 

IncomeEligibility看起來這

HshldSize AKGuidline WomanChildYr WomanChildMo SeniorYr SeniorMo PFDYr PFDMo 
1 9020 16687 1391 11726 977 878 73 
2 13970 25845 2154 18161 1513 1756 146 
3 18920 35002 2917 24596 2050 2634 219 
4 23870 44160 3680 31031 2586 3512 292 
5 28820 53317 4443 37466 3122 4390 365 
6 33770 62475 5206 43901 3658 5268 439 
7 38720 71632 5969 50336 4195 6146 512 
8 43670 80790 6733 56771 4731 7024 585 
9 48620 89947 7496 63206 5267 7902 658 
10 53570 99105 8259 69641 5803 8780 731 
11 58520 108262 9022 76076 6340 9658 804 
12 63470 117420 9785 82511 6876 10536 878 
13 68420 126577 10548 88946 7412 11414 951 
14 73370 135735 11311 95381 7948 12292 1024 
15 78320 144892 12074 101816 8485 13170 1097 
16 83270 154050 12838 108251 9021 14048 1170 
17 88220 163207 13601 114686 9557 14926 1243 
18 93170 172365 14364 121121 10093 15804 1317 
19 98120 181522 15127 127556 10630 16682 1390 
20 103070 190680 15890 133991 11166 17560 1463 

觸發應該設置StatusID = 5的clientrow如果他們都沒有資格。 到目前爲止,我有這個觸發

create trigger tr_EligebilityCheck 
on dbo.Clients 
FOR INSERT,UPDATE 
as 
/*Check if Senior not eligible by age*/ 
If (select CategCode from inserted)='SR' 
declare 
@DOB date 
SET @DOB = (select dob from inserted) 
if DATEDIFF(YEAR,@DOB,GETDATE())<60 

BEGIN 
Update Clients 
set StatusID = 5 
From Clients c, inserted i 
where c.CategCode = 'SR' and i.ClientID = C.ClientID 
END 

/*Check if Children eligebel by age*/ 
If (select CategCode from inserted)='CH' 
declare 
@DOBCH date 
SET @DOBCH = (select dob from inserted) 
if DATEDIFF(YEAR,@DOBCH,GETDATE()) >=6 

BEGIN 
Update Clients 
set StatusID = 5 
From Clients c, inserted i 
where c.CategCode ='CH' and i.ClientID = C.ClientID 
END 

,但不知道如何通過收入增加檢查,請幫助,如果你有一個想法如何做到這一點。也看起來像我的觸發器不工作時拋出錯誤當我正在運行插入新reccourd

Msg 512, Level 16, State 1, Procedure tr_EligebilityCheck, Line 6 
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. 

謝謝!

+0

家庭規模在客戶和收入中是否可以參考或您是否在兩個表中存儲相同的數據?我也沒有看到任何標準來確定哪些不符合條件?什麼決定了?這只是狀態嗎?你有多深入正規化你的數據模型? – 2013-03-05 01:38:31

+0

我有表,要求IncomeEligibility它上市CategoryGode收入(「SR」和「CH」)根據家庭規模(從1到20)。表看起來像創建表IncomeEligibility ( HshldSize詮釋不爲空, AKGuidline INT NOT NULL, WomanChildYr詮釋不爲空, WomanChildMo詮釋不爲空, SeniorYr詮釋不爲空, SeniorMo詮釋不爲空, PFDYr詮釋不爲空, PFDMo詮釋不爲空 ); IncomeEligibility沒有任何主鍵 – Andrey 2013-03-05 01:41:49

回答

1

如果這是識​​別誰不符合條件的唯一方法,則準備好選擇語句。所以你只需要更新那些標準中的那些。做一個子查詢(我做了而不是 SQLFiddle)。您可以將更新置於觸發器所需的任何位置。

UPDATE Clients 
SET 
theColName = 'value' 
WHERE 
ClientID IN (Select ClientID 
      from Clients c 
      join IncomeEligibility i 
        on c.HshldSize = i.HshldSize 
      where c.HshldSize= i.HshldSize 
        and c.AnnualHshldIncome >= i.SeniorMo 
        and StatusID in (1,2) 
        and c.CategCode = 'SR' 
        and MonthlyYearly ='month') 

的誤差約爲該行If (select CategCode from inserted)='SR'選擇CategCode查詢返回多個值,所以等號是錯誤的操作。雖然我會把'SR'放入一個變量中,但試試這種方法。

if @var IN (select CategCode from inserted) 

該語句查找結果集中的值IN,而不是查看它是否等於多個值。看看你的查詢結果,看看它是否返回多個CategCodes,這就是錯誤所說的。

+0

您應該使用「插入」表加入您的子查詢,只查看實際更新的客戶端。 – 2013-03-05 02:14:31

+0

它的工作原理,但現在它觸發了我在DB中的另一個觸發器的錯誤。消息217,級別16,狀態1,過程WICUpdateStatusId4,第4行 超過了最大存儲過程,函數,觸發器或視圖嵌套級別(限制32)。 – Andrey 2013-03-05 02:18:27

+0

如果我刪除了它的觸發器,但是我必須有那個觸發器 – Andrey 2013-03-05 02:21:58