多部分标识符“i.charged_amount”可以是多部分标识符“i.charged_amount”。无法绑定
我正在尝试编写一个触发器,如果客户尝试购买某些东西,但如果他们超过了信用卡限额,则交易将被取消。
我的桌子是:
客户(名称,SSN,代码),
帐户(customer_code,acc_number,余额,费率),
信用卡(已发行、已过期、限额、余额、cc_number),
transactions(date,cc_number,charged_amount,conf_number,shop_code)
我写的是
CREATE TRIGGER check_balance ON transactions
FOR INSERT
AS
DECLARE @balance int, @limit int
SELECT @balance = balance, @limit = limit
FROM creditcard
INNER JOIN inserted i ON creditcard.cc_number = i.cc_number
IF (@balance + i.charged_amount > @limit)
BEGIN
ROLLBACK TRANSACTION
END
但是我得到了
消息 4104,级别 16,状态 1,过程 check_balance,第 10 行 无法绑定多部分标识符“i.charged_amount”。 其中第 10 行是 IF (@balance + i.charged_amount > @限制)
我知道该消息意味着我无法使用 i.* ,因为它的范围仅在 select..from
中。
我尝试像我一样使用引用新行,但在引用附近出现语法错误。我正在使用 SQL Server 2005。
有人可以帮助我吗?
I am trying to write a trigger where if a client tries to purchase something, but if they pass their credit card limit, the transaction is cancelled.
my tables are:
customers(name,ssn,code),
accounts(customer_code,acc_number,balance,rate),
creditcard(issued,expired,limit,balance,cc_number),
transactions(date,cc_number,charged_amount,conf_number,shop_code)
And what I wrote is
CREATE TRIGGER check_balance ON transactions
FOR INSERT
AS
DECLARE @balance int, @limit int
SELECT @balance = balance, @limit = limit
FROM creditcard
INNER JOIN inserted i ON creditcard.cc_number = i.cc_number
IF (@balance + i.charged_amount > @limit)
BEGIN
ROLLBACK TRANSACTION
END
But I get a
Msg 4104, Level 16, State 1, Procedure check_balance, Line 10
The multi-part identifier "i.charged_amount" could not be bound.
where line 10 is
IF (@balance + i.charged_amount >
@limit)
I know that the message means that I can't use i.* , cause the scope of it is only in the select..from
.
I tried using referencing new row as I, but I got a syntax error near referencing. I am using SQL Server 2005.
Could anyone please help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对标量变量的分配不适用于多行插入。
inserted
是一个可以包含任意行数的表。插入
甚至可能包含同一张信用卡的多个不同行,这些行单独可以,但加起来就会超出帐户限制。逻辑应该是这样的......
Your assignment to scalar variables won't work with multi row inserts.
inserted
is a table that may contain any number of rows.An
insert
may even contain multiple different rows for the same credit card that individually are OK but collectively would exceed the account limit.The logic would need to be something like...
试试这个:
try this:
我认为问题在于,当别名 i 仅在您为其添加别名的查询范围内时,您尝试使用别名 i。请将 IF 语句中的引用更改为 insert 而不是 i。
I think the problem is that you are trying to use the alias i when it is only in scope for the query you aliased it in. Kindly change the reference in the IF statement to inserted instead of i.