Can I Use ROWNUM < SEQUENCE.NEXTVAL in select statement(sqlplus) on a table?

189 Views Asked by At

I have an emp table SQL> select * from emp;

 EMPNO ENAME

  5000 JOHN
  5001 ADAM
  5002 MIKE

I need help writing a procedure to run "Select EMPNO||ENAME from emp" on one row at a time till the end of the table. Trying to use the below statement with N as a sequence.nextval. It does not work. Please help me doing resolving this.

select EMPNO||ENAME from emp
where rownum < N+1
minus
select EMPNO||ENAME from emp
where rownum < N;
1

There are 1 best solutions below

6
On

If you want to access each row and do something with it in a loop:

declare
tmp as varchar2(500); -- whatever size you need

begin

  for emp_rec in (select * from emp order by empno )
  loop

    -- do something with record data
    tmp  := emp_rec.empno || emp_rec.ename;
    -- etc.

  end loop;
end;