Skip to Main Content

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Charlene.

Asked: January 08, 2014 - 1:04 am UTC

Last updated: January 08, 2014 - 11:12 pm UTC

Version: 10g

Viewed 1000+ times

You Asked

The following is a procedure in my package. It works, but I know they are not good codes and I want to improve them. I want to use a temporary table, but realize it is not a good way in Oracle. I want to use FOR LOOP and cursor, but should I put these mini procedures into a physical table first? Your advice would be greatly appreciated.
procedure mapData
is
begin
...
if g_err_found is null then
doSPBPERS;
end if;

if g_err_found is null then
doSPRADDR_TELE;
end if;

if g_err_found is null then
doGOREMAL(g_scRec.EMAIL);
end if;

if g_err_found is null then
doSRBRECR('COMA');
end if;

if g_err_found is null then
doSARADAP('COMA');
end if;

if g_err_found is null then
doSORHSCH_SORPCOL('COMA');
end if;

if g_err_found is null then
doSORLCUR_SORLFOS('COMA');
end if;

if g_err_found is null then
doSARRSRC('COMA');
end if;

if g_err_found is null then
doSRRRSRC('COMA');
end if;
--
if g_err_found is null then
doSORTEST;
end if;

if g_err_found is null then
popSARCHKLrec('COMA');
end if;

if g_err_found is null then
popSORCONTrec('COMA');
end if;

if g_err_found is null then
doSARACMT('COMA');
end if;
--
if g_err_found is null then
popGORPRACrec;
end if;
--
if g_err_found is null then
popGOBINTLrec;
end if;
--
if g_err_found is null then
popGORVISArec;
end if;
--
if g_err_found is null then
doSARQUAN;
end if;-- ADDED 11/3/2010

-- end if;
--
exception
when others then
dbms_output.put_line('error in mapData: ' || SQLERRM);
doError(' mapData ',SQLERRM);
end mapData;

and Tom said...

I hate your code:

https://www.google.com/search?q=site%3Aasktom.oracle.com+"i+hate+your+code"

your when others is horrible, all it does is hide information from you.


and it looks like it is just this sort of "error handling" that is causing your code to look as bad as it does.

Based on the name "g_err_found" - I would say that is a global variable (bad bad technique) and the routines you are calling will set it to a non-NULL value when some error occurs, like a return code (that is so 1980's in coding style, return codes are). You need to inspect this "return code" every time before doing something to make sure you are supposed to do something. This return code is probably being set in another WHEN OTHERS handler that looks like:


begin
...
exception when others then g_err_found := .....;
end;

without a raise or raise_application_error.

IF instead of setting a return code, you simply let the EXCEPTIONS propogate - your code would simply be:


procedure mapData
is
begin
     doSPBPERS;
     doSPRADDR_TELE;
     doGOREMAL(g_scRec.EMAIL);
     doSRBRECR('COMA');
     ...
     popGORVISArec;
     doSARQUAN;
exception
when others 
then
    doError(' mapData ',SQLERRM);
    RAISE;
end mapData;



so the code in doWHATEVER would raise an exception (that is why they exist..) instead of setting a return code. If one of them error out - there you go, you jump over the rest, go to your when others, log the error using "doError" and then RE-RAISE IT (very important, you RE-RAISE IT)


stop doing return code. start using exceptions.


and read this to see why you want your plsql blocks to propagate exceptions all of the way back to the client:

http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:7804346100346071863

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

tuning the query

Babu, February 22, 2014 - 2:24 pm UTC

Hi tom,
below is the query i tuned from old one.but still it is taking 15 minutes.is there any way to tune
better.or it seems the better one.(tables contain rows in lacs nearly in millions).can you tell
me...please please...

SELECT /*+ leading(r.run r.rsk )*/
R.Uprice,
R.Aprice,
R.Pprice,
R.exeID,
R.exeDATE,
R.SID,
R.PVprice,
R.PV,
R.PREMIUMPV,
R.BOOK,
R.Makeup,
R.MAkeupprice,
T.businessTYPE,
T.REASON,
T.businessTABLE,
T.businessID,
'20 Dec 2013' AS ARC_date,
'LDN' AS ARC_place,
'2013-12-23 04.52.56' AS ARC_DATETIME
FROM
OWNER.totalRISK R,
OWNER.TRADE T,
(SELECT Name
FROM OWNER.BOOK
WHERE BookGroupYN = 'N'
START WITH Parent IN ('FX ACE ' , 'harward ' , 'harvard_LTFX' , 'FX Utility' , 'GCS FX' , 'HRE
London' , 'MGT London' , 'proper London' , 'Prop Strategy' , 'Spot ' , 'SYSTEMATIC')
CONNECT BY PRIOR Name=Parent
) b
WHERE
R.SID = T.SID
AND T.VERSION = R.VERSION
AND R.Book = b.name
AND R.Department='FX Cash Controlling'
AND R.Location = 'London'
AND R.RunType = 'EOD'
AND R.RUNDATE = '20 Dec 2013'


More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library