set serveroutput ON

variable max_secs NUMBER

begin
    :max_secs:=to_number(&2)*60;
end;
/

variable c_tt refcursor

DECLARE
    r CLOB;
    t varchar2(30) := 'SQLTT_&1';
BEGIN
    BEGIN
        dbms_sqltune.drop_tuning_task(t);
    EXCEPTION
        WHEN OTHERS THEN
            NULL;
    END;
    
    r := dbms_sqltune.create_tuning_task(task_name => t, sql_id => '&1', time_limit => :max_secs);
    dbms_sqltune.execute_tuning_task(t);    
    
    open :c_tt for select dbms_sqltune.report_tuning_task(t) recommendations from dual;
    
    if :c_tt is NULL then 
        begin
          open :c_tt for select 'N/A' recommendations from dual;
        end;
    end if;

END; 
/ 

print :c_tt

