PLSQL Exception handling in GET_FILE - oracle

Trying to handle exception if the input file received has invalid records or no data. The below exception runs in loop even though the input file has only 1 record.
LOOP
BEGIN
UTL_FILE.GET_LINE(a_UIN_input_file,a_UIN_file);
a_rec := substr(a_uin_file,1,9);
EXCEPTION
WHEN NO_DATA_FOUND THEN
a_error_file := utl_file.fopen('TAMUOUT','PWT_TEST5_ERROR.txt','w');
utl_file.put_line(a_error_file,'Bad UIN Read ' ||SQLERRM||'\n');
utl_file.fclose(a_error_file);
-- EXIT;
END;
END LOOP;
FOR rec IN get_details_4_uin_c(a_rec) LOOP
a_pidm :=fwt_get_pidm_from_uin(a_rec);
a_bill_hours := fwt_get_enrolled_hours(a_pidm,in_term_code);
utl_file.put_line(a_out_file,a_parm,
autoflush=>TRUE);
END LOOP;
utl_file.fclose(a_uin_input_file);

UTL_FILE.GET_LINE raises the NO_DATA_FOUND exception when you have reached the end of the file and then keep on trying to read more.
So your exception handler for NO_DATA_FOUND should exit the loop. But within the loop, if you read a line successfully, then run your loop on that.
I believe the code believe will help you get to your solution.
BEGIN
LOOP
BEGIN
UTL_FILE.get_line (a_uin_input_file, a_uin_file);
a_rec := SUBSTR (a_uin_file, 1, 9);
FOR rec IN get_details_4_uin_c (a_rec)
LOOP
a_pidm := fwt_get_pidm_from_uin (a_rec);
a_bill_hours := fwt_get_enrolled_hours (a_pidm, in_term_code);
UTL_FILE.put_line (a_out_file, a_parm, autoflush => TRUE);
END LOOP;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
UTL_FILE.fclose (a_uin_input_file);
EXIT;
END;
END LOOP;
END;

Related

Unable to get dbms_sql.column_value

I'm trying to read a query from a table and process it with DBMS_SQL package. It parses correctly. I can also see the columns, which occurs in a query. But I'm not able to fetch results. In debug mode It throws the exception while trying to getting dbms_sql.column_value (please see the code below).
/*--------------------------------------------------------------------------------------------------------------------*/
PROCEDURE MY_PROC( nCSV_EXP_CFG_ID IN NUMBER,nN1 IN NUMBER DEFAULT null )
/*--------------------------------------------------------------------------------------------------------------------*/
as
l_ntt_desc_tab dbms_sql.desc_tab;
nCursorId INTEGER;
nColCnt INTEGER;
nRowCnt INTEGER;
rCSV_EXP_CFG CSV_EXP_CFG%ROWTYPE;
TYPE rowArray IS VARRAY(20) OF VARCHAR2(255);
colVal rowArray;
BEGIN
SELECT * INTO rCSV_EXP_CFG
FROM CSV_EXP_CFG
WHERE
CSV_EXP_CFG_ID = nCSV_EXP_CFG_ID;
nCursorId:=dbms_sql.open_cursor;
dbms_sql.parse(nCursorId, rCSV_EXP_CFG.EXPORT_TABLE, dbms_sql.native);
IF nN1 IS NOT NULL THEN DBMS_SQL.BIND_VARIABLE (nCursorId, 'n1', nN1); END IF;
dbms_sql.describe_columns(nCursorId, nColCnt, l_ntt_desc_tab);
FOR i IN 1..nColCnt LOOP
DBMS_OUTPUT.PUT_LINE( l_ntt_desc_tab(i).col_name);
--DBMS_SQL.DEFINE_COLUMN(nCursorId, i, colVal(i), 255);
END LOOP;
nRowCnt:=dbms_sql.execute(nCursorId);
LOOP
EXIT WHEN dbms_sql.fetch_rows(nCursorId) = 0;
FOR i IN 1..nColCnt LOOP
--here I'm getting the exception
dbms_sql.column_value(nCursorId, i, colVal(i));
END LOOP;
END LOOP;
Dbms_sql.close_cursor(nCursorId);
EXCEPTION WHEN OTHERS THEN
NULL;
END MY_PROC;
What am I doing wrong?

exception handling in cursors

This question is not duplicate. (i m using oracle 10g)
I searched a lot but my problem seems to be diffrent
I have following cursor
DECLARE
-- Some declarations
--
CURSOR C1 IS
-- some select statements
Begin
for r in c1 loop
-- Insert queries
DBMS_OUTPUT.PUT_LINE('INSERTED records');
End loop;
EXCEPTION WHEN others THEN
dbms_output.put_line('error' || SQLERRM);
END;
As per the above cursor, whenever error occures during insert , error is printed on output and execution stops.
whereas It should continue looping.
I tried adding exception block inside loop but still not working
Then you have to use another begin - exception - end within de loop.
Something like this.
DECLARE
-- Some declarations
--
CURSOR C1 IS
-- some select statements
Begin
for r in c1 loop
BEGIN
-- Insert queries
DBMS_OUTPUT.PUT_LINE('INSERTED records');
EXCEPTION
WHEN OTHERS
THEN
dbms_output.put_line('error in LOOP' || SQLERRM);
END;
End loop;
EXCEPTION WHEN others THEN
dbms_output.put_line('error' || SQLERRM);
END;

