I have writen a pl/pgsql function to trucate tables. HIST_CLEANUP_TBL has 5 table_name in it. Out of 5, 1 table_name doesn't exist in the database.
Need is: It has to check all tables. If table does not exist's write the error to a error table and truncate the remaining table.
However, here when ever the unavailable table comes in the loop the function getting terminated.
How to captuture the error code and text in a variable and how to continue the loop though 1 table does not exist.
We are using postgresql 8.2
BEGIN
for recordvar in
SELECT 'truncate table ' || TABLE_NAME as SQRY FROM HIST_CLEANUP_TBL
loop
QUERY1 := recordvar.SQRY;
RAISE NOTICE 'QUERY: (%)', QUERY1;
BEGIN
execute QUERY1;
RAISE NOTICE 'QUERY EXECUTED SUCCESSFULLY';
EXCEPTION
WHEN others
THEN
--GET STACKED DIAGNOSTICS
ERR_NUM := SQLSTATE;
ERR_MESG := SQLERRM ;
RAISE NOTICE 'ERROR WHILE EXECUTING THE QUERY : %',ERR_NUM;
insert into Refresh_Error_tbl(sqlerr_code,sqlerr_desc,sql_desc, INSERT_DT,refresh_load_id) values(ERR_NUM,ERR_MESG,QUERY1,current_timestamp,'1');
END;
end loop;
END;