Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
961 views
in Technique[技术] by (71.8m points)

oracle - Create table in Procedure

I have created the table "Risiko" which shouldntbe dynamic, i tought it would be that simple but it was not. How should i solve it? and what am i doing wrong?

CREATE OR REPLACE PROCEDURE TABLERISIKO IS


    BEGIN
       drop table risiko;
       CREATE TABLE Risiko
          (
          RNr       INTEGER,
          Projekt       INTEGER,
          Text       VARCHAR(25),
          Gruppe       INTEGER,
          Auswirkung    INTEGER,
          WKeit       INTEGER,
          Pruefdatum    DATE,
          PRIMARY KEY    (RNr),
          CONSTRAINT FKRisiko1 FOREIGN KEY (Projekt)
          REFERENCES Projekt(ProNr),
          CONSTRAINT FKRisiko2 FOREIGN KEY (Gruppe)
          REFERENCES Risikogruppe (RGNr),
          CONSTRAINT PosAuswirkung CHECK(Auswirkung >=0),
          CONSTRAINT WKeitProzent CHECK(WKeit>=0 AND WKeit<=100)
          );

       INSERT INTO Risiko 
       VALUES(1,1,'Anforderungenunklar',1,50000,30
       ,TO_DATE('25.01.06','DD.MM.YY'));
       INSERT INTO Risiko 
       VALUES(2,1,'Abnahmeprozess offen',2,30000,70
       ,TO_DATE('26.02.06','DD.MM.YY'));
       INSERT INTO Risiko
       VALUES(3,2,'Ansprechpartner wechseln',1,20000,80
       ,TO_DATE('06.05.06','DD.MM.YY'));
       INSERT INTO Risiko
       VALUES(4,2,'neue Entwicklungsumgebung',3,40000,20
       ,TO_DATE('05.10.06','DD.MM.YY'));

    END;

Thanks for help

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Because we can't write DDL statements directly in PL/SQL block, for example when we write

CREATE OR REPLACE PROCEDURE TABLERISIKO IS
BEGIN
    drop table risiko;
    CREATE TABLE Risiko
    (
        RNr       INTEGER,
        Projekt       INTEGER,
        Text       VARCHAR(25),
        Gruppe       INTEGER,
        Auswirkung    INTEGER,
        WKeit       INTEGER,
        Pruefdatum    DATE,
        PRIMARY KEY    (RNr),
        CONSTRAINT FKRisiko1 FOREIGN KEY (Projekt)
        REFERENCES Projekt(ProNr),
        CONSTRAINT FKRisiko2 FOREIGN KEY (Gruppe)
        REFERENCES Risikogruppe (RGNr),
        CONSTRAINT PosAuswirkung CHECK(Auswirkung >=0),
        CONSTRAINT WKeitProzent CHECK(WKeit>=0 AND WKeit<=100)
    );
    INSERT INTO Risiko VALUES(1,1,'Anforderungenunklar',1,50000,30 ,TO_DATE('25.01.06','DD.MM.YY'));
    INSERT INTO Risiko VALUES(2,1,'Abnahmeprozess offen',2,30000,70 ,TO_DATE('26.02.06','DD.MM.YY'));
    INSERT INTO Risiko VALUES(3,2,'Ansprechpartner wechseln',1,20000,80 ,TO_DATE('06.05.06','DD.MM.YY'));
    INSERT INTO Risiko VALUES(4,2,'neue Entwicklungsumgebung',3,40000,20 ,TO_DATE('05.10.06','DD.MM.YY'));
END;

It will not work But,

CREATE OR REPLACE PROCEDURE TABLERISIKO IS
BEGIN
    execute immediate 'drop table risiko';
    execute immediate 'CREATE TABLE Risiko
    (
        RNr       INTEGER,
        Projekt       INTEGER,
        Text       VARCHAR(25),
        Gruppe       INTEGER,
        Auswirkung    INTEGER,
        WKeit       INTEGER,
        Pruefdatum    DATE,
        PRIMARY KEY    (RNr),
        CONSTRAINT FKRisiko1 FOREIGN KEY (Projekt)
        REFERENCES Projekt(ProNr),
        CONSTRAINT FKRisiko2 FOREIGN KEY (Gruppe)
        REFERENCES Risikogruppe (RGNr),
        CONSTRAINT PosAuswirkung CHECK(Auswirkung >=0),
        CONSTRAINT WKeitProzent CHECK(WKeit>=0 AND WKeit<=100)
    )';
    execute immediate '
        INSERT INTO Risiko VALUES(1,1,||'''||'Anforderungenunklar'||'''||,1,50000,30 ,TO_DATE(||'''||'25.01.06'||'''||,||'''||'DD.MM.YY'||'''||));
        INSERT INTO Risiko VALUES(2,1,||'''||'Abnahmeprozess offen'||'''||,2,30000,70 ,TO_DATE(||'''||'26.02.06'||'''||,||'''||'DD.MM.YY'||'''||));
        INSERT INTO Risiko VALUES(3,2,||'''||'Ansprechpartner wechseln'||'''||,1,20000,80 ,TO_DATE(||'''||'06.05.06'||'''||,||'''||'DD.MM.YY'||'''||));
        INSERT INTO Risiko  VALUES(4,2,||'''||'neue Entwicklungsumgebung'||'''||,3,40000,20 ,TO_DATE(||'''||'05.10.06'||'''||,||'''||'DD.MM.YY'||'''||))';
    commit;
END;

It will run successfully.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...