Oracle Forms - Error 103, Encountered the symbol "END"

I'm using Oracle Forms Builder 11.
My code:
declare
type myType is varray(3000) of my_table%rowtype;
myAsset myType:=myType();
i number;
n number;
exNoInvNum exception;
begin
go_block('my_block');
first_record;
i:=1;
loop
myAsset.extend();
myAsset(i).hqId:=:my_block.hqId;
myAsset(i).deptId:=:my_block.deptId;
myAsset(i).invNum:=:my_block.invNum;
exit when :system.last_record='TRUE';
i:=i+1;
next_record;
end loop;
go_block('my_block');
first_record;
loop
if (:my_block.linkedInvNum is not null) then
n:=0;
select count(*) into n
from my_table s
where s.invNum=:my_block.linkedInvNum
and s.hqId=:my_block.hqId
and (s.deptId=:my_block.deptId
or (s.deptId is null and :my_block.deptId is null));
if (n=0) then
for i in myAsset.first .. myAsset.last loop
if (myAsset(i).invNum=:my_block.linkedInvNum
and myAsset(i).hqId=:my_block.hqId
and (myAsset(i).deptId=:my_block.deptId
or (myAsset(i).deptId is null and :my_block.deptId is null))) then
n:=1;
end if;
end loop;
end if;
if (n=0) then
raise exNoInvNum;
else
commit_form;
go_block('my_table');
clear_block(no_validate); set_item_property('my_block.generate_excel',ENABLED,property_true); set_item_property('my_block.process_data',ENABLED,property_false);
end if;
end if;
exit when :system.last_record='TRUE';
next_record;
end loop;
exception
when exNoInvNum then
message('No existing inventory number!');
when others then
null;
end;
end;
I get Error 103 ad line 2, column 1: Encountered the symbol "END"
I checked my code for typo errors, missing semicolumns and similar stuff, but it looks like everything is fine.
Any ideas?
I get Error 103 ad line 2, column 1: Encountered the symbol "END"
Well, you do have an extra END keyword in your PL/SQL block.
end;
end;
The syntax for an anonymous PL/SQL block is:
DECLARE
...
BEGIN
...
EXCEPTION
...
END;
And, this:
when others then
null;
is itself a bug in your code.
A when others is almost always a BUG unless it is immediately followed by a RAISE. Remember, for errors, RAISE –> CATCH –> HANDLE. why do we need an exception handler? To catch the errors, log them(optional), and finally do something about them.
Read WHEN OTHERS THEN NULL – A bug

Continue loop in reading excel rows even when an exception occurred in PL/SQL

Here's the overview of the code:
PROCEDURE
BEGIN
WHILE LOOP --loop each row
--Read each row and add in table
COMMIT;
END LOOP;
EXCEPTION
WHEN OTHERS
--log error
ROLLBACK;
END;
Ex: There are 5 lines. The 3rd line has an error in it. The program stops right after it reads the error line, making the other lines next to it not evaluated/read. My goal is after it went to the exception, it will output the line and continue with reading the other lines. So how do I return back to the loop if it went to the exception?
Try this, then you will cache error only in inner anonymous block:
PROCEDURE
BEGIN
WHILE LOOP --loop each row
BEGIN
--Read each row and add in table
COMMIT;
EXCEPTION
WHEN OTHERS
--log error
ROLLBACK;
END;
END LOOP;
END;
Try using a independent PL/SQL block in the loop itself.
BEGIN
WHILE LOOP --loop each row
BEGIN
--Read each row and add in table
COMMIT;
EXCEPTION --Catch the exception here and print the data and it will move on
END;
END LOOP;
EXCEPTION
WHEN OTHERS
--log error
ROLLBACK;
END;

Continue on error in loop

The loop below is callign a proc that does various 'things'
If it should throw an exception it also 'raises' it. I want to catch it and ignore it and allow the loop to continue processing the next value in the array.
Thanks
WHILE indx IS NOT NULL LOOP
table_dump_csv(tableList(indx), tableList(indx) || '.csv');
indx := tableList.NEXT(indx);
END LOOP;
One possible approach...
WHILE indx IS NOT NULL LOOP
BEGIN
table_dump_csv(tableList(indx), tableList(indx) || '.csv');
EXCEPTION
WHEN OTHERS THEN
-- Handle/Ignore the exception as appropriate
END;
indx := tableList.NEXT(indx);
END LOOP;
Alternatively you could change the procedure into a function which returns a success/failure code.

Resources