简体   繁体   English

Oracle11g 中的 SQL 语句被忽略和 ORA-00922

[英]SQL statement ignored and ORA-00922 in Oracle11g

I am getting two errors for my trigger and haven't been able to figure out where the problem is, any help is much appreciated :) The trigger should fire on the update of ListPrice column.我的触发器出现两个错误,并且无法找出问题出在哪里,非常感谢任何帮助:) 触发器应该在更新 ListPrice 列时触发。 The trigger should not allow a new list price to be less than StandardCost + 20%.触发器不应允许新标价低于 StandardCost + 20%。 This trigger should allow any price increase.IT should correctly discard changes if the price is too low or allow the update if the price is not too low.此触发器应允许任何价格上涨。如果价格过低,IT 应正确放弃更改,或者如果价格不太低则允许更新。

First creating this table:首先创建这个表:

CREATE TABLE Product (
ProductID NUMBER NOT NULL PRIMARY KEY,
ListPrice NUMBER NOT NULL,
StandardCost NUMBER NOT NULL,
ProductDesc varchar2(20) NOT NULL
);

Then inserting this data into the table:然后将此数据插入表中:

INSERT INTO product VALUES(1, 20,10,'A');
INSERT INTO product VALUES(2, 21,12,'B');
INSERT INTO product VALUES(3, 22,14,'C');
INSERT INTO product VALUES(4, 23,16,'D');
INSERT INTO product VALUES(5, 40,19,'E');
INSERT INTO product VALUES(6, 22,10,'F');
INSERT INTO product VALUES(7, 25,21,'G');
INSERT INTO product VALUES(8, 29,22,'H');
INSERT INTO product VALUES(9, 25,23,'I');
INSERT INTO product VALUES(10, 30,25,'J');

Here is the code for the trigger where I am getting the error:这是我收到错误的触发器代码:

CREATE OR REPLACE TRIGGER Product_Price_Check
BEFORE UPDATE OF listprice ON product
FOR EACH ROW
DECLARE
sCost NUMBER(10,5);
BEGIN
sCost := (:old.standardcost + (:old.standardcost*0.2));
IF((:new.listprice) > (:old.listprice))
THEN
SET :old.listprice := :new.listprice;
END IF;
IF (:new.listprice < (sCost))
THEN
RAISE_APPLICATION_ERROR(-20101,'cannot update as price is less');
END IF;
END;
/

These are the error codes:这些是错误代码:

Error(6,1): PL/SQL: SQL Statement ignored错误(6,1):PL/SQL:SQL 语句被忽略
Error(6,5): PL/SQL:bORA-00922: missing or invalid option错误 (6,5): PL/SQL:bORA-00922: 缺少或无效的选项

I understand that don't need to user SET keyword in the code.我知道不需要在代码中使用SET关键字。 Along with that we cannot assign value to old.除此之外,我们不能为旧值赋值。 I have commented that part in below code.我在下面的代码中评论了该部分。 Further, you can completely comment first if block as you only need to raise error if your conditions are not satisfied otherwise it will allow the update.此外,您可以首先完全评论if块,因为如果您的条件不满足,您只需要引发错误,否则它将允许更新。

create or replace trigger product_price_check before
    update of listprice on product
    for each row
declare
    scost   number(
        10,5
    );
begin
    scost   :=:old.standardcost + (:old.standardcost * 0.2 );

    --if :new.listprice > :old.listprice then
        -- set :old.listprice   := :new.listprice;
    --end if;

    if (:new.listprice < ( scost ) ) then
        raise_application_error(
            -20101,
            'cannot update as price is less'
        );
    end if;

end;
/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM