CREATE TABLE test_tab (
s_id NUMBER(10),
e_id NUMBER(10),
active_flg NUMBER(1)
);
INSERT INTO test_tab VALUES(1,11,1);
INSERT INTO test_tab VALUES(2,11,1);
INSERT INTO test_tab VALUES(3,11,0);
INSERT INTO test_tab VALUES(4,12,1);
INSERT INTO test_tab VALUES(5,12,1);
COMMIT;
Tool Used: SQL Developer(18c)
I want to update the active_flg column by identifying the value 0 in it. Suppose, for e_id 11 we have 3 rows so first it should check if for e_id 11 is there any 0 active_flg if 0 exists for that particular e_id then it should update entire rows for e_id 11 to 0. If there are no entries for active_flg then it should not update anything.
My Attempt:
SET SERVEROUTPUT ON;
DECLARE
lv_row test_tab%ROWTYPE;
BEGIN
FOR i IN (SELECT * FROM test_tab)
LOOP
SELECT * INTO lv_row FROM test_tab WHERE e_id = i.e_id AND active_flg = 0;
UPDATE test_tab SET active_flg = 0 WHERE active_flg = 0;
END LOOP;
END;
But I am getting no data found error.
Expected output:
+------+------+------------+
| s_id | e_id | active_flg |
+------+------+------------+
| 1 | 11 | 0 |
| 2 | 11 | 0 |
| 3 | 11 | 0 |
| 4 | 12 | 1 |
| 5 | 12 | 1 |
+------+------+------------+