PL SQL with multiple cursors. Please help with my UPDATE
485386Aug 21 2006 — edited Aug 21 2006Hi All,
I've only done basic PLSQL programming and i'm having trouble with this one. Any help would be greatly appreciated. I need to update the version number of my assets and need to use rownum to do so. I am trying to use multiple cursors in my PLSQL statement because i cannot use ORDER BY in my UPDATE statement
There are two tables ASSET and ASSET_GROUP. ASSET_GROUP_ID can point to multple ASSETS. I want to update a new field on ASSET called version and i need to use ASSET_GROUP_ID to do this. I'm trying to use nested loops but my inner most loop isn't retrieving values. I am doing something wrong with my 2nd cursor. I need to use the values retrieved in the 1st cursor to pull values in my 2nd cursor. Then run an update based off the values fetched in my 2nd cursor. Here is my code:
DECLARE
assetgroupid asset.asset_group_id%TYPE;
rownum_version pods.VERSION%TYPE;
assetid asset.ID%TYPE;
/* Cursor declaration: */
CURSOR get_distinct_asssetgroupid IS
SELECT DISTINCT ID FROM asset_group_id;
CURSOR get_rownum_forupdate (assetgroupid IN NUMBER) IS
SELECT ROWNUM, ID FROM asset WHERE asset_group_id = assetgroupid ORDER BY ID;
BEGIN
OPEN get_distinct_assetgroupid;
OPEN get_rownum_forupdate(assgroupid);
LOOP
/* Retrieve each row of the result of the above query into PL/SQL variables: */
FETCH get_distinct_assetgroupid INTO assetgroupid;
EXIT WHEN get_distinct_assetgroupid%NOTFOUND;
LOOP
FETCH get_rownum_forupdate INTO rownum_version, assetid;
EXIT WHEN get_rownum_forupdate%NOTFOUND;
update asset set version = rownum_version where asset.id = assetid;
END LOOP;
end LOOP;
/* Free cursor used by the query. */
CLOSE get_distinct_assetgroupid;
CLOSE get_rownum_forupdate;
END;
I know performance on this bad. It's a one time update for historical data so i just need it to work once and i'm not concerned w/ performance.