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

How to execute a select query stored in variable

former_member200116
Active Participant
0 Likes
2,964

Hello helpers ,

I have some "select queries" stored in the database . Now I can derive this query in some variable . How do I execute this query from the variable .

example :

Data Query type char50 .

QueryVar = 'Select MATNR from MBEW where BWKEY = '0001' . '

How do I execute this Query stoored in variable QueryVar in ABAP program ?

Thanks a lot for helping .

Regards

Shashank

1 ACCEPTED SOLUTION
Read only

Sougata
Active Contributor
1,775

Not possible to use the ABAP syntax as per the value stored in the variable of your table i.e. not possible to use the entire 'SELECT matnr FROM mbew WHERE bwkey = .....' as a dynamic statement, however it is possible to use a dynamic WHERE condition in ABAP known as (cond_syntax).

From ABAP Keyword Documentation - "A logical expression can be specified as a parenthesized data object cond_syntax that contains the syntax of a logical expression or is initial when the statement is executed."

Example of a dynamic (cond_syntax) in ABAP below:


PARAMETERS: column TYPE c LENGTH 8, 
            value  TYPE c LENGTH 30. 

DATA spfli_wa TYPE spfli. 

DATA cond_syntax TYPE string. 

CONCATENATE column '= value' 
            INTO cond_syntax SEPARATED BY space. 

TRY. 
    SELECT SINGLE * 
           FROM spfli 
           INTO spfli_wa 
           WHERE (cond_syntax). 
  CATCH cx_sy_dynamic_osql_error. 
    MESSAGE `Wrong WHERE condition!` TYPE 'I'. 
ENDTRY. 

Hope this helps.

Cheers,

Sougata.

7 REPLIES 7
Read only

Former Member
0 Likes
1,775

Hi,

check dis

/people/happy.tony/blog/2006/09/01/installing-db-connect-with-mssql-server-database

/people/graham.robinson/blog/2008/10/27/using-abap-to-access-non-sap-databases

may dis help u.

regards,

Archana

Read only

0 Likes
1,775

Hello Archana ,

Thank you so much for the concern ! But these post gives the information about how to connect to external DB's .

I saw in Exec SQL ... EndEXEC . but I think this is used for connecting to external DB ..

Thanks for the infomation but unfortunately didnt helped me ....

Well if you find any information please post the same , and if I find I will update that too .

Thanks and Regards

Shashank

Read only

Sougata
Active Contributor
1,776

Not possible to use the ABAP syntax as per the value stored in the variable of your table i.e. not possible to use the entire 'SELECT matnr FROM mbew WHERE bwkey = .....' as a dynamic statement, however it is possible to use a dynamic WHERE condition in ABAP known as (cond_syntax).

From ABAP Keyword Documentation - "A logical expression can be specified as a parenthesized data object cond_syntax that contains the syntax of a logical expression or is initial when the statement is executed."

Example of a dynamic (cond_syntax) in ABAP below:


PARAMETERS: column TYPE c LENGTH 8, 
            value  TYPE c LENGTH 30. 

DATA spfli_wa TYPE spfli. 

DATA cond_syntax TYPE string. 

CONCATENATE column '= value' 
            INTO cond_syntax SEPARATED BY space. 

TRY. 
    SELECT SINGLE * 
           FROM spfli 
           INTO spfli_wa 
           WHERE (cond_syntax). 
  CATCH cx_sy_dynamic_osql_error. 
    MESSAGE `Wrong WHERE condition!` TYPE 'I'. 
ENDTRY. 

Hope this helps.

Cheers,

Sougata.

Read only

0 Likes
1,775

Hello Sougata ,

Thanks for the information !

My case is that the query is stored in database . I have to retrive this query ans use this in my abap code .

For example .

I will use some select statement to select this query in my workarea

WA_ITAB-Query = ' Select MATNR from MBEW Where BWKEY = 0001 '

How can I use WA_ITAB-Query in ABAP code to fetch data ?

The requirement is like that

I tried your code but its different as we are providing the column and value fields in but the query is already there in code .

Thanks ...looking for help

Shashank

Read only

Sougata
Active Contributor
0 Likes
1,775

Shashank,

It is also possible to use (column_syntax) and (dbtab_syntax) together with (cond_syntax) when using SELECT statements in ABAP. For more info on (column_syntax) and (dbtab_syntax) just have a quick look at ABAP Keyword documention on SELECT statement (hit F1 on SELECT then scroll down to Select->Select result->Select Columns....)

So in your case, you need to separate out (split) the value in 'wa_itab-query' into other variables or append into separate internal tables using common keys etc. - then looping at those tables with the common key (READ TABLE WITH KEY....) use the following syntax at the time of triggering the SELECT query:

SELECT (column_syntax) 
       FROM (dbtab_syntax)
       WHERE (cond_syntax).

Also worth a look at this example below:

PARAMETERS: p_cityfr TYPE spfli-cityfrom, 
            p_cityto TYPE spfli-cityto. 

DATA: BEGIN OF wa, 
         fldate TYPE sflight-fldate, 
         carrname TYPE scarr-carrname, 
         connid   TYPE spfli-connid, 
       END OF wa. 

DATA itab LIKE SORTED TABLE OF wa 
               WITH UNIQUE KEY fldate carrname connid. 
DATA: column_syntax TYPE string, 
      dbtab_syntax TYPE string. 

column_syntax = `c~carrname p~connid f~fldate`. 

dbtab_syntax = `( ( scarr AS c ` 
  & ` INNER JOIN spfli AS p ON p~carrid  = c~carrid` 
  & ` AND p~cityfrom = p_cityfr` 
  & ` AND p~cityto   = p_cityto )` 
  & ` INNER JOIN sflight AS f ON f~carrid = p~carrid ` 
  & ` AND f~connid = p~connid )`. 

SELECT (column_syntax) 
       FROM (dbtab_syntax) 
       INTO CORRESPONDING FIELDS OF TABLE itab. 

LOOP AT itab INTO wa. 
  WRITE: / wa-fldate, wa-carrname, wa-connid. 
ENDLOOP.

Hope this helps.

Cheers,

Sougata.

Read only

0 Likes
1,775

Hello Sougata ,

Thanks for the information ..

I am trying to use the below code as generic one , so that this will work for any select query including the joins ..

If this works then lets see ....I will put the information in the post and will also confirm the post ...

I am trying to build a generic query for which we can build the column_syntax and the dbtab_syntax for this below

select and lets see if this works

SELECT (column_syntax)

FROM (dbtab_syntax)

INTO CORRESPONDING FIELDS OF TABLE itab.

@ Saurabh >> Hello saurabh , thanks for the concern but a very huge infomation ...and I am with less time to go through this info now ...any way thanks for this will be helpful

Regards

Shashank

Read only

Former Member
0 Likes
1,775

This message was moderated.