Oracle插入返回

时间:2015-04-17 18:50:17

标签: oracle 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;
/

然后:

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;

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

1 个答案:

答案 0 :(得分:5)

此脚本适用于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;