SQL 触发器无法正常工作
这是我拥有的 2 个表,我想实现一个触发器,客户在一家银行的帐户不能超过 5 个,但总共可以超过 5 个。
CREATE TABLE ACCOUNT(
ACCOUNT_NO VARCHAR(20) NOT NULL,
BALANCE REAL,
BANK_CODE VARCHAR(20),
BRANCH_NO VARCHAR(25),
ACCOUNT_CODE VARCHAR(20),
PRIMARY KEY(ACCOUNT_NO),
);
CREATE TABLE ACCOUNT_CUSTOMER(
CUS_NO VARCHAR(20) NOT NULL,
ACCOUNT_NO VARCHAR(20) NOT NULL,
PRIMARY KEY(CUS_NO,ACCOUNT_NO),
FOREIGN KEY(ACCOUNT_NO) REFERENCES ACCOUNT(ACCOUNT_NO),
);
这是我写的触发器,但我总共不能创建超过 5 个帐户,因为它检查所有银行而不是单个银行的所有帐户。
CREATE TRIGGER TRIGGER1
ON ACCOUNT_CUSTOMER
FOR INSERT,UPDATE
AS BEGIN
DECLARE @COUNT INT
DECLARE @CUS_NO VARCHAR(20)
SELECT @COUNT=COUNT(AC.ACCOUNT_NO)
FROM INSERTED I,ACCOUNT_CUSTOMER AC
WHERE I.CUS_NO=AC.CUS_NO
GROUP BY(AC.CUS_NO)
IF @COUNT>5
ROLLBACK TRANSACTION
END
据我猜测,问题出在 GRUBBY 函数内。
here are the 2 tables i have, i want to implement an trigger that customer cannot have more than 5 accounts from a one bank, but can have more than 5 in total.
CREATE TABLE ACCOUNT(
ACCOUNT_NO VARCHAR(20) NOT NULL,
BALANCE REAL,
BANK_CODE VARCHAR(20),
BRANCH_NO VARCHAR(25),
ACCOUNT_CODE VARCHAR(20),
PRIMARY KEY(ACCOUNT_NO),
);
CREATE TABLE ACCOUNT_CUSTOMER(
CUS_NO VARCHAR(20) NOT NULL,
ACCOUNT_NO VARCHAR(20) NOT NULL,
PRIMARY KEY(CUS_NO,ACCOUNT_NO),
FOREIGN KEY(ACCOUNT_NO) REFERENCES ACCOUNT(ACCOUNT_NO),
);
heres the trigger i wrote but i can't create more than 5 accounts in total because it checks for all the accounts in all the banks rather than a single bank.
CREATE TRIGGER TRIGGER1
ON ACCOUNT_CUSTOMER
FOR INSERT,UPDATE
AS BEGIN
DECLARE @COUNT INT
DECLARE @CUS_NO VARCHAR(20)
SELECT @COUNT=COUNT(AC.ACCOUNT_NO)
FROM INSERTED I,ACCOUNT_CUSTOMER AC
WHERE I.CUS_NO=AC.CUS_NO
GROUP BY(AC.CUS_NO)
IF @COUNT>5
ROLLBACK TRANSACTION
END
THE PROBLEM IS WITHIN THE GROUPBY FUNCTION AS I GUESS.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这很容易在有约束的情况下实现:
编辑:有时触发器不会触发。只有受信任的约束才能 100% 保证数据完整性。
要插入,我将使用 Numbers 表:
我将使用触发器来禁止修改 BANK_CODE。
this is easy to implement with constraints:
Edit: sometimes triggers do not fire. Only trusted constraints 100% guarantee data integrity.
To insert, I would use Numbers table:
I would use a trigger to prohibit modifications of BANK_CODE.
我会尝试这样的操作:
将触发器的这一部分替换
为:
还要考虑 INSERTED 表中可能有多个记录。如果这些记录适用于多个客户,并且任何客户导致此触发器回滚事务,则不会应用那些未违反您的规则的客户的更新。这可能永远不会发生(如果您的应用程序从不一次更新多个客户的记录),或者可能是预期的行为。
I would try something like this:
Replace this part of your trigger
with this:
Also consider that the INSERTED table may have multiple records in it. If those records are for more than one customer and any of the customers causes this trigger to rollback the transaction, then the updates for those customers that did not violate your rule will not be applied. This may never happen (if your application never updates records for more than one customer at a time), or may be the intended behavior.
尝试此操作而不是触发器中的当前查询。我认为这可能有效。
我的语法可能有点偏差,但你已经了解了总体思路。
Try this instead of the current query in your trigger. I think that this might work.
My syntax might be a bit off but you get the general idea.
您的查询的问题在于您仅通过唯一的客户标识符进行搜索。
您的查询必须同时搜索唯一客户和银行标识符的计数。我将把确切的查询留给您,但这就是您想要的伪代码:
这将返回客户+银行组合存在的次数。这就是你想要的极限。
The trouble with your query is that you are only searching by unique customer identifier.
Your query must search a count of a unique customer AND bank identifier together. I'll leave the exact query to you, but here's what you want in pseudocode:
This will return how many times a customer + bank combination exist. That's the limit you want.
感谢您的回答,在经历了所有这些之后,我想出了这个解决方案。我插入了一个嵌套查询,它会给我银行代码,通过该代码我可以得到计数
Thanks for the answers, after going through all, i came up with this solution. I inserted a nested query taht will give me the bankcode and by that code i get the count