Displaying Elapsed Time: The difference between two dates If you ever need to display the number of hours

, minutes and seconds between two events, this will help. This little package can be called once to record the start time, then later to display the elapsed time since the initial call. It has two modes of displaying the time, depending on whether the elapsed time is more or less than 10 seconds. When it is less than ten seconds, it displays the seconds down to 0.01 second. This process even corrects for some truncation errors caused by the floating point arithmetic used to extract hours, minutes and seconds. The output looks like one of these: Elapsed time = 9.25 seconds Elapsed time (HH:MI:SS) = 00:02:15 Call Elapsed_Time as follows: PK2.ELAPSED_TIME(T,'start'); -- to initialize PK2.ELAPSED_TIME(T); -- to retrieve the formatted text. Here is the package:

CREATE OR REPLACE PACKAGE PK2 IS PROCEDURE ELAPSED_TIME (T IN OUT VARCHAR2,MODE1 IN VARCHAR2 DEFAULT NULL); END PK2; . / SHOW ERRORS PACKAGE PK2 CREATE OR REPLACE PACKAGE BODY PK2 IS -------------------------------------START_TIME DATE; START_TIMEN NUMBER; -PROCEDURE ELAPSED_TIME --------------------------------(T IN OUT VARCHAR2,MODE1 IN VARCHAR2 DEFAULT NULL) IS ET NUMBER; -- Elapsed run time ETN NUMBER; -- Elapsed time to hundredths of seconds BEGIN IF MODE1 IS NOT NULL THEN START_TIME := SYSDATE; START_TIMEN := DBMS_UTILITY.GET_TIME; ELSE ET := SYSDATE - START_TIME + 0.000001; ETN := (DBMS_UTILITY.GET_TIME - START_TIMEN) / 100; IF ETN >= 10 THEN T:='Elapsed time (HH:MM:SS) = ' ||Ltrim(To_char( Trunc(ET*24), '99900'))||':' ||Ltrim(To_char(Mod(Trunc(ET*1440),60), '00')) ||':' ||Ltrim(To_char(Mod(Trunc(ET*86400),60) ,'00')); ELSE T:='Elapsed time =' ||To_char(ETN,'0.00')||' seconds'; END IF; END IF; END ELAPSED_TIME; -END PK2; -- End of Package Body -. / SHOW ERRORS PACKAGE BODY PK2 Here is a script to test the process:

