Oracle
 sql >> Database >  >> RDS >> Oracle

Come utilizzare SYS_REFCURSUR in select for update in pl/sql

Un sys_refcursor non può essere utilizzato in update dichiarazione. È possibile utilizzare un cursore esplicito come mostrato di seguito. Usa in questo modo:

    DECLARE
    cursor a_cursor is
      SELECT mytable.VID
      FROM   mytable
      WHERE  ROWNUM <= COUNT FOR UPDATE;

    a_id number;
    begin
    OPEN a_cursor;
    loop     
      FETCH a_cursor INTO a_id;          
      exit when a_cursor%notfound; 

        UPDATE mytable SET
        ...
        WHERE  VID = a_vid;                   
    end loop;
    COMMIT;
   close a_cursor;
 end;

Modifica:

create or replace PROCEDURE get_rows(
                                   a_cursor OUT SYS_REFCURSOR,
                                   a_id IN VARCHAR,
                                   a_count IN NUMBER)
IS                                   
cursor a_cur is
  SELECT mytable.VID
  FROM   mytable
  WHERE  ROWNUM <= a_COUNT ;

a_id NUMBER;
cnumber number;
BEGIN 
OPEN a_cur;

 LOOP
  FETCH a_cur INTO a_id; 

  IF a_cur%notfound THEN
   cnumber := 9999;
  End if;

  exit when a_cursor%notfound; 

    UPDATE mytable SET
     ...
    WHERE  VID = a_vid;
  END loop;
  COMMIT;    
  CLOSE a_cur;

  Open a_cursor for select * from mytable;

end ;