Home » SQL & PL/SQL » SQL & PL/SQL » Looping in a For loop (Oracle 10g)
Looping in a For loop [message #607641] Mon, 10 February 2014 08:36 Go to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

Hi All,

Instead of 14 inserts/updates can we make the things done with only 5 inserts. Please help me.

CREATE TABLE job_cnt (cnt NUMBER,JOB VARCHAR2(10));

DECLARE
  l_pre_val VARCHAR2(100):='1';
  l_cur_val VARCHAR2(100):='1';
  l_cnt     NUMBER       :=0;
BEGIN
  FOR i IN
  (SELECT JOB FROM emp ORDER BY 1 )
  LOOP
    l_cur_val   :=i.JOB;
    IF l_pre_val<>l_cur_val THEN
      l_cnt     :=1;
      dbms_output.put_line('insert'||l_cnt||' '||l_cur_val);
      INSERT INTO job_cnt VALUES(l_cnt,l_cur_val );
    ELSE
      l_cnt :=l_cnt+ 1;
      dbms_output.put_line('update'||l_cnt||' '||l_cur_val);
      UPDATE job_cnt SET cnt=l_cnt WHERE job=l_cur_val;
    END IF;
    l_pre_val:=l_cur_val;
  END LOOP;
  COMMIT;
END;

SELECT * FROM job_cnt;

Regards,
Nathan
Re: Looping in a For loop [message #607642 is a reply to message #607641] Mon, 10 February 2014 08:40 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Never do in PL/SQL, that which can be done in plain SQL.

Take a serious look at the MERGE statement.
Re: Looping in a For loop [message #607644 is a reply to message #607642] Mon, 10 February 2014 08:56 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
And ignoring merge for a second, did it not occur to you to write a select that actually selects the count of records per job?
Re: Looping in a For loop [message #607645 is a reply to message #607644] Mon, 10 February 2014 09:06 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
But really that table just shouldn't exist, if necessary create a view instead.
Re: Looping in a For loop [message #607646 is a reply to message #607645] Mon, 10 February 2014 09:47 Go to previous messageGo to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

We do not want direct output because after doing some calculations the records need to be inserted.
Re: Looping in a For loop [message #607647 is a reply to message #607646] Mon, 10 February 2014 09:54 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>We do not want direct output
so please provide an example (SQL & results) of not direct output

[Updated on: Mon, 10 February 2014 09:54]

Report message to a moderator

Re: Looping in a For loop [message #607649 is a reply to message #607646] Mon, 10 February 2014 10:13 Go to previous messageGo to next message
gazzag
Messages: 1118
Registered: November 2010
Location: Bedwas, UK
Senior Member
Cookiemonster wants to know why you are duplicating the data into another table. Why not just create a view on the required data?
Re: Looping in a For loop [message #607650 is a reply to message #607649] Mon, 10 February 2014 10:27 Go to previous messageGo to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

Because the data will not come from the single cursor,there are multiple cursors. After looping through the different cursors data the required results will be captured in the table for returing as a cursor to front end application. The exact test case is hard to replicate for emp and dept table.
Re: Looping in a For loop [message #607651 is a reply to message #607650] Mon, 10 February 2014 10:44 Go to previous message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
If dept and emp don't replicate your problem then don't use them.
As it stands we have no idea what you are actually trying to accomplish or what problems you are encountering.
So we can't offer any help apart from the incredibly generic "do as much work in sql as possible"
If you want better advise than that then you're going to have to give a better description of your issue, otherwise you're just wasting everyone's time, including your own.
Previous Topic: Pragma autonoums transaction
Next Topic: ORA-4091- Mutating Trigger Error
Goto Forum:
  


Current Time: Thu Apr 25 02:32:40 CDT 2024