0

I'm trying to run a few statements in one sql file but it does not work.

declare 
  new_sequence INTEGER;
begin 
  select LAST_NUMBER + 1 
    into new_sequence 
    from user_sequences 
   where SEQUENCE_NAME = 'MLTS_SEQUENCE';

  execute immediate 'Create sequence Table_SEQ start with '
      || new_sequence ||' increment by 1';
end;

If i run this block with the option 'execute as one statement' in the eclipse database plugin it works.

How can i mark the sqlscript to run each of these blocks as one statement, to execute the script later with sqlplus or something different than eclipse?

I tried GO in front and / at the end but that also didn't work.

2
  • 1
    GO is sybase / sqlserver syntax. "/" at the end is right (it has to be the 1st character on a line by itself). what error occurred when you ran via sqlplus with a / after the block Commented Mar 8, 2013 at 15:14
  • "but it does not work." is not enough detail. If you want us to help you you need to describe what happens, including any error messages you get. Also, please be clear about what you are running where. Commented Mar 8, 2013 at 16:49

2 Answers 2

1

As long as it's on its own line and left aligned, it should be ok in sql*plus:

SQL> create sequence MLTS_SEQUENCE start with 1 cache 20;

Sequence created.

SQL> select MLTS_SEQUENCE.nextval from dual;

   NEXTVAL
----------
         1

SQL> declare
  2    new_sequence INTEGER;
  3  begin
  4    select LAST_NUMBER + 1
  5      into new_sequence
  6      from user_sequences
  7     where SEQUENCE_NAME = 'MLTS_SEQUENCE';
  8
  9    execute immediate 'Create sequence Table_SEQ start with '
 10        || new_sequence ||' increment by 1';
 11  end;
 12  /

PL/SQL procedure successfully completed.

SQL> select Table_SEQ.nextval from dual;

   NEXTVAL
----------
        22
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks you're right it does work, must have had some error in my script. Should it work also with squirrel/eclipse etc.? because there i get "PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following: := . ( @ % ; not null range default character"
@LolaParola that error would suggest you've ommitted the ";" after END ie you had END then / on the next line ?
Everything works fine now, thanks! Had to change the statement seperator from ; to / in the squirrel "session properties -> sql"
What about comments? Can one embed comments in a foo.sql? What's the syntax? Thanks!
1

The proper format for sqlplus is the following:

declare
  ...
begin
  ...
end;
/

Comments

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.