简体   繁体   English

触发器中的SQL ORACLE错误

[英]SQL ORACLE error in trigger

I'm trying to create a trigger and I get the following error: 我正在尝试创建触发器,但出现以下错误:

Error(24,5): PLS-00103: Found the symbol "BEGIN" when it was expected one of the following: * & - + / at loop mod remainder rem and or || 错误(24,5):PLS-00103:预期出现以下情况之一时,找到了符号“ BEGIN”:*&-+ / at loop mod其余部分rem和或|| multiset. 多集。 I'm quite a newbie, thanks in advance! 我是一个新手,在此先感谢!

CREATE OR replace TRIGGER ins_livro
      instead OF INSERT ON viewLivros
      FOR EACH ROW
    DECLARE 
    cnt NUMBER := 10;
    biggestID Number;
    BEGIN 
        Select max(exemplar_id) into biggestID from exemplar;
        INSERT INTO livro (
                      id_livro, 
                      nome_livro,
                      id_editora,
                      ano,
                      Preco_Aluguer,
                      Preco_Compra,
                      Preco_Multa
                    ) 
        VALUES      (:new.id_livro, 
                     :new.nome_livro, 
                     :new.id_editora, 
                     :new.ano, 
                     :new.Preco_Aluguer, 
                     :new.Preco_Compra, 
                     :new.Preco_Multa 
                    ); 
        WHILE cnt > 0
        BEGIN
          SET biggestID = biggestID + 1
          INSERT INTO exemplar (
                      id_exemplar,
                      id_livro
                      )
          VALUES      (
                      :new.biggestID,
                      :new.id_livro
                      );
          SET cnt = cnt - 1
        END;
    END; 

You are missing the loop and end loop clauses: 您缺少loopend loop子句:

WHILE cnt > 0
LOOP
BEGIN
      SET biggestID = biggestID + 1
      INSERT INTO exemplar (
                  id_exemplar,
                  id_livro
                  )
      VALUES      (
                  :new.biggestID,
                  :new.id_livro
                  );
      SET cnt = cnt - 1
    END;
END LOOP;

You have a few mistakes in your syntax. 您的语法有一些错误。 Here it is corrected: 此处已更正:

CREATE OR REPLACE TRIGGER ins_livro INSTEAD OF
  INSERT ON viewLivros FOR EACH ROW DECLARE cnt NUMBER := 10;
  biggestID NUMBER;
  BEGIN
    SELECT MAX(exemplar_id) INTO biggestID FROM exemplar;
    INSERT
    INTO livro
      (
        id_livro,
        nome_livro,
        id_editora,
        ano,
        Preco_Aluguer,
        Preco_Compra,
        Preco_Multa
      )
      VALUES
      (
        :new.id_livro,
        :new.nome_livro,
        :new.id_editora,
        :new.ano,
        :new.Preco_Aluguer,
        :new.Preco_Compra,
        :new.Preco_Multa
      );
    WHILE cnt > 0
    LOOP
      BEGIN
        biggestID := biggestID + 1;
        INSERT
        INTO exemplar
          (
            id_exemplar,
            id_livro
          )
          VALUES
          (
            biggestID,
            :new.id_livro
          );
        cnt := cnt - 1;
      END;
    END LOOP;
  END; 

Issues: 问题:

You can't use this syntax: SET biggestID = biggestID + 1 您不能使用以下语法: SET biggestID = biggestID + 1

You need to use: biggestID := biggestID + 1; 您需要使用: biggestID := biggestID + 1;

Notice the SET keyword has been removed, = has been changed to := and the line has been terminated with a semicolon. 请注意, SET关键字已删除, =已更改为:=并且该行已用分号终止。

Also, there's no such thing as :new.biggestID . 另外,没有诸如:new.biggestID之类的东西。 That is a variable that you've defined in the trigger. 这是您在触发器中定义的变量。 It needed to be replaced by just biggestID . 它只需替换为biggestID

Finally, the BEGIN and END around this block were replaced with LOOP and END LOOP : 最后,此块周围的BEGINEND替换为LOOPEND LOOP

 SET biggestID = biggestID + 1
      INSERT INTO exemplar (
                  id_exemplar,
                  id_livro
                  )
      VALUES      (
                  :new.biggestID,
                  :new.id_livro
                  );
      SET cnt = cnt - 1

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

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