返回Oracle SQL插入语句中的参数

时间:2012-08-24 08:25:05

标签: sql oracle

我有一个insert语句,我需要返回一个参数,以便以后使用它。我收到一个奇怪的错误,我似乎无法找到解决方案。任何人都可以对此有所了解吗?

set define off;
variable videoID number;

insert into VM_VIDEO (VIDEO_ID, ...)
  values (SEQ_VMMIGVIDEO_ID.NEXTVAL, ...)
  returning VIDEO_ID into :videoID;

insert into IMAGES (IMAGE_ID, ...)
  values (SEQ_IMAGE_ID.NEXTVAL, ...);

update VM_VIDEO
  set THUMB_IMAGE_ID = SEQ_IMAGE_ID.CURRVAL
  where VIDEO_ID = :videoID;

错误:

Error starting at line 4 in command:
insert into VM_VIDEO (VIDEO_ID, ...) values (SEQ_VMMIGVIDEO_ID.NEXTVAL, ...) returning VIDEO_ID into :videoID
Error report:
SQL Error: Not all return parameters registered

1 个答案:

答案 0 :(得分:7)

您需要将其放入PL / SQL块:

declare
  videoID number;
begin
  insert into VM_VIDEO (VIDEO_ID, ...)
    values (SEQ_VMMIGVIDEO_ID.NEXTVAL, ...)
    returning VIDEO_ID into videoID;

  insert into IMAGES (IMAGE_ID, ...)
    values (SEQ_IMAGE_ID.NEXTVAL, ...);

  update VM_VIDEO
    set THUMB_IMAGE_ID = SEQ_IMAGE_ID.CURRVAL
    where VIDEO_ID = videoID;
end;