Hey guys. I'm having a problem being able to update a table properly via ROWNUM. This little script is an example of what I'm trying to do. The primary key of the table I want to update is a 12 digit number that increments in the thousands, making it more difficult to select the exact data I want.
I know ROWNUM is a pseudo column generated at runtime, but I'd like to know if there's anyway to update each entry of a table with the data I actually want.
DROP TABLE FKTEST;
CREATE TABLE FKTEST (
ID INT PRIMARY KEY NOT NULL ,
COL1 VARCHAR2(1),
COL2 VARCHAR2(1)
);
DECLARE
BEGIN
FOR LCNT IN 1 .. 10
LOOP
INSERT INTO FKTEST (ID) VALUES (LCNT);
UPDATE FKTEST SET COL1 = DECODE(LCNT,
1, 'A', 2, 'B', 3, 'C', 4, 'D', 5, 'E', 6, 'F', 7, 'G', 8, 'H', 9, 'I', 10, 'J')
WHERE LCNT = ROWNUM;
UPDATE FKTEST SET COL2 = DECODE(LCNT,
1, 'A', 2, 'B', 3, 'C', 4, 'D', 5, 'E', 6, 'F', 7, 'G', 8, 'H', 9, 'I', 10, 'J')
WHERE LCNT = ID;
END LOOP;
END;
.
SELECT * FROM FKTEST;
What I don't understand from this example, is why only the first row with the WHERE LCNT = ROWNUM command is used is updated, and updated to the first value of the DECODE.
Edited by: 921217 on 15-Mar-2012 11:45