在oracle pl / sql中显示执行立即执行结果时出错

时间:2018-02-22 12:04:03

标签: oracle plsql

 CREATE OR REPLACE PROCEDURE create_tbl_user
    (tbl_name varchar2)
    IS
    sql_stat varchar2(500);
    l_count number;
    BEGIN
        --checking the presence of tablespace
        sql_stat := 'select count(*) from dba_tablespaces where tablespace_name = upper('||tbl_name||')';
        dbms_output.put_line(sql_stat);
        execute immediate sql_stat into l_count;
        dbms_output.put_line(l_count);
       /* IF (l_count != 0) THEN 
            dbms_output.put_line('Tablespace with '||tbl_name||' name already present');
        END IF;*/
    END;


 set serveroutput on;
 exec create_tbl_user('tbs');
Error starting at line : 18 in command -
BEGIN CREATE_TBL_USER('temp'); END;
Error report -
ORA-00942: table or view does not exist
ORA-06512: at "SAMAN.CREATE_TBL_USER", line 10
ORA-06512: at line 1
00942. 00000 -  "table or view does not exist"
*Cause:    
*Action:

Screenshot: the error i get in sqldeveloper

1 个答案:

答案 0 :(得分:1)

稍作修改; USING(第11行)是您正在寻找的。

SQL> CREATE OR REPLACE PROCEDURE create_tbl_user (tbl_name VARCHAR2)
  2  IS
  3     sql_stat   VARCHAR2 (500);
  4     l_count    NUMBER;
  5  BEGIN
  6     --checking the presence of tablespace
  7     sql_stat :=
  8        'select count(*) from dba_tablespaces where tablespace_name = upper(:1)';
  9     DBMS_OUTPUT.put_line (sql_stat);
 10
 11     EXECUTE IMMEDIATE sql_stat INTO l_count USING tbl_name;
 12
 13     DBMS_OUTPUT.put_line (l_count);
 14
 15     IF (l_count != 0)
 16     THEN
 17        DBMS_OUTPUT.put_line (
 18           'Tablespace with ' || tbl_name || ' name already present');
 19     END IF;
 20  END;
 21  /

Procedure created.

SQL> EXEC create_tbl_user('temp');
select count(*) from dba_tablespaces where tablespace_name = upper(:1)
1
Tablespace with temp name already present

PL/SQL procedure successfully completed.

SQL>