为什么我不能在SQL Developer中运行此脚本?

时间:2016-03-12 15:53:54

标签: oracle stored-procedures plsql oracle-sqldeveloper

伙计我真的需要你的帮助。我写了几行代码,现在我被卡住了,因为我无法在SQL Developer中运行以下程序。

PROCEDURE

create or replace 
PROCEDURE ROUTSETTER(
    var IN VARCHAR2)
IS
  COUNT_RECORDS NUMBER       := 0;
  BASE_TABLE    VARCHAR2(20) := 'DROGA';
  NEW_TABLE     VARCHAR2(20) := 'DROGA_COPY';
BEGIN
  SELECT COUNT(*)
  INTO COUNT_RECORDS
  FROM ALL_OBJECTS
  WHERE OBJECT_TYPE = 'TABLE'
  AND OBJECT_NAME   = NEW_TABLE;
  IF COUNT_RECORDS  > 0 THEN
    EXECUTE IMMEDIATE 'TRUNCATE TABLE '||NEW_TABLE;
    dbms_output.put_line('TABLE '||NEW_TABLE||' HAS BEEN TRUNCATED');
  ELSE
    EXECUTE IMMEDIATE 'CREATE TABLE '||NEW_TABLE||' AS SELECT * FROM '||BASE_TABLE||' WHERE WYCENA = '||var;
    dbms_output.put_line('TABLE '||NEW_TABLE||' HAS BEEN CREATED');
  END IF;
END ROUTSETTER;

错误

ORA-00933: SQL command not properly ended
ORA-06512: at "PROJEKT.ROUTSETTER", line 17
ORA-06512: at line 6

1 个答案:

答案 0 :(得分:1)

你的路线;

EXECUTE IMMEDIATE 'CREATE TABLE '||NEW_TABLE||
     ' AS SELECT * FROM '||BASE_TABLE||' WHERE WYCENA = '||var;

...将var(这是一个varchar)直接放入SQL语句中而没有适当的引用。

如果我们假设var没有单引号,那么您的行应该是类似的;

EXECUTE IMMEDIATE 'CREATE TABLE '||NEW_TABLE||
     ' AS SELECT * FROM '||BASE_TABLE||' WHERE WYCENA = '''||var||'''';