多部分标识符“i.charged_amount”可以是多部分标识符“i.charged_amount”。无法绑定

发布于 2024-10-06 04:15:32 字数 983 浏览 0 评论 0原文

我正在尝试编写一个触发器,如果​​客户尝试购买某些东西,但如果他们超过了信用卡限额,则交易将被取消。

我的桌子是:
客户(名称,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏末的微笑 2024-10-13 04:15:32

对标量变量的分配不适用于多行插入。 inserted 是一个可以包含任意行数的表。

插入甚至可能包含同一张信用卡的多个不同行,这些行单独可以,但加起来就会超出帐户限制。

逻辑应该是这样的......

IF EXISTS
(
SELECT c.cc_number
FROM creditcard c
INNER JOIN inserted i ON c.cc_number = i.cc_number
GROUP BY c.cc_number, c.limit, c.balance
HAVING c.balance + SUM(i.charged_amount) > c.limit
)

ROLLBACK...

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...

IF EXISTS
(
SELECT c.cc_number
FROM creditcard c
INNER JOIN inserted i ON c.cc_number = i.cc_number
GROUP BY c.cc_number, c.limit, c.balance
HAVING c.balance + SUM(i.charged_amount) > c.limit
)

ROLLBACK...
一影成城 2024-10-13 04:15:32

试试这个:

create trigger check_balance on transactions

for insert
as

IF EXISTS (SELECT 1 
           FROM creditcard 
               INNER JOIN inserted i ON creditcard.cc_number = i.cc_number 
           GROUP BY c.cc_number
           HAVING MIN(creditcard.balance)+SUM(i.charged_amount)>MIN(creditcard.limit)
          )
BEGIN
    raiserror ('bad limit found',16,1)
    ROLLBACK TRANSACTION
    return
END

try this:

create trigger check_balance on transactions

for insert
as

IF EXISTS (SELECT 1 
           FROM creditcard 
               INNER JOIN inserted i ON creditcard.cc_number = i.cc_number 
           GROUP BY c.cc_number
           HAVING MIN(creditcard.balance)+SUM(i.charged_amount)>MIN(creditcard.limit)
          )
BEGIN
    raiserror ('bad limit found',16,1)
    ROLLBACK TRANSACTION
    return
END
剩余の解释 2024-10-13 04:15:32

我认为问题在于,当别名 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文