Consultor Eletrônico



Kbase 51109: Retornando o valor da sequencia inserido no registro em Oracle
Autor   Eloi Rene Pscheidt - CAT
Acesso   Público
Publicação   19/06/2015
Neste kbase temos um exemplo de como conhecer o valor de uma sequencia que foi inserido num registro, via comando INSERT.
Este procedimento funciona tanto para uso da sequencia explícita no comando INSERT ou via TRIGGER.
Exemplo construído utilizando a ferramenta SQL*Plus do Oracle.

SQL> create table t1 (c1 number, c2 varchar2(10));
Table created.
SQL> create sequence seq1;
Sequence created.
SQL> var x number;
SQL> insert into t1 (c1, c2) values (seq1.nextval, 'teste') returning c1 into :x;
1 row created.
SQL> print x;
X
----------
2
SQL> insert into t1 (c1, c2) values (seq1.nextval, 'teste2') returning c1 into :x;
1 row created.
SQL> print x;
X
----------
3
SQL> l
1 create or replace trigger trig1
2 before insert on t1
3 for each row
4 begin
5 :new.c1 := seq1.nextval;
6* end;
SQL> /
Trigger created.
SQL> insert into t1 (c2) values ('teste3') returning c1 into :x;
1 row created.
SQL> print x;
X
----------
4
SQL> select * from t1;
C1 C2
---------- ----------
2 teste
3 teste2
4 teste3