Skip to Main Content
  • Questions
  • Is it feasible to use Dynamic SQL for a statement with "into :num" clause?

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Paul.

Asked: August 01, 2016 - 10:00 pm UTC

Last updated: August 02, 2016 - 3:02 pm UTC

Version: 11.2.0.4

Viewed 1000+ times

You Asked

We have the following statement executed in the database by the outside-of-the-database application:

"SELECT COUNT(*) INTO :num FROM TABLE1"

I'd like to be able to execute this statatement at will via Dynamic SQL to preserve the same sql_id.

We are deploying SQL baselines and ability to run any statement and check whether v$sql.sql_plan_baseline column is populated, is a plus. We don't have any issues using Dynamic SQL to execute any other "regular" statement, but is it even feasible to execute the above statement with the "INTO :num" clause in PL/SQL?

and Chris said...

I'd like to be able to execute this statatement at will via Dynamic SQL to preserve the same sql_id.

I don't understand. If you're using dynamic SQL, that implies the text of the query changes. And if the text changes, you'll have a different SQL ID!

If you're not changing the statement, why do you need dynamic SQL?

Anyhow, if you want to get the values from dynamic SQL in PL/SQL you can return the values from execute immediate with something like this:

var c number;

begin
  execute immediate 'select count(*) from dual' into :c;
end;
/

print :c

         C
----------
         1


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

More to Explore

Performance

Get all the information about database performance in the Database Performance guide.