2013-08-07 74 views
2

有人可以幫助我修復該代碼。它不會在PL/SQL上運行(SQLPLUS @ script.sql),給出 SP2-0552:未聲明綁定變量「NEW」。序列和觸發器PL/SQL腳本用於在表上自動生成ID

Script.SQL

prompt Creating Table SYSTEMDATALOG; 

declare 
    counter1 integer; 
    counter2 integer; 

begin 
    SELECT COUNT(*) INTO counter1 FROM ALL_TABLES WHERE TABLE_NAME='SYSTEMDATALOG' AND OWNER='MZMESDB'; 

    if counter1 = 1 then 
     DROP TABLE SYSTEMDATALOG; 
    end if; 

    SELECT COUNT(*) INTO counter2 FROM ALL_SEQUENCES WHERE SEQUENCE NAME='SEQSYSTEMDATALOG'; 

    if counter2 = 1 then 
     DROP SEQUENCE SEQSYSTEMDATALOGID; 
    endif; 

    CREATE TABLE "MZMESDB"."SYSTEMDATALOG" ("ID" INTEGER NOT NULL , 
         "DATETIME" DATE NOT NULL , 
         "TYPE" VARCHAR2(64) NOT NULL, 
         "SEVERITY" INTEGER NOT NULL, 
         "SOURCE" VARCHAR2(64) NOT NULL, 
         "USER" VARCHAR2(64) NOT NULL, 
         "MESSAGE" VARCHAR2(1024), PRIMARY KEY ("ID") VALIDATE); 


    CREATE SEQUENCE SEQSYSTEMDATALOGID; 

    CREATE OR REPLACE TRIGGER TRIGSYSTEMDATALOGID 
     BEFORE INSERT ON SYSTEMDATALOG 
     FOR EACH ROW 
      BEGIN 
       SELECT SEQSYSTEMDATALOGID.NEXTVAL INTO :NEW.ID FROM DUAL; 
      END TRIGSYSTEMDATALOGID; 
end; 
/

提前感謝任何形式的幫助。

[DEFINITIVE CODE]

提示創建表SYSTEMDATALOG;

申報 counter1 integer; counter2整數; SELECT COUNT(*)INTO counter1 FROM ALL_TABLES WHERE TABLE_NAME ='SYSTEMDATALOG'AND OWNER ='MZMESDB';其中,

if counter1 = 1 then 
    execute immediate 'DROP TABLE SYSTEMDATALOG'; 
end if; 

SELECT COUNT(*) INTO counter2 FROM ALL_SEQUENCES WHERE SEQUENCE_NAME='SEQSYSTEMDATALOG'; 

IF counter2 = 1 then 
    execute immediate 'DROP SEQUENCE SEQSYSTEMDATALOGID'; 
END IF; 

execute immediate 'CREATE TABLE "MZMESDB"."SYSTEMDATALOG" ( 
        "ID" INTEGER NOT NULL , 
        "DATETIME" DATE NOT NULL , 
        "TYPE" VARCHAR2(64) NOT NULL, 
        "SEVERITY" INTEGER NOT NULL, 
        "SOURCE" VARCHAR2(64) NOT NULL, 
        "USER" VARCHAR2(64) NOT NULL, 
        "MESSAGE" VARCHAR2(1024), PRIMARY KEY ("ID") VALIDATE)'; 

execute immediate 'CREATE SEQUENCE SEQSYSTEMDATALOGID'; 

execute immediate 'CREATE OR REPLACE TRIGGER TRIGSYSTEMDATALOGID 
      BEFORE INSERT ON SYSTEMDATALOG 
      FOR EACH ROW 
      BEGIN 
      SELECT SEQSYSTEMDATALOGID.NEXTVAL INTO :NEW.ID FROM DUAL; 
      END TRIGSYSTEMDATALOGID;'; 

end;

回答

2

一個問題是您正在混合PL/SQL(匿名PL/SQL塊)和DDL。雖然可以在PL/SQL中編寫DML語句(SELECT/INSERT/UPDATE/DELETE),但不能直接在PL/SQL中編寫DDL(CREATE/DROP等)。

您需要在匿名PL/SQL塊之外運行您的DDL語句或使用 EXECUTE IMMEDIATE '<statement>';

至於在:NEW上的錯誤,我認爲它會消失,當你解決其他問題。如果沒有,試着加入:

SET SCAN OFF

在SQL * PLUS文件的開頭。

+0

我正在使用EXECUTE IMMEDIATE,但沒有成功。我改變了腳本,因爲它不起作用... – Cox

+0

你在你的執行立即代碼中有一些語法錯誤: 1.一個'end if;'拼錯了''endif;' 2.一些字符串常量aren' t由單引號('SYSTEMDATALOG'應該是'SYSTEMDATALOG')。 –

+0

Endif確實是一個錯誤。我試過了,沒有引號,行爲也是一樣的。即使修復endif,我仍然會遇到「遇到符號」;「當期待以下某種情況時:如果」在PL/SQL處理器的最後一行。 – Cox

相關問題