Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Exit out of a SQL script

USER101Jun 12 2025

Hello

I am trying to write a script for SQL TUNING ADVISOR.. The requirement I have is that if the SQL ID is not found in the cursor cache, then the script must exit.. Just gave it a few tries but seems the script doesn't exit once the SQL ID is not found in the cache.

Any advise please ?


accept SQL_ID prompt "Enter the SQL ID :"
declare 
l_count integer;
begin
select count(*) into l_count where sql_id = '&&SQL_ID';
if l_count < 1
then 
    dbms_output.put_line("SQL DOES NOT EXIST IN CURSOR CACHE..Try from AWR";
    raise PLAN_NOT_FOUND;
end if;
EXCEPTION
WHEN PLAN_NOT_FOUND then
    << EXIT THE SCRIPT >>

DECLARE
l_sql_tune_task_id VARCHAR2(100);
BEGIN
l_sql_tune_task_id := DBMS_SQLTUNE.create_tuning_task (
sql_id => '&&SQL_ID',
scope => DBMS_SQLTUNE.scope_comprehensive,
time_limit => 500,
task_name => '&&SQL_ID_tuning_task11',
description => 'Tuning task1 for statement &&SQL_ID');
DBMS_OUTPUT.put_line('l_sql_tune_task_id: ' || l_sql_tune_task_id);
END;
/
EXEC DBMS_SQLTUNE.execute_tuning_task(task_name => '&&SQL_ID_tuning_task11');


select dbms_sqltune.report_tuning_task('&&SQL_ID_tuning_task11') from dual;
Comments
Post Details
Added on Jun 12 2025
9 comments
168 views