如何为变量分配序列值?

Nic*_*unt 5 sql oracle

我需要为变量分配一个序列值,以便在序列值递增后使用.我试过这个,但它给出了一个错误:

variable imageID number;
select SEQ_IMAGE_ID.CURRVAL into :imageID from dual;

select * from IMAGES where IMAGE_ID = :imageID;


Error starting at line 2 in command:
select SEQ_IMAGE_ID.CURRVAL into :imageID from dual
Error report:
SQL Error: ORA-01006: bind variable does not exist
01006. 00000 -  "bind variable does not exist"
Run Code Online (Sandbox Code Playgroud)

我已经三重检查了序列名称是否正确,有什么想法吗?

Ale*_*ole 7

您似乎在variable声明中在SQL*Plus或SQL Developer中执行此操作.您需要在PL/SQL块中进行赋值,使用显式begin/ endexec隐藏的调用:

variable imageID number;
exec select SEQ_IMAGE_ID.CURRVAL into :imageID from dual;
select * from IMAGES where IMAGE_ID = :imageID;
Run Code Online (Sandbox Code Playgroud)

如果您使用的是11g,则不需要select,您可以指定:

variable imageID number;
exec :image_id := SEQ_IMAGE_ID.CURRVAL;
select * from IMAGES where IMAGE_ID = :imageID;
Run Code Online (Sandbox Code Playgroud)

您还可以使用替换变量:

column tmp_imageid new_value image_id;
select SEQ_IMAGE_ID.CURRVAL as tmp_imageID from dual;
select * from IMAGES where IMAGE_ID = &imageID;
Run Code Online (Sandbox Code Playgroud)

请注意更改:以指示绑定变量,&以指示替换变量.

  • 完善.我所需要的只是执行官,但更全面地了解这些选项总是好的. (2认同)