Dynamically create the where clause of a select statement

Dynamically create the where clause of a select statement

I would be very grateful for your help. I want to dynamically create the where clause of a select statement. I am able to do it when the actual where clause is static but it fails when using results from an internal table.

* define the internal table which will
* hold the where clause
data w_clause_b(80) occurs 10 with header line.

data : count_rec type i.

* select the transaction which will be * used in the select statement
select-options: s_trx for tstc-tcode no intervals obligatory
default 'va01'.

move 'tcode in s_trx' to where_tab.
append where_tab
clear where_tab.

SELECT COUNT( * ) INTO COUNT_REC
FROM ZALEPRSTAT
WHERE STARTDATE GE RAW_DATE_FR
AND STARTDATE LE RAW_DATE_TO
AND (WHERE_TAB).

It works when I use:
concatenate 'tcode =' '''VA01''' into where_tab separated by space.

I have also tried various combinations including:
concatenate 'tcode in' s_trx into where_tab separated by space.

    Requires Free Membership to View

    When you register, you will start receiving targeted emails from my award-winning team of editorial writers. Our goal is to keep you informed on the hottest topics and biggest challenges faced by SAP professionals today.

    Hannah Smalltree, Editorial Director

    By submitting your registration information to SearchSAP.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchSAP.com is governed by our Terms of Use. You may contact us at webmaster@TechTarget.com.

I can't tell why yours isn't working! I've created the following small program which does pretty much what yours does and it works fine.

TABLES: tstc.

DATA where_tab(80) OCCURS 10 WITH HEADER LINE.
DATA : count_rec TYPE i.

SELECT-OPTIONS: s_trx FOR tstc-tcode NO INTERVALS OBLIGATORY
DEFAULT 'va01'.

MOVE 'tcode in s_trx' TO where_tab.
APPEND where_tab.
CLEAR where_tab.
SELECT COUNT( * ) INTO count_rec
FROM tstc.
WHERE (where_tab).
WRITE: count_rec.

This was first published in July 2006