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 SQL

Former Member
0 Kudos

Hello, there.

I want to define a dynamic select statment: Only the selection fields are suppose to be dynamic, meaning "select var_dynfld1 var_dynfld2 from kna1 where kunnr = 222".

Does anyone can send me the exact syntax ?

Thanks in advance,

Rebeka

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hey again.

Let me be more exact & specific:

I have an internal table which I want to read from.

The problem is that I want to set my key fields according to some dynamic conditions.

DATA:

BEGIN OF ITAB OCCURS 0,

KUNNR LIKE KNA1-KUNNR,

MATNR LIKE MARA-MATNR,

SUM LIKE BSEG-WRBTR,

END OF ITAB.

At first time I want to write READ TABLE ITAB WITH KEY KUNNR = 2

and after that: READ TABLE ITAB WITH KEY MATNR = 2.

I want the KUNNR and MATNR to be defined as variables.

6 REPLIES 6

athavanraja
Active Contributor
0 Kudos

code sample

REPORT YDYNSEL
       NO STANDARD PAGE HEADING.


data: itab type standard table of sflight .
TYPE-POOLS RSFS.

DATA SELECT_FIELDS TYPE RSFS_FIELDS.


data: FIELDLISTS TYPE RSFS_TAB_FIELDS,
           FLAG_READ.

  IF FLAG_READ = SPACE.
    FIELDLISTS-TABLENAME = 'SFLIGHT'.
    READ TABLE SELECT_FIELDS WITH KEY FIELDLISTS-TABLENAME
                             INTO FIELDLISTS.
    append 'CARRID' to FIELDLISTS-fields .
    append 'CONNID' to FIELDLISTS-fields .
    FLAG_READ = 'X'.
  ENDIF.




SELECT (FIELDLISTS-FIELDS)
up to 10 rows from sflight into corresponding fields of table itab .

Regards

Raja

krzysztof_konitz4
Contributor
0 Kudos

Hi,

How to write dynamic where condition you can find in documentation:

http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb3990358411d1829f0000e829fbfe/content.htm

in section "Specifying Columns Dynamically"

There is also small example there...

regards

Krzys

Former Member
0 Kudos

Hi Rebeka,

Check the below mentioned code is the same requirement you are looking for. If not the Explain your requirement in details.

If you are using parameters statements instead of select-options then you need to use Where condition like this in select statement(WHERE kunnr = s_kunnr).

DATA: Begin of itab occurs10,

kunnr type kunnr,

land1 type land1_gp,

ort01 type ort01_gp,

pstlz type pstlz,

end of itab.

select-options: s_kunnr for kna1-kunnr.

start-of-selection.

SELECT kunnr land1 ort01 pstlz

FROM kna1

INTO TABLE itab

WHERE kunnr in s_kunnr.

Ashven

athavanraja
Active Contributor
0 Kudos

Hi,

If the question is answered, can you mark the thread as answered.

raja

Former Member
0 Kudos

Hey again.

Let me be more exact & specific:

I have an internal table which I want to read from.

The problem is that I want to set my key fields according to some dynamic conditions.

DATA:

BEGIN OF ITAB OCCURS 0,

KUNNR LIKE KNA1-KUNNR,

MATNR LIKE MARA-MATNR,

SUM LIKE BSEG-WRBTR,

END OF ITAB.

At first time I want to write READ TABLE ITAB WITH KEY KUNNR = 2

and after that: READ TABLE ITAB WITH KEY MATNR = 2.

I want the KUNNR and MATNR to be defined as variables.

0 Kudos

data: fnam(40) .

move: 'KUNNR' to fnam .

read table itab with key (fnam) = 2 .

clear fnam .

move: 'MATNR' to fnam .

read table itab with key (fnam) = 2 .

Regards

Raja