6.. DBMS_Output. If you pull the mouse down a line.. Use the special SQL Plus copy/paste shortcut to quickly re-execute commands. . This way when you open the file. the alternate Ctrl-Insert to copy and Shift-Insert to paste work great. 5. and the file starts with the "CREATE OR REPLACE PROCEDURE. (PFE does not store line numbers with the file.100 loop X := Sysdate.) With PFE's line numbers and my "no blank lines" rule below. and ends with 3 lines: a period alone in column 1 (terminates SQL Plus edit mode). If you have a command on your SQL Plus window that you need to type in again. Use the features in this window to navigate to the directory you want to use. Use a good editor. just do this: Highlight the command using the standard Windows method -Press the left mouse button. named ~a.Spend some time looping End loop. it corresponds to the number in file you are editing. Then open a file -. End. you can find the pl/sql errors easily. the directory you are in becomes the current working directory.SQL. or you can set preferences to always supply them. You can change the working directory where SQL Plus looks for scripts to execute from within SQL Plus. rather than re-typing it. So here are some things I have found to be really helpful in getting the job done. It is a great aid to working with SQL Plus.sql script in the directory simply by typing @NAME where NAME is a file named NAME. and ending with no Return at the end.Set SERVEROUTPUT ON SIZE 20000 Declare T Varchar2(40). NEVER put blank lines in your script. a slash (to run the script). This works for me because I most always create a package or procedure. This will copy and paste the text onto the command line. To do this. when the PL/SQL compiler gives you a line/column where the error occurs.Put_Line(T). beginning with two dashes. Instead.. to cut or paste text using the keyboard keys.sql (so it is always first). / Using SQL Plus While I would rather write Forms if given a choice.ELAPSED_TIME(T).'start'). turn on line numbering -. But before releasing the left button." line. and the command: SHOW ERRORS PROCEDURE XXX Synchronizing line numbers is really important when your procedure is hundreds or thousands of lines long. 1. For I in 1. and drag the mouse across the text. That way. click the right button. You can run any . causing SQL Plus to execute the command. the standard Windows Ctrl-C and Ctrl-V keys do not work -. . PK2.ELAPSED_TIME(T. it will also send a Return. -. 2. 4. In SQL Plus. I often find myself with the task of writing some pretty hairy pl/sql procedures.you get garbage when you try to paste. In PFE. Once you open a file. Begin PK2. make sure its last line is a comment.there is an icon on PFE's toolbar. X Date. If you create a small file to open. SQL Plus will return back to the SQL> prompt. 3. You can use this method repeatedly to copy and paste parts of a line or several lines at once on your screen.I keep a little one with only several lines in it. you use the File -> Open pull-down menu selection to get to the Open File window. Always use two dashes as a comment line instead.

Paste all the lines of the select statement from my PFE edit session.This select interprets the output of an -.enter is: delete plan_table.Note that this script does this at the end. object_node varchar2(128). the table Plan_Table must exist in -. If it does not.sql to create it. All you need is to -.can stop the looping by pressing Ctrl-C. step by step.SQL script ----- If you don't have UTLXPLAN. Any time I want to view the explain plan sequence for some difficult SQL select.SQL script ----set echo off set feedback off -. you should either run this script -.script C:\ORAWIN\RDBMS71\ADMIN\utlxplan.your schema.In order to run Explain Plan. object_type varchar2(30). object_instance numeric. operation varchar2(30).End of EXPLAIN. position numeric. remarks varchar2(80). Execute the statement -. -----.OR be sure to delete everything in Plan_Table. you need to remove the blank spaces from the last line in the script. I do the following in a SQL Plus session: Type EXPLAIN PLAN FOR and press Enter. statement. timestamp date. just run this: create table PLAN_TABLE ( statement_id varchar2(30). then run (from SQL Plus) the sql -. -update plan_table set statement_id='A' where statement_id is null..7.SQL.2*(level-1))||operation||' '||options||' '||object_name ||' '||decode(id. set feedback on set echo on -----. You -.SQL script below does all the rest. delete plan_table. optimizer varchar2(255). options varchar2(30). object_name varchar2(30). commit. -. id numeric.. In a SQL Plus script.If you don't clear out plan_table.Begin EXPLAIN. named Explain.SQL. parent_id numeric. --. then this script loops.(just press enter) Type @EXPLAIN The Explain. . Now I just wish I could find an explanation somewhere that would tell me exactly what the Explain Plan output means..0. -.Each time you run Explain Plan. Explain Plan Tips I have provided below a short script that I keep in my SQL Plus directory.'Cost = '||position) "Query Plan" From plan_table Start with id = 0 and statement_id ='A' connect by prior id = parent_id and statement_id ='A'. search_columns numeric. -Select lpad(' '. object_owner varchar2(30).EXPLAIN PLAN FOR Select. -. other long). if you ever run the script and it ends with the message "Input truncated to nn characters".

My reasons-..). Don't raise your own exceptions in your procedures. It just confuses the process. and you have to handle it (Exception when. 2. When you RAISE . especially for someone else trying to read your code.. To handle a user-created exception. making all the code easier to follow.. Then the calling procedure can test the Return_Code value immediately after the call.they break the rules of structured programming..). use a parameter -. it isn't immediately clear to the next person reading your code that the sub procedure can raise such an error. If you use an exception in the caller. . To pass back an error condition to a calling procedure.something like Return_Code. the line number returned with the exception error points to the exact place the error occurred. it is just a disguised GOTO statement. Don't code Exception handlers unless you have a clear and compelling reason to do so. how can you ever guess what problem might occur? By leaving out the exception code and by following my no blank lines rule. It is so much easier and clearer to take care of a special condition in-line in your code. you have to raise it (GOTO the end).PL/SQL coding 1. you have to write special code in three places in your procedure: you have to declare it (Pragma exception init. which forces processing immediately to jump to the end of the procedure. Unless there is a specific problem you are trapping.

Master your semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master your semester with Scribd & The New York Times

Cancel anytime.