B. Bowles
B. Bowles

Reputation: 764

Specifying a DB table dynamically? Is it possible?

I am writing a BSP and based on user-input I need to select data from different DB tables. These tables are in different packages. Is it possible to specify the table I want to use, based on its path, like this:

data: path1 type string value 'package1/DbTableName',
      path2 type string value 'package2/OtherDbTableName',
      table_to_use type string.

if some condition
table_to_use = path1.    
elseif some condition
table_to_use = path2.    
endif.

select *
from table_to_use

     ...

endselect

I am new to ABAP & Open SQL and am aware this could be an easy/silly question :) Any help at all would be very much appreciated!

Upvotes: 2

Views: 133

Answers (1)

PATRY Guillaume
PATRY Guillaume

Reputation: 4337

You can define the name of the table to use in a variable, and then use the variable in the FROM close of your request :

data tableName type tabname.  
if <some condition>.   
   tableName='PA0001'.   
else.   
   tableName='PA0002'.   
endif.   
select * from (tableName) where ...

there are a few limitation to this method, as the stable can not contains fields of type RAWSTRING, STRING or SSTRING.

as for the fact that the table are in different package, i don't think it matters.

Regards,

Upvotes: 5

Related Questions