Application Development 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: 

Dynamic select

Former Member
0 Kudos

Hi,

I am doing a select with a dynamic where condition but I only need to check a field value in database table if there is an entry. Is there any way to achieve this?

For example I have this DB table:

Group field value

1 field1 A

1 field2 B

2 field3 C

I need to get the 'Group field' and imagine that, in my condition tab for select, I have for example that field1 = B and field3 = C.

In this case, I need that select returns 'group 2', as for group 1 field1 is different from my field1 value and group2 does not care about the value of field1.

So it would be great that I could say in the where condition tab to check the field value only if in DB table the entry exists...

Do you know how to do it?

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Use the OR operator instead of the AND operator, i.e. select from table where field1 = B or field3 = C.

Regards,

Steve.

7 REPLIES 7

Former Member
0 Kudos

It is possible that you can use a string to be your where clause in the selection statement. That means you can make a dynamic where clause according to the data by concatentate. I am sorry that my answer is not specific enough but hope this would help you.

Attached with the code:


FORM Select_Data_ByType using type_string.
  Data: where_clause type string Value 'where ',
        replace_offset type i.
  if type_string+0(1) = '1'.
    Concatenate where_clause 'type = Y AND' INTO where_clause separated by ' '.
  endif.
  if type_string+1(1) = '1'.
    Concatenate where_clause 'type = N AND ' INTO where_clause separated by ' '.
  endif.
  if type_string+2(1) = '1'.
    Concatenate where_clause 'type = NA AND ' INTO where_clause separated by ' '.
  endif.
  replace_offset = strlen( where_clause ) - 4.
  replace section offset replace_offset of where_clause with ''.
  write where_clause.
ENDFORM.

Edited by: Leon Law on Oct 8, 2008 6:22 PM

0 Kudos

Hi Leon,

I am already using a string for my dynamic where condition, but I need to know if I can say in the string not to compare a field if threre is no entry in the BD table.

Thanks anyway for your answer.

Former Member
0 Kudos

Use the OR operator instead of the AND operator, i.e. select from table where field1 = B or field3 = C.

Regards,

Steve.

0 Kudos

Hi Steve,

thanks a lot but it doesn't work in my case because if I have the same table I said before but I have this values in condition tab

field1 = B and field2 = B, my select would return group1 and it should not return any value...

0 Kudos

I see, I had misunderstood your scenario.

0 Kudos

No problem

I wanted to avoid to do a loop inside a select but I am afraid I will have to do it..

0 Kudos

I think so. If my understanding is correct, you need to retrieve all the data first before checking if the data really exist in the database. From my knowledge (I am new to ABAP...), it seems there is no way to check the existence of a value without accessing the table.