PL/SQL problem with 'drop sequence' when called within procedure

Hello there,

I want to create a procedure that creates then drops a sequence (the actual procedure does a lot more but this is the part that does not work).

So I assumed that I would use the following:

create or replace procedure create_then_drop_seq is

begin
   
   create sequence my_seq
   start with 1
   increment by 1
   nomaxvalue;
 
  drop sequence my_seq;
end;

But when I try and execute the procedure it does not work. Even though the lines of code in the procedure work when I input them raw into the SQL console?

Hopefully there will be a nice and simple answer to this question (he says with fingers crossed!).

Thanks

Ren
BeamAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tony_HaslerCommented:
The problem is that not all of SQL is valid from within PL/SQL.  Specifically, DDL is prohibited.  This is the workaround:

create or replace procedure create_then_drop_seq is
x number ;
begin
   execute immediate 'create sequence my_seq start with 1 increment by 1 nomaxvalue';
   execute immediate 'select my_seq.nextval from dual' into x ;
   dbms_output.put_line(x) ;
   execute immediate 'drop sequence my_seq';
exception
   when others then
   execute immediate 'drop sequence my_seq';
   raise ;
end;

Note that the sequence is NOT local to the procedure, so if two sessions were to use the procedure they would
share the same sequence.  I assume that this is what you want, because otherwise you could just use a local (or package) variable and then just say:

x := x + 1 ;

Hope this helps
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BeamAuthor Commented:
Its works now thanks Tony.

Also thanks for the clear explaination.

Its good to actually understand why you are doing something.

Cheers

Ren
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.