Paul Mynarik

IT Consultant

How-To use data from a dynamic SQL in a procedure

If you are using in your procedure a data load from the HANA model you may need to parametrize for example the Placeholders.

I’m not aware about any possibility how to do that directly in the procedure but I may also be wrong, so if you know about the way, please leave the comment. Otherwise please see, how the local table can be used for that purpose.

In the following example we will need to use variable for the placeholder “YEAR”:

lt_table = SELECT 
  "attribute",
  SUM("measure") as "measure"
FROM 
  "HANA_MODEL"('PLACEHOLDER' = ('$$YEAR$$', '2020')) 
WHERE 
  "attribute" = 'value'
GROUP BY 
  "attribute";

This can be done by using a local temporary table, filling it with the data and selecting it to the table variable or directly use it:

declare lv_sql nclob;
declare lv_year varchar(4);
declare lv_attribute_filter nvarchar(255);
declare lt_table table(
  "attribute" nvarchar(255),
  "measure" decimal(15,3));


-- Create local tmp table
CREATE LOCAL TEMPORARY TABLE #tmp_table(
  "attribute" nvarchar(255),
  "measure" decimal(15,3));
  
-- Fill local tmp table
lv_sql = 'insert into #tmp_table SELECT "attribute",';
lv_sql = :lv_sql || ' SUM("measure") as "measure"';
lv_sql = :lv_sql || ' FROM "HANA_MODEL"(''PLACEHOLDER'' = (''$$YEAR$$'', ''' || :lv_year || '''))'; 
lv_sql = :lv_sql || ' WHERE "attribute" = ''' || :lv_attribute_filter || ''' ';
lv_sql = :lv_sql || ' GROUP BY "attribute";';
exec :lv_sql;
  
-- Fill table variable from the local tmp table
lt_table = select * from #tmp_table;
DROP TABLE #tmp_table;

-- Use data from table
if :lt_table ."attribute"[1] = '' Then
  -- Any logic...
end if;

 

Leave a Reply