SQL ORACLE 触发器错误

SQL ORACLE error in trigger

我正在尝试创建触发器,但出现以下错误:

Error(24,5): PLS-00103: Found the symbol "BEGIN" when it was expected one of the following: * & - + / at loop mod remainder rem and or || 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; 

您缺少 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;

你的语法有一些错误。此处更正:

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; 

问题:

您不能使用此语法:SET biggestID = biggestID + 1

您需要使用:biggestID := biggestID + 1;

请注意,SET 关键字已被删除,= 已更改为 :=,该行已以分号结束。

此外,没有 :new.biggestID 这样的东西。这是您在触发器中定义的变量。它只需要替换为 biggestID.

最后,这个块周围的BEGINEND被替换为LOOPEND LOOP

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