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

Dinamic Select

Former Member
0 Likes
311

Hi all,

I am trying to do a dynamic select in which I don´t know which are going to be the conditions of the clause WHERE.

This is my code;

DATA: BEGIN OF it_selopt OCCURS 0,
        field      TYPE string,
        value      TYPE string,
      END OF it_selopt,
      wa_selopt LIKE LINE OF it_selopt.

DATA cond_syntax TYPE string.

                ............

it_selopt-field = 'MATNR'.
it_selopt-value = 'Hello'.
APPEND it_selopt.


LOOP AT it_selopt.
  wa_selopt = it_selopt.

* First condition
  AT FIRST.
       CONCATENATE wa_selopt-field '=' wa_selopt-value
         INTO cond_syntax SEPARATED BY space.
    CONTINUE.  "
  ENDAT.
* Other conditions  
       CONCATENATE cond_syntax 'AND' wa_selopt-field '=' wa_selopt-value
            INTO cond_syntax SEPARATED BY space.
ENDLOOP.


TRY. 
    SELECT SINGLE * 
           FROM eban 
           WHERE (cond_syntax). 

  CATCH cx_sy_dynamic_osql_error. 
    MESSAGE `Wrong WHERE condition!` TYPE 'I'. 
ENDTRY.

The problem is that in runtime gives a syntax error for the following thing:

In runtime the variable cond_syntax has the following: MATNR = Hello

And it should have this: MATNR = 'Hello'

within quotation marks.

Because if it does not take Hello as if it was a variable.

Also I have tried the following thing:

DATA: field_2_read TYPE string.
  DATA: d_tabix TYPE n.

  LOOP AT it_selopt.
       wa_selopt = it_selopt.
       d_tabix = sy-tabix.
       CONCATENATE 'IT_SELOPT[' d_tabix ']-low' INTO field_2_read.

* First condition
       AT FIRST.
          CONCATENATE wa_selopt-field '=' field_2_read  
            INTO cond_syntax SEPARATED BY space.
          CONTINUE.
       ENDAT.

* Other conditions  
       CONCATENATE cond_syntax 'AND' wa_selopt-field '=' field_2_read 
            INTO cond_syntax SEPARATED BY space.
  ENDLOOP.

But it continues giving me a error.

Any idea how?

Thanks in advance

1 ACCEPTED SOLUTION
Read only

ThomasZloch
Active Contributor
0 Likes
269
it_selopt-value = '''Hello'''.

Thomas

1 REPLY 1
Read only

ThomasZloch
Active Contributor
0 Likes
270
it_selopt-value = '''Hello'''.

Thomas