Oracle 插入返回

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29707220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 02:49:24  来源:igfitidea点击:

Oracle insert into returning

oracleinsert

提问by Wulpo

I'm trying return id after insert:

我正在尝试插入后返回 id:

CREATE SEQUENCE seq_osobne_udaje
INCREMENT BY 1 START WITH 1;

CREATE OR REPLACE TRIGGER trig_osobne_udaje_seq
BEFORE INSERT ON osobne_udaje
FOR EACH ROW
BEGIN
  SELECT seq_osobne_udaje.nextval INTO :new.id FROM dual;
END;
/

and then:

进而:

var tmp number;

insert into osobne_udaje(name,sur,born,is_man) 
values('Jacob','Wulp',to_date('28.07.1992','DD.MM.YYYY'),'Y') 
returning id into tmp;

but it write exception in insert row: SQL Error: ORA-00905: missing keyword

但它在插入行中写入异常:SQL 错误:ORA-00905:缺少关键字

回答by MT0

This script works in SQL Developer:

此脚本适用于 SQL Developer:

DROP TRIGGER trig_osobne_udaje_seq;
DROP SEQUENCE seq_osobne_udaje;
DROP table osobne_udaje;

create table osobne_udaje(
  id NUMBER,
  name VARCHAR2(20),
  sur  VARCHAR2(20),
  born DATE,
  is_man CHAR(1)
)
/

CREATE SEQUENCE seq_osobne_udaje
INCREMENT BY 1 START WITH 1;
/

CREATE OR REPLACE TRIGGER trig_osobne_udaje_seq
BEFORE INSERT ON osobne_udaje
FOR EACH ROW
BEGIN
  :new.id := seq_osobne_udaje.nextval;
END;
/

var tmp number;
/

BEGIN
  insert into osobne_udaje(name,sur,born,is_man) 
  values('Jacob','Wulp',to_date('28.07.1992','DD.MM.YYYY'),'Y')
  returning id into :tmp;
END;
/

print tmp;