Change inserted value with trigger

◇◆丶佛笑我妖孽 提交于 2020-03-24 04:54:58

问题


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;

回答1:


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;


来源:https://stackoverflow.com/questions/20021644/change-inserted-value-with-trigger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!