Home » SQL & PL/SQL » SQL & PL/SQL » Execute sequence once per select (Oracle 10g)
Execute sequence once per select [message #607530] Sat, 08 February 2014 04:51 Go to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member


HI ALL,

I want to execute the sequnce once for each select . But it's not happening. Please help.

  CREATE SEQUENCE test_seq increment by 1 minvalue 1 nocycle noorder cache 20;
  
  SELECT case when rownum = 1 then 'x' else 'xxx' end test_data,
  case when rownum = 1 then test_seq.nextval else test_seq.currval end test_seq ,emp.* FROM emp;
  
  


Regards,
Nathan
Re: Execute sequence once per select [message #607531 is a reply to message #607530] Sat, 08 February 2014 05:01 Go to previous messageGo to next message
Michel Cadot
Messages: 68633
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

You have to create a function to call your sequence.


Re: Execute sequence once per select [message #607533 is a reply to message #607531] Sat, 08 February 2014 05:03 Go to previous messageGo to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

Thank you,

But what might be the reason why it is behaving like this.
Re: Execute sequence once per select [message #607534 is a reply to message #607531] Sat, 08 February 2014 05:05 Go to previous messageGo to next message
Michel Cadot
Messages: 68633
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

SQL> create sequence s;

Sequence created.

SQL> create or replace function f (rn integer) return integer as 
  2    v pls_integer;
  3  begin
  4    if rn=1 then select s.nextval into v from dual;
  5    else select s.currval into v from dual;
  6    end if;
  7    return v;
  8  end;
  9  /

Function created.

SQL> select f(rownum), emp.ename
  2  from emp
  3  /
 F(ROWNUM) ENAME
---------- ----------
         1 SMITH
         1 ALLEN
         1 WARD
         1 JONES
         1 MARTIN
         1 BLAKE
         1 CLARK
         1 SCOTT
         1 KING
         1 TURNER
         1 ADAMS
         1 JAMES
         1 FORD
         1 MILLER

14 rows selected.

Re: Execute sequence once per select [message #607535 is a reply to message #607533] Sat, 08 February 2014 05:09 Go to previous messageGo to next message
Michel Cadot
Messages: 68633
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

sss111ind wrote on Sat, 08 February 2014 12:03
Thank you,

But what might be the reason why it is behaving like this.


It is designed (and documented) like this.
Choices have to be made, this choice was made.

Re: Execute sequence once per select [message #607536 is a reply to message #607535] Sat, 08 February 2014 05:17 Go to previous message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

Thank you very much sir, It worked exactly fine.
Previous Topic: etl script, same database, two different schemas
Next Topic: for loop
Goto Forum:
  


Current Time: Mon Apr 15 23:29:14 CDT 2024