这个SQL代码有什么错误?

时间:2016-11-23 13:40:01

标签: database stored-procedures dynamic-sql oracle12c

我正在尝试在表A中插入值,我想检查值是否存在print' value是否存在'如果没有创建新表并向表B添加值。我将尝试下面的代码,但它会给出编译错误

2 个答案:

答案 0 :(得分:0)

你犯了一些错误。这是第二个版本:D请试试这个。

SET SERVEROUTPUT ON;

CREATE OR REPLACE PROCEDURE add_job ( job_id VARCHAR2, job_title VARCHAR2 )
IS
   var   VARCHAR2 ( 4000 );
BEGIN
   IF ( SELECT job_id
          FROM jobs
         WHERE job_id = job_id )
         IS NOT NULL
   THEN
      DBMS_OUTPUT.put_line ( 'Value already Exits' );
   ELSE

         -- insert new job in jobs table---
         INSERT INTO jobs
              VALUES ( job_id,
                       job_title,
                       NULL,
                       NULL );

         EXECUTE IMMEDIATE
            'CREATE TABLE ' || 'EMP_' || UPPER ( job_id ) || '( 
                  EMPLOYEE_ID   NUMBER(6,0) NOT NULL,     
                  FIRST_NAME   VARCHAR2(20) ,
                  LAST_NAME     VARCHAR2(25) NOT NULL,
                  SALARY     NUMBER(8,2),
                  DEPARTMENT_ID  NUMBER(4,0),
                  CONSTRAINT fk_e' || UPPER ( job_id ) || ' 
                          FOREIGN KEY (EMPLOYEE_ID)
                          REFERENCES EMPLOYEES(EMPLOYEE_ID)
              )';
    END IF;
      EXCEPTION
         WHEN OTHERS
         THEN
            DBMS_OUTPUT.put_line ( 'ERROR' );


END;
/

新版本:D并尝试不使用像VAR这样的用户变量或名称如“table”

的表
CREATE OR REPLACE PROCEDURE add_job ( job_id VARCHAR2, job_title VARCHAR2 )
IS
   err_msg       VARCHAR2 ( 4000 );
   stmt          VARCHAR2 ( 4000 );
   flg_cnt_job   NUMBER;
BEGIN
   SELECT COUNT ( * )
     INTO flg_cnt_job
     FROM jobs
    WHERE job_id = job_id;

   IF ( flg_cnt_job > 0 )
   THEN
      DBMS_OUTPUT.put_line ( 'Value already exits' );
   ELSE
      -- insert new job in jobs table---
      INSERT INTO jobs
           VALUES ( job_id,
                    job_title,
                    NULL,
                    NULL );
      COMMIT;

      stmt := 'CREATE TABLE EMP_' || UPPER ( job_id ) || ' (
                   employee_id     NUMBER ( 6, 0 ) NOT NULL,
                   first_name      VARCHAR2 ( 20 ),
                   last_name       VARCHAR2 ( 25 ) NOT NULL,
                   salary          NUMBER ( 8, 2 ),
                   department_id   NUMBER ( 4, 0 ),
                   CONSTRAINT fk_e' || UPPER ( job_id ) || '
                       FOREIGN KEY ( employee_id ) 
                       REFERENCES employees ( employee_id ))';

      EXECUTE IMMEDIATE stmt;
   END IF;
EXCEPTION
   WHEN OTHERS
   THEN
      err_msg := SQLERRM;
      DBMS_OUTPUT.put_line ( err_msg );
END;
/

答案 1 :(得分:0)

我认为你误解了PL / SQL中的if句子。你不是使用begin而是THEN,并且在结束之前结束if。 这看起来怎么样? 有一段时间以来,我使用PL / SQL,所以请耐心等待。

<system.webServer>
  <staticContent>
    <remove fileExtension=".mp4"/>
    <mimeMap fileExtension=".mp4" mimeType="video/mp4"/>
    <remove fileExtension=".ogv"/>
    <mimeMap fileExtension=".ogv" mimeType="video/ogg"/>
    <remove fileExtension=".m4v"/>
    <mimeMap fileExtension=".m4v" mimeType="video/m4v"/>
    <remove fileExtension=".webm"/>
    <mimeMap fileExtension=".webm" mimeType="video/webm"/>
  </staticContent>
</system.webServer>

要获得更好的错误输出,我喜欢使用:

CREATE OR REPLACE PROCEDURE add_job(job_id VARCHAR2,job_title VARCHAR2)
IS

var varchar2(4000);
job_exists number := 0; 

BEGIN 


    SELECT COUNT(job_id) INTO job_exists FROM jobs WHERE job_id = job_id; 

    IF job_exists != 0 THEN 
      DBMS_OUTPUT.PUT_LINE('All Ready Value IS Exits');
    ELSE 
       INSERT INTO jobs VALUES (job_id,job_title,null,null);

          EXECUTE IMMEDIATE  
              'CREATE TABLE ' || 'EMP_' || UPPER(job_id) ||
              '( 
                  EMPLOYEE_ID   NUMBER(6,0) NOT NULL,     
                  FIRST_NAME   VARCHAR2(20) ,
                  LAST_NAME     VARCHAR2(25) NOT NULL,
                  SALARY     NUMBER(8,2),
                  DEPARTMENT_ID  NUMBER(4,0),
                  CONSTRAINT fk_e' || UPPER(job_id) || ' 
                          FOREIGN KEY (EMPLOYEE_ID)
                          REFERENCES EMPLOYEES(EMPLOYEE_ID)
              )';
    END IF; 

    EXCEPTION 
        WHEN others THEN 
            dbms_output.put_line('ERROR');
END add_job;