创建触发器阻止插入
我正在尝试执行以下触发器:
create trigger t23
on studies
after insert, update, delete
as
begin
REFERENCING NEW ROW NewStudent
FOR EACH ROW
WHEN (30 <= (SELECT SUM(credits) FROM Studies)
DELETE FROM NewStudent N
WHERE N.spnr = NewStudent.spnr
end
我正在尝试创建一个触发器,该触发器仅在学分 < 时插入学生。或 == 到“30”。 “Credits”是 int 类型。
我在尝试实现此触发器时遇到许多错误。我真的已经尝试了一切,但我别无选择。该领域的专家能否为我指出正确的方向?
I'm trying to execute the following trigger:
create trigger t23
on studies
after insert, update, delete
as
begin
REFERENCING NEW ROW NewStudent
FOR EACH ROW
WHEN (30 <= (SELECT SUM(credits) FROM Studies)
DELETE FROM NewStudent N
WHERE N.spnr = NewStudent.spnr
end
I'm trying to create a trigger which only inserts a student if the credits is < or == to '30'. The "Credits" is a type int.
I'm getting numerous errors trying to implement this trigger. I really have tried everything and i m out of options. Could someone who is expert in the field point me in the right direction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CREATE TRIGGER MSDN 文档完全满足您的要求:
这里的关键是
ROLLBACK TRANSACTION
,只需调整示例以满足您的需要即可。编辑:这应该可以满足您的需求,但我尚未对其进行测试,因此您的里程可能会有所不同。
另一个编辑,基于一些假设(请注意,我即时编写了此脚本,因为我现在无法测试它):
这样,当您在
dbo.results
表中插入记录时,约束会检查如果学生已通过课程,并在适当的情况下取消插入。但是,最好在应用程序层检查这些东西。The example "Using a DML AFTER trigger to enforce a business rule between the PurchaseOrderHeader and Vendor tables" in the CREATE TRIGGER MSDN documentation does exaclty what you're looking for:
The key here is
ROLLBACK TRANSACTION
, just adapt the example to suit your need and you're done.Edit: This should accomplish what you're looking for, but I have not tested it so your mileage may vary.
Another edit, based on some assumptions (please note I wrote this script on the fly since I can't test it right now):
This way when you insert records in the
dbo.results
table the constraint checks if the student has passed the course, and cancels the insertion if appropriate. However, it's better to check this things in the application layer.