Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Use a variable as a table name with NATIVE SQL

Former Member
0 Likes
4,368

Hi all,

I am trying to execute a SELECT statement in order to fetch data from an external Oracle DB table to SAP with the following instructions:

EXEC SQL.

SELECT cityfrom, cityto

INTO STRUCTURE :wa

FROM spfli

WHERE mandt = :sy-mandt AND

carrid = :p_carrid AND connid = :p_connid

ENDEXEC.

However, I need to indicate the external table name from a variable instead of the solution above. That is, declaring a variable and store the name of the table (e.q. spfli) in it. The resulting ABAP code would be something like:

EXEC SQL.

SELECT cityfrom, cityto

INTO STRUCTURE :wa

FROM <VARIABLE>

WHERE mandt = :sy-mandt AND

carrid = :p_carrid AND connid = :p_connid

ENDEXEC.

Does anybody know if is possible to do that?

If not, is there any other solution?

Thank you in advance

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,397

Yes, as Suhas said, you could use the ADBC API and his class CL_SQL_CONNECTION to achieve this...

Here is a small example:

 
PARAMETERS: p_carrid TYPE spfli-carrid,
                           p_connid TYPE spfli-connid.

DATA:
  l_con_ref      TYPE REF TO cl_sql_connection,
  l_stmt         TYPE string,
  l_stmt_ref     TYPE REF TO cl_sql_statement,
  l_dref         TYPE REF TO data,
  l_res_ref      TYPE REF TO cl_sql_result_set,
  l_col1         TYPE spfli-carrid,
  l_col2         TYPE spfli-connid,
  l_wa           TYPE spfli.

CONSTANTS:
  c_tabname  TYPE string VALUE 'SPFLI'.


* Create the connecction object
CREATE OBJECT l_con_ref.

* Create the SQL statement object
CONCATENATE 'select * from' c_tabname 'where carrid = ? and connid = ?'
       INTO l_stmt SEPARATED BY space.                           "#EC NOTEXT

l_stmt_ref = l_con_ref->create_statement( ).

* Bind input variables
GET REFERENCE OF l_col1 INTO l_dref.
l_stmt_ref->set_param( l_dref ).
GET REFERENCE OF l_col2 INTO l_dref.
l_stmt_ref->set_param( l_dref ).

* Set the input value and execute the query
l_col1 = p_carrid.
l_col2 = p_connid.

l_res_ref = l_stmt_ref->execute_query( l_stmt ).

* Set output structure
GET REFERENCE OF l_wa INTO l_dref.
l_res_ref->set_param_struct( l_dref ).

* Show result
WHILE l_res_ref->next( ) > 0.
  WRITE: / 'Result:', l_wa-carrid, l_wa-connid.
ENDWHILE.

* Close the result set object
l_res_ref->close( ).

Otherwise you can also use the FM DB_EXECUTE_SQL...

Kr,

m.

10 REPLIES 10
Read only

former_member226519
Active Contributor
0 Likes
2,397

you could try to create a FORM dynamically at runtime with your SQL statement and perform that FORM.

Read only

darren_bambrick2
Active Participant
0 Likes
2,397

Hi

Can you try something like this

data: lv_table type string value 'theTable'.

select * from :lv_table

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
2,397

Hello,

AFAIR you cannot use dynamic tokens in Native SQL. In order to cater to your requirement you have to use [ADBC|http://help.sap.com/abapdocu_702/en/abenadbc.htm] classes.

BR,

Suhas

PS: IMHO ADBC classes are easier to use & maintain

Read only

Former Member
0 Likes
2,398

Yes, as Suhas said, you could use the ADBC API and his class CL_SQL_CONNECTION to achieve this...

Here is a small example:

 
PARAMETERS: p_carrid TYPE spfli-carrid,
                           p_connid TYPE spfli-connid.

DATA:
  l_con_ref      TYPE REF TO cl_sql_connection,
  l_stmt         TYPE string,
  l_stmt_ref     TYPE REF TO cl_sql_statement,
  l_dref         TYPE REF TO data,
  l_res_ref      TYPE REF TO cl_sql_result_set,
  l_col1         TYPE spfli-carrid,
  l_col2         TYPE spfli-connid,
  l_wa           TYPE spfli.

CONSTANTS:
  c_tabname  TYPE string VALUE 'SPFLI'.


* Create the connecction object
CREATE OBJECT l_con_ref.

* Create the SQL statement object
CONCATENATE 'select * from' c_tabname 'where carrid = ? and connid = ?'
       INTO l_stmt SEPARATED BY space.                           "#EC NOTEXT

l_stmt_ref = l_con_ref->create_statement( ).

* Bind input variables
GET REFERENCE OF l_col1 INTO l_dref.
l_stmt_ref->set_param( l_dref ).
GET REFERENCE OF l_col2 INTO l_dref.
l_stmt_ref->set_param( l_dref ).

* Set the input value and execute the query
l_col1 = p_carrid.
l_col2 = p_connid.

l_res_ref = l_stmt_ref->execute_query( l_stmt ).

* Set output structure
GET REFERENCE OF l_wa INTO l_dref.
l_res_ref->set_param_struct( l_dref ).

* Show result
WHILE l_res_ref->next( ) > 0.
  WRITE: / 'Result:', l_wa-carrid, l_wa-connid.
ENDWHILE.

* Close the result set object
l_res_ref->close( ).

Otherwise you can also use the FM DB_EXECUTE_SQL...

Kr,

m.

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
2,397

Have a look at program adbc_demo. Its good.

Keshav

Read only

Former Member
0 Likes
2,397

Thank you all, I solved the problem !!

Read only

0 Likes
2,397

how did you solve it?

BR Merrill

Read only

former_member212148
Participant
0 Likes
2,397

Dear Manu,

Your code is working fine.

My requirement is little bit different.

I want to get data from two table into internal table.

I have written code using inner join but i am getting dump

The exception 'CX_SQL_EXCEPTION' was raised,

Could you give me solution.

Thanks

Ranjit K.

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
2,397

Hello Ranjit,

Please create a new thread for and provide relevant & appropriate data to get better responses.

BR,

Suhas

Read only

0 Likes
2,397

Thanks Suhas,

I will keep it mind for future.