1

I am sending to a downstream application events and I need in each event a sequence number, as a control number that I don't have gaps, I don't miss sending events. What I am trying to achieve is having a db sequence, to have assigned to the results of a query the same sequence number. I don't want to use a procedure, function, etc, only some SQL.

For example I need a query result like this:

col1row1  col2row1 seq1
col1row2  col2row2 seq1
col1row3  col2row3 seq1
col1row4  col2row4 seq1

The problems I found is that sequence have restrictions is queries or that I have different values for seq1.

I tried this:

SELECT t.*, ROWNUM, 
CASE ROWNUM
  WHEN 1 THEN MY_SEQ.NEXTVAL
  ELSE MY_SEQ.CURRVAL
END as event_seq
from my_table t

or

SELECT t.*, ROWNUM, decode(rownum, 1, MY_SEQ.NEXTVAL, MY_SEQ.CURRVAL) from my_table t

and the sequence is defined:

CREATE SEQUENCE MY_SEQ MINVALUE 1 MAXVALUE 999999999999999999999999999 
INCREMENT BY 1 START WITH 1 NOCACHE NOORDER NOCYCLE ;
2
  • '... that I don't have gaps...', be aware that it's not possible to avoid gaps in sequences... Commented Jul 11, 2014 at 15:07
  • The reasons I need no gaps is that the downstream system that receive messages will see and understand there were some exceptional conditions. Other than this situation, by setting NOCACHE, I will get each time one number, there will be no caching and I sould not have gaps. Commented Jul 14, 2014 at 13:46

1 Answer 1

1

If you are within the same session, you just just fill MY_SEQ.CURRVAL by making a call from dual with MY_SEQ.NEXTVAL.

select my_seq.nextval from dual;
select my_seq.currval, t.* from my_table t;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Bob, but I wanted something similar with what you sent me, but in just one SQL call, not in two.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.