How to get last Quence on Oracle?

Asked

Viewed 6,341 times

2

I tried 3 different ways, to make one select of the last sequence of my table, but none worked.

SELECT SEQ_ID_PAIS.CURRVAL FROM DUAL;

Error:

 ORA-08002: a sequência SEQ_ID_PAIS.CURRVAL ainda não foi definida nesta sessão
08002. 00000 -  "sequence %s.CURRVAL is not yet defined in this session"

SELECT LAST_NUMBER FROM SEQ_ID_PAIS;

Error:

 ORA-02201: sequência não permitida aqui
    02201. 00000 -  "sequence not allowed here"

SELECT LAST_VALUE FROM SEQ_ID_PAIS;

Error:

ORA-02201: sequência não permitida aqui
02201. 00000 -  "sequence not allowed here"

what would be the correct way to obtain this value ?

  • @Articuno I thought the error was in the code in java, more realized, that my carelessness was in sql, so I re-asked the question.

  • Tip: no need to keep erasing and recreating the same question. If no one has answered you, it is because your question is either not very clear or because no one who mastered the subject has yet seen the question. Be patient and whenever possible, edit and improve the question, an hour someone answers.

  • https://www.techonthenet.com/oracle/errors/ora08002.php I think the "currval" can only be called after the "nextval", why you know the last Quence before using it !?

  • @Motta will print on a screen as ID.

  • Okay, then do after the "nextval".

  • I did not understand very well, I will have to make 2 selects ?

Show 1 more comment

2 answers

2

The CURRVAL command is only for the session that sequentially ran, so you will no longer have this value.

You can try to see the structure of it,

SELECT *
  FROM user_sequences
 WHERE sequence_name = 'nome_da_sua_sequence'; 

failing, you can search the table that it is used for the insert and check the MAX country.

0

If you want to return right after insertion you can do so:

insert into SIG_PROJETOAGRUPAMENTOSERVICO
  (idagrupamentoservico, descricao)
values
  (sq_Sig_Projagrupamentoserv.nextval, :nomeAgrupamento)
 returning idagrupamentoservico into :idagrupamento

This way your query will return the entered value.

If you want to access it later just do so:

SELECT last_number
  FROM user_sequences
 WHERE sequence_name = '<sequence_name>';
  • from the error, I tried it like this: SELECT last_number FROM PARENTS WHERE sequence_name = '<SEQ_ID_PAIS>'; it returns me : ORA-00904: "SEQ_ID_PAIS": invalid identifier 00904. 00000 - "%s: invalid Identifier" *Cause: *Action: Line error: 1 Column: 38

  • This is usually syntax error, takes a print and sends here how to do the query. If possible put in question the sql if creating tables, so I can recreate the environment here and simulate.

  • SELECT last_number FROM user_sequences WHERE sequence_name = 'SEQ_ID_PAIS'

Browser other questions tagged

You are not signed in. Login or sign up in order to post.