0

I've been writing up a Stored Proc to select a bunch of ID's from a table, save the resultset into a cursor, then loop through the Cursor, updating rows in a table table based on whichever ID is in focus in the Cursor. Planning to add in a few more conditional statements, but so far..

CREATE OR REPLACE PROCEDURE CHANGE_PESONAL_DETAILS
AS

CURSOR
person_cur
IS

select b.id
    from blah1 b
        inner join blah2 b2 on b.id = b2.fkid;
person_rec person_cur%ROWTYPE;

BEGIN
FOR person_rec in person_cur

LOOP

dbms_output.put_line
(
person_rec.id
);

UPDATE blah1 set Details = DetailsLineOne || ' ' || DetailsLineTwo WHERE id = person_rec.id;

END LOOP;
COMMIT;

END;

What I have here compiles fine, though when executed, the update never takes place. I've added in the output line to see what is even in the Cursor, but nothing gets printed.

My Select statement to populate the Cursor runs fine on its own, returning all the data I'd expect it to, though when executed in the above, nothing. It's as if everything after Begin isn't executing, though I can't understand why as previously mentioned, my Select returns results on its own.

Maybe in the context of populating a cursor something is behaving different? Any advice would be great..

3
  • if you run the cursor select as standalone then , does it return any data (in the same session that you're running the procedure as)? also when you're running the procedure have you set serveroutput on (or theequivalent if you're using a gui)? Commented Mar 14, 2013 at 10:52
  • You don't need the cursor in the first place. It only makes your UPDATE a lot less efficient. Commented Mar 14, 2013 at 10:52
  • @DazzaL - Yes the cursor select returns the data I'd expect it to when ran alone, even after adding in set serveroutput on, still no output printed. Commented Mar 14, 2013 at 10:58

2 Answers 2

1

Please check the procedure without a cursor and loop.

CREATE OR REPLACE PROCEDURE CHANGE_PESONAL_DETAILS
AS
BEGIN
UPDATE blah1 set Details = DetailsLineOne || ' ' || DetailsLineTwo WHERE id IN (select b.id
    from blah1 b inner join blah2 b2 on b.id = b2.fkid);
END;

Check using loop

CREATE OR REPLACE PROCEDURE CHANGE_PESONAL_DETAILS
AS
CURSOR person_cur IS
  select b.id
    from blah1 b
        inner join blah2 b2 on b.id = b2.fkid;

  person_rec person_cur%ROWTYPE;
BEGIN
    OPEN person_cur;
    LOOP
      FETCH person_cur INTO person_rec;
      EXIT WHEN person_cur%NOTFOUND;

      UPDATE blah1 set Details = DetailsLineOne || ' ' || DetailsLineTwo WHERE id = person_rec.id;
    END LOOP;
    CLOSE person_cur;
END;
Sign up to request clarification or add additional context in comments.

1 Comment

Works fine doing it this way. Though I want to add in some conditional statements at later date, so keeping it as a proc is my only option really.
0

we can't declare a rowtype of cursor if we use the cursor in for loop the variable used in for loop will be used to access the columns in the cursor.

So remove the rowtype declaration and try like and moreover include the SET SERVEROUT ON command before executing the procedure to see the results(printable statements)

CREATE OR REPLACE PROCEDURE CHANGE_PESONAL_DETAILS
AS

CURSOR person_cur IS
     select b.id from blah1 b inner join blah2 b2 on b.id = b2.fkid;
BEGIN
     FOR person_rec in person_cur LOOP
          dbms_output.put_line(person_rec.id);
          UPDATE blah1 set Details = DetailsLineOne || ' ' || DetailsLineTwo WHERE id = person_rec.id;
     END LOOP;
     COMMIT;
END;

4 Comments

Even after removing the rowtype, I still have no luck. Having replaced the output to be a basic 'Hello World!' string, not even that is being output. It's as if nothing is even being executed following the select statement to populate the Cursor!
I think ur select statement doesnot give any records Just try to execute only the select statement i.e select b.id from blah1 b inner join blah2 b2 on b.id = b2.fkid; and tell us the result .
select statement returns all the records I expect when ran alone.
Ok !! Try removing the cursor and just include the same select statement in for loop i.e CREATE OR REPLACE PROCEDURE CHANGE_PESONAL_DETAILS AS begin For i in (select b.id as id from blah1 b inner join blah2 b2 on b.id = b2.fkid) loop dbms_output.put_line(person_rec.id); UPDATE blah1 set Details = DetailsLineOne || ' ' || DetailsLineTwo WHERE id = i.id; end loop; end;

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.