Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.misc -> Re: How to: retrieve Sequence.NEXTVAL to a variable?
On Fri, 13 Nov 1998 11:18:56 -0000, "Hugh Pendry"
<hpendry_at_trinitech.co.uk> wrote:
>Does anyone know how to retrieve the next value (or current value) a
>sequence into a variable in a stored procedure. I have tried various things
>including:-
>
>function x
>(
> ...
>)
>return ...
>is
> MyVar integer
>begin
> select MySequence.nextVal into MyVar;
> ...
>end;
>
>But have had no luck getting anything to work.
You almost had it...you just need to SELECT from DUAL instead. As in:
Sequence created.
SQL> create procedure doit as
2 myvar integer;
3 begin
4 SELECT seq_foo.NEXTVAL INTO myvar FROM DUAL;
5 DBMS_OUTPUT.PUT_LINE('The value is: ' || myvar );
6 end;
7 /
Procedure created.
SQL> exec doit;
The value is: 1
PL/SQL procedure successfully completed.
SQL> exec doit;
The value is: 2
PL/SQL procedure successfully completed.
SQL>
Thanks!
Joel
Joel R. Kallman Oracle Government, Education, & Health
Columbus, OH http://govt.us.oracle.com jkallman@us.oracle.com http://www.oracle.com