Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7675853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:03:48+00:00 2026-05-31T17:03:48+00:00

I want to write a function in PL/SQL, which has a sql-statement as parameter.

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T17:03:49+00:00Added an answer on May 31, 2026 at 5:03 pm

    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.

     create or replace procedure measureTime (statement  IN varchar2 )AS 
    
       l_start_time timestamp default systimestamp;
    
      BEGIN
    
        dbms_output.enable;
    
        EXECUTE IMMEDIATE statement;
        COMMIT; 
    
        dbms_output.put_line('executeTime '|| systimestamp - l_start_time);
    
      END measureTime;
    

    Alternatively if you want it in seconds say change systimestamp - l_start_time to trunc((systimestamp - l_start_time) * 24 * 60 * 60)

    This does seem like an over-worked way to go about things. execute immediate will 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 commit will 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 statement properly 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 parameter statement.

    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.

    select elapsed_time 
      into neededTime 
      from v$sql 
     where sql_text = statement;
    

    Even without syntax errors there are several reasons your statement might not work for instance, sql_text in v$sql only has the first 1,000 characters, so if statement is more than 1k characters long there would be no match. If you have a group by clause, according to the documentation, statistics are not stored.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a function that accepts a parameter which can be either
I want to write a function which can validate a given value (passed as
I want to write a User-Defined function in SQL SERVER which will take a
I want to write a function like equalp, which gives #t for (equalp Xy
i want to write a function that prints multi-dimensional objects which are text (or
I want to write the following function which should be used in an Excel
I want to write a little DBQuery function in perl so I can have
Has anyone tried to dynamically select which properties they want to write to an
I want to write a function in Python that returns different fixed values based
I want to write a function that takes an array of letters as an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.