感謝您關注此問題。我正在使用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.
謝謝!
家庭規模在客戶和收入中是否可以參考或您是否在兩個表中存儲相同的數據?我也沒有看到任何標準來確定哪些不符合條件?什麼決定了?這只是狀態嗎?你有多深入正規化你的數據模型? – 2013-03-05 01:38:31
我有表,要求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