Search code examples
hanahana-sql-script

How to get the last executed SQL statement in HANA?


I am inside a SQLScript procedure and would like to return the last SQL Statement from this procedure, like the last rowcount:

/********* Begin Procedure Script ************/ 
BEGIN 
    select 1 as "my_data" from dummy;
    select '::last_SQL' as "last executed sql" from dummy;
    select ::ROWCOUNT as "rowcount" from dummy;
END;
 /********* End Procedure Script ************/

The column "last executed SQL" should be populated with select 1 as "my_data" from dummy in this case. Is there any variable holding the last statement (or any easy way to retrieve the query plan)?


Solution

  • Maybe you can query sys.m_sql_plan_cache system view

    Please check following SELECT statement

    select
        statement_string, last_execution_timestamp
    from SYS.M_SQL_PLAN_CACHE 
    where user_name = 'KODYAZ' 
    order by last_execution_timestamp desc;
    

    I believe you can improve the query by introducing new filter criteria.