感谢您查看此问题。我正在使用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
如果clientrow不符合条件,触发器应设置StatusID = 5。 到目前为止,我有这个触发器
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 :(得分: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,这就是错误所说的内容。