Sql 插入和更新时被触发器卡住
谢谢你关注这个问题。我正在使用MSSQL Server Express。 我正在尝试编写一个触发器,它将根据客户的年龄和收入更新客户的资格。我写了一段很长的时间,但我完全被资格部分卡住了。我有一份声明,可以根据收入、周期月/年、家庭规模选择不符合条件的客户Sql 插入和更新时被触发器卡住,sql,triggers,sql-server-2008-r2,Sql,Triggers,Sql Server 2008 R2,谢谢你关注这个问题。我正在使用MSSQL Server Express。 我正在尝试编写一个触发器,它将根据客户的年龄和收入更新客户的资格。我写了一段很长的时间,但我完全被资格部分卡住了。我有一份声明,可以根据收入、周期月/年、家庭规模选择不符合条件的客户 Select ClientID,c.HshldSize,c.MonthlyYearly,c.AnnualHshldIncome,i.SeniorMo, StatusID,i.HshldSize from Clients c j
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
不可满足性看起来是这样的
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
但不知道如何增加收入支票,请帮助,如果你有一个如何做到这一点的想法。看起来我的触发器不起作用,当我插入新记录时抛出错误
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.
谢谢大家! 如果这是确定谁不合格的唯一方法,那么您就准备好select语句。所以你只需要更新那些符合标准的。用我没有处理过的子查询来完成。您可以将更新放在触发器中需要的任何位置
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')
如果select CategCode from inserted='SR'select CategCode查询返回多个值,则错误与此行有关,因此等号是错误的运算符。虽然我会把'SR'放在一个变量中,但请尝试这种方法
if @var IN (select CategCode from inserted)
该语句在resultset中查找该值,而不是查看它是否等于多个值。查看您的查询结果,看看它是否返回了多个categcode,这就是错误所在。如果这是识别不符合条件的人的唯一方法,那么您已经准备好select语句。所以你只需要更新那些符合标准的。用我没有处理过的子查询来完成。您可以将更新放在触发器中需要的任何位置
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')
如果select CategCode from inserted='SR'select CategCode查询返回多个值,则错误与此行有关,因此等号是错误的运算符。虽然我会把'SR'放在一个变量中,但请尝试这种方法
if @var IN (select CategCode from inserted)
该语句在resultset中查找该值,而不是查看它是否等于多个值。查看您的查询结果,看看它是否返回了多个Categcode,这就是错误所在。客户中的家庭规模和不可满足性是一个参考,还是在两个表中存储相同的数据?我也看不到任何标准来确定哪些是不合格的?是什么决定了这一点?这只是现状吗?您对数据模型的规范化有多深?我有一个名为IncomeEligibility的表,它是根据家庭规模从1到20的分类代码“SR”和“CH”列出的收入。该表看起来像create table IncomeEligibility HshldSize int not null、AKGuidline int not null、WomanChildYr int not null、WomanChildMo int not null,SeniorYr int不为空、SeniorMo int不为空、PFDYr int不为空、PFDMo int不为空;IncomeEligibility没有任何主键,客户中的家庭规模和IncomeEligibility是一个参考,或者您在两个表中存储的数据相同?我也看不到任何标准来确定哪些是不合格的?是什么决定了这一点?这只是现状吗?您对数据模型的规范化有多深?我有一个名为IncomeEligibility的表,它是根据家庭规模从1到20的分类代码“SR”和“CH”列出的收入。该表看起来像create table IncomeEligibility HshldSize int not null、AKGuidline int not null、WomanChildYr int not null、WomanChildMo int not null,SeniorYr int不为空、SeniorMo int不为空、PFDYr int不为空、PFDMo int不为空;IncomeEligibility没有任何主键。您应该将子查询与插入的表连接起来,以便只查看实际正在更新的客户端。它可以工作,但现在它在DB中的另一个触发器上触发错误。Msg 217,级别16,状态1,过程WICUpdateStatusId4,第4行最大存储过程、函数、触发器或视图嵌套级别超出限制32。如果删除该触发器,它将正常工作,但我必须让该触发器检查在上述代码中是否多次触发该触发器。触发器在更新时触发,然后它执行一个触发触发器的更新,插入时也是如此。Insert FIELS trigger、update ACCOUNCES和FIELS trigger您应该将子查询与插入的表连接起来,以便只查看实际正在更新的客户端。它可以工作,但现在它在DB中的另一个触发器上触发错误。Msg 217,级别16,状态1,过程WICUpdateStatusId4,第4行最大存储过程、函数、触发器或视图嵌套级别超出限制32。如果删除该触发器,它将正常工作,但我必须让该触发器检查在上述代码中是否多次触发该触发器。触发器在更新时触发,然后它执行一个触发触发器的更新,插入时也是如此。Ins ert触发、更新发生并触发触发器