如何在PL/SQL中创建其值仍然存在的变量?

Joe*_*eS. 2 oracle toad plsql

我正在编写一个过程,其中我需要一个变量中的值,我可以更改并保持该变量.

更具体地说,这需要是一个从10001开始的计数器,并且每次调用该过程时都会递增.我不想返回这个值,因为它需要连接到其他几个值,然后将返回(用于另一个表的ID生成).

我研究了在SQL PLUS中使用绑定变量似乎没有帮助.我宁愿不为这个值创建一个表.是否有允许这样的选项?

Ale*_*sej 5

你可能需要一个序列

create sequence proc_seq start with 1;
create or replace procedure testSeq is
begin
  dbms_output.put_line('Seq is ' || proc_seq.nextVal);
end;
Run Code Online (Sandbox Code Playgroud)

每次调用该过程时,该值将增加1:

SQL> exec testSeq;
Seq is 1

PL/SQL procedure successfully completed.

SQL> exec testSeq;
Seq is 2

PL/SQL procedure successfully completed.

SQL> exec testSeq;
Seq is 3

PL/SQL procedure successfully completed.
Run Code Online (Sandbox Code Playgroud)

根据您的Oracle版本,您可能无法像我一样使用序列; 在这种情况下,您可以使用变量来存储序列的值,使用SQL语句中的序列:

create or replace procedure testSeq is
    s number;
begin
  select proc_seq.nextVal into s from dual;
  dbms_output.put_line('Seq is ' || s);
end;
Run Code Online (Sandbox Code Playgroud)