I have a query as shown below:
TRUNCATE TABLE DNARTLOAD;
COMMIT;
.... <Cursor here> .... <do something>.....
tmp_future_phaseA := substr(tmp_phases,1,1);
tmp_future_phaseB := substr(tmp_phases,2,1);
tmp_future_phaseC := substr(tmp_phases,3,1);
INSERT INTO DNARTLOAD (LOADNAME,KVARATING,PHASE,FULLPHASE) VALUES
(tmp_load_name, tmp_kvarating, tmp_future_phaseA, tmp_phase);
The problem is that the query fails to insert tmp_phases correctly every time. What I’m doing is taking the substring of temp phases (which is never null) and assigning the first, second, and third character to another set of varchar variables. Once they are assigned, I insert the data into a table and commit. The problem is it doesn’t work all the time. It will ALWAYS fill tmp_future_phaseA, B, and C with a character, but when I insert the tmp_phase it will sometimes populate null and sometimes populate the values.
Example Data:
tmp_phase = 'ABC'
tmp_futurephaseA = 'A'
tmp_futurephaseB = 'B'
tmp_futurephaseC = 'C'
Sometimes I’ll get the following:
A 'ABC'
B 'ABC'
C 'ABC'
Other times during the same plsql I’ll get the following:
A NULL
B NULL
C NULL
EDIT: Added for question:
LOOP
tmp_future_phaseA := NULL;
tmp_future_phaseB := NULL;
tmp_future_phaseC := NULL;
tmp_phases := NULL;
FETCH C3 INTO tmp_load_name, tmp_kvarating, tmp_phases;
EXIT WHEN C3%NOTFOUND;
IF LENGTH(tmp_phases) = 3 THEN
tmp_kvarating := tmp_kvarating / 3;
END IF;
IF LENGTH(tmp_phases) = 2 THEN
tmp_kvarating := tmp_kvarating /2;
END IF;
tmp_future_phaseA := substr(tmp_phases,1,1);
tmp_future_phaseB := substr(tmp_phases,2,1);
tmp_future_phaseC := substr(tmp_phases,3,1);
IF tmp_future_phaseA IS NOT NULL THEN
INSERT INTO DNARTLOAD (LOADNAME,KVARATING,PHASE) VALUES (tmp_load_name, tmp_kvarating, tmp_future_phaseA);
END IF;
IF tmp_future_phaseB IS NOT NULL THEN
INSERT INTO DNARTLOAD (LOADNAME,KVARATING,PHASE) VALUES (tmp_load_name, tmp_kvarating, tmp_future_phaseB);
END IF;
IF tmp_future_phaseC IS NOT NULL THEN
INSERT INTO DNARTLOAD (LOADNAME,KVARATING,PHASE) VALUES (tmp_load_name, tmp_kvarating, tmp_future_phaseC);
END IF;
Sometimes your program works, sometimes it doesn’t. This means you have a data problem.
More accurately, you have written a program which caters correctly for data in some states but not in others. There’s absolutely no way we’re going to be able to diagnose that.
So, what you need to do is debug your code. If you have a proper IDE that should offer you a debugging facility. Otherwise you will need to use something like DBMS_OUTPUT to display the input for each trip round the loop and the subsequent outcome, so that you can understand which values your program isn’t handling correctly.