使用触发器更改插入的值 [英] Change inserted value with trigger
问题描述
几周前我才刚开始学习SQL,我正在尝试制作一个触发器,如果插入的值小于10,它会将插入的值更改为10.我现在搜索了4h,发现了很多答案,但没有一个好(对我来说).我真的不明白问题出在哪里. 这是代码:
I've just started to learn SQL a few weeks ago and I'm trying to make a trigger which changes the inserted value into 10 if it's smaller than 10. I searched for 4h now and I've found a lot of answers but none was good(for me). I really don't understand where the problem is. Here is the code:
CREATE OR REPLACE TRIGGER NumberOfBooks
BEFORE INSERT
ON Book
FOR EACH ROW
BEGIN
IF new.nobook < 10
THEN
SET new.nobook = 10;
END IF;
END;
推荐答案
在Oracle的触发器语法中,新插入的记录由:new
而不是new
引用(请注意冒号).另外,SET
是更新语句的一部分,而不是设置字段值的方法-这些是通过简单的赋值完成的,但是请注意,这些是通过:=
而不是=
完成的.
因此,您的触发器应显示为:
In Oracle's trigger syntax the newly inserted record is referred to by :new
, not new
(notice the colon). Additionally, SET
is a part of an update statement, not a way to set field values - those are done by simple assignments, but note that these are done with :=
rather than =
.
So, your trigger should read:
CREATE OR REPLACE TRIGGER NumberOfBooks
BEFORE INSERT
ON book
FOR EACH ROW
BEGIN
IF :new.nobook < 10
THEN
:new.nobook := 10;
END IF;
END;
这篇关于使用触发器更改插入的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!