I want to write a function in PL/SQL, which has a sql-statement as parameter. The statement can be DDL,DML and DCL
I want to print the time from the executed statement in the console.
I have following code:
create or replace procedure measureTime (statement IN varchar2 )AS
--declarations
neededTime INTEGER;
BEGIN
dbms_output.enable;
EXECUTE IMMEDIATE statement
COMMIT;
SELECT ELAPSED_TIME AS neededTime FROM V_$SQL WHERE SQL_TEXT=statement
dbms_output.put_line('executeTime '|| neededTime);
END measureTime;
But it doesn’t work. What’s wrong?
There’s a simpler way to do it; simply take the start time before you start and take that off the current time when you’ve finished.
Alternatively if you want it in seconds say change
systimestamp - l_start_timetotrunc((systimestamp - l_start_time) * 24 * 60 * 60)This does seem like an over-worked way to go about things.
execute immediatewill be slightly slower as Oracle has to validate the query before it can run each time. Why are you not just executing the query?The
commitwill also commit everything that has happened in that session, which may not be what you want.You should also ensure that you have escaped your
statementproperly to avoid SQL injection.In answer to your question:
Your procedure wouldn’t have compiled as you needed a semi-colon after your
execute immediate. This should not be included in the parameterstatement.Your syntax for your select is also incorrect. As this is PL/SQL and not SQL you need to use the into clause and you were missing a semi-colon at the end of this statement as well.
Even without syntax errors there are several reasons your statement might not work for instance,
sql_textinv$sqlonly has the first 1,000 characters, so ifstatementis more than 1k characters long there would be no match. If you have agroup byclause, according to the documentation, statistics are not stored.