1

Guys I do really need your help. I have written few lines of code and now I am stuck because I cannot run below procedure in 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;

ERROR

ORA-00933: SQL command not properly ended
ORA-06512: at "PROJEKT.ROUTSETTER", line 17
ORA-06512: at line 6
2
  • Do you mean you can't create it or really that you can't run it? If the latter, how are you trying to run it? Commented Mar 12, 2016 at 16:03
  • Just simply: exec routsetter('IV.4'); Commented Mar 12, 2016 at 16:39

1 Answer 1

1

Your line;

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

...puts var (which is a varchar) straight into an SQL statement without appropriate quoting.

If we assume var is free from single quotes, your line should be something like;

EXECUTE IMMEDIATE 'CREATE TABLE '||NEW_TABLE||
     ' AS SELECT * FROM '||BASE_TABLE||' WHERE WYCENA = '''||var||'''';
Sign up to request clarification or add additional context in comments.

2 Comments

Yes! I found it is useful to print out any dynamically created SQL statements prior to call execute immediate. That helps pointing out these simple but nasty bugs.
@GeeBee Adding what commands you use to print them may be useful for others with the same problem :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.