cancel
Showing results for 
Search instead for 
Did you mean: 

Issues in Building Dynamic SQL Query in HANA Sql

Former Member
0 Kudos

hi all,

I am new to HANA and got a requirement as under:

I have a cursor_1 and from it I built another cursor_2 and looping the cursor_2 inside of cursor_1. and the logic is something like this:

      For tab1 as cursor_1....

          For tab2 as cursor_2....

               ->here I get 3 fields from the tab2 as tab2.hana_table, tab2.field1 and tab2.field2 -> these will dynamically changed...

               now, I need to go to the respective tab2.hana_table and fetch the data present in tab2.field1 and tab2.field2 -> which are also dynamic...

               I used different ways of selects as under:

1)va_sql := 'select' || '' || tab2.field1 || '' || tab2.field2 || '' 'into  :va_field1_out, :va_field2_out' || '' || 'from' || '' || tab2.hana_table;   

     execute immediate(va_sql);

2)va_sql := 'select' || ' ' || ‘“’ ||tab2.field1 || ‘“’ || ‘,’ || ‘“’ || tab2.field2 || ‘“’ || 'into  :va_field1_out, :va_field2_out' || '' || 'from' || ' ' || tab2.hana_table;  

     execute immediate(va_sql);

3)va_sql := 'select "tab2.field1" , "tab2.field2" from "tab2.hana_table" into  ":va_field1_out" , ":va_field2_out" ';

     execute immediate(va_sql);

4)execute immediate 'select ' || :va_field1_in || ', ' || :va_field2_in || 'from '  || :va_hana_table || 'into' || ' ' || :va_field1_out || ', ' || :va_field2_out ;  

5)exec 'select ' || tab2.field1 || ' ,' || tab2.field2 || ' from '  || tab2.hana_table || ' into ' || :va_field1_out || ' ,' || :va_field2_out;

I was getting issues like: INTO can't be used in the EXEC or EXECUTE IMMEDIATE and finally I was not able to get values into local variables of the Stored procedure.... Even I tried to achieve the requirement by creating another procedure and thereby get the dynamic table contents, but it did not work out fine....

Please provide inputs on how to write a dynamic select query with fields tab2.hana_table, tab2.field1 and tab2.field2....

Thanks for your help in advance...

Regards,

Vishnu

Accepted Solutions (0)

Answers (1)

Answers (1)

lbreddemann
Active Contributor
0 Kudos

This seems to be a duplicate/twin of , so let's close this one.

The question about EXEC and INTO can be answered from the documentation anyhow.