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 (Field) -- in DYNAMIC SELECT

Former Member
0 Kudos

Hey yall,

This is somewhat complicated, but if you can help me out I'd appreciate it.

I'm having some difficulty with the following situation:

I have a table ZCatFld that contains the catalog name and the field name (within that catalog).

I then try to search through the catalog to find the KEY value of the record where the field (as defined in the selection screen) matches the value entered by the user.

In the selection screen, there is an option to enter in values for GLAcct or CustNO.

Zcatfld's Identifier is this field <value of either 'GLacct' or 'Custno'.>

So I do the following:

IF GLACCT-LOW IS INITIAL.

SEARCHEDFLD = 'GLACCT'.

EnteredValue = GLACCT-LOW.

ELSE.

IF CUSTNO-LOW IS INITIAL.

SEARCHEDFLD = 'CUSTNO'.

EnteredValue = CUSTNO-LOW.

ENDIF.

ELSE.

EXIT.

ENDIF.

SELECT catalogname INTO (TABLENAME) FROM Zcatfld WHERE SearchedFld EQ SearchFld.

SELECT FLD FROM zcatfld INTO (FIELDNAME) WHERE SearchedFld EQ SearchFld.

SELECT Key INTO varKey FROM (TABLENAME) WHERE (FIELDNAME) EQ EnteredValue.

<i>the (fieldname) does not work as I thought it would. I thought that by enclosing it in ( ) it would be dynamic and still be respected as a field structure. Can someone please help? I'd appreciate it. Thankyou.</i>

N L

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi Natasha,

This is how you need to modify your code.


DATA: BEGIN OF lt_cond OCCURS 0 ,
        cond_line(60) TYPE c ,
      END OF lt_cond .

IF glacct-low IS INITIAL.
  searchedfld = 'GLACCT'.
  enteredvalue = glacct-low.
ELSEIF custno-low IS INITIAL.
  searchedfld = 'CUSTNO'.
  enteredvalue = custno-low.
ELSE.
  EXIT.
ENDIF.

SELECT catalogname fld INTO (tablename,fieldname)
                       FROM zcatfld
                      WHERE searchedfld EQ searchfld.
ENDSELECT.
REFRESH lt_cond.
CLEAR lt_cond.

CONCATENATE fieldname
            'EQ'
            enteredvalue
            '.'
       INTO lt_cond-cond_line SEPARATED BY space .
APPEND lt_cond .
CLEAR lt_cond.

SELECT key INTO varkey
           FROM (tablename)
          WHERE (lt_cond).
ENDSELECT.

Make sure that the field name and the table name are in uppercase.

Let us know if this helped.

Srinivas

9 REPLIES 9

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

Regards,

Rich Heilman

Former Member
0 Kudos

Use field symbols to reference the INTO area. You can create this area dynamically with CREATE DATA...

ASSIGN <FS> to wa.
SELECT ... INTO <FS>

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

As Charles said, Use field symbols to do this.



data: xmara type mara.
parameters: p_field(10) type c default 'XMARA'.
  field-symbols: <fs>.
  assign (p_field) to <fs>.
  select single * from mara into <fs>
                where matnr in s_matnr.
  check sy-subrc = 0.

Regards,

Rich Heilman

Message was edited by: Rich Heilman

ssimsekler
Active Contributor
0 Kudos

Hi Natasha

For dynamic WHERE condition try this:

DATA: BEGIN OF lt_cond OCCURS 0 ,
        cond_line(60) TYPE c ,
      END OF lt_cond .

REFRESH lt_cond .
CONCATENATE fieldname 'EQ' enteredvalue '.'
            INTO lt_cond-cond_line
            SEPARATED BY space .
APPEND lt_cond .

SELECT SINGLE key INTO varkey 
       FROM (tablename) 
       WHERE lt_cond .

Hope this helps...

*--Serdar

ssimsekler@yahoo.com

0 Kudos

Hey Serdar- thanks for helping.

For some reason, when I debug this, the portion of code you sent me isn't picking up any key value.

I entered in a GLACCT -- I know glacct has a catalog and a catalog field in the CatalogLookup table Zdoltarc02.

And I manually looked in that catalog and that field to make sure the value I entered has a corresponding key.

But when I step through the debugger, nothing is retrieved into the varKey.

Can you offer any advice? I'm reading up on field symbols as Charles had previously suggested, but I don't know if they are necessary.

Thanks,

Natasha

REPORT ZINDEXEDTEST2 .

*****************************************

DATA: BEGIN OF lt_cond OCCURS 0 ,

cond_line(60) TYPE c ,

END OF lt_cond .

data: wa_skas TYPE skas,

wa_kna1 TYPE KNA1,

searchedfld type string,

enteredvalue LIKE SKAS-SAKNR,

tablename LIKE dd02t-tabname,

varkey LIKE zarixfi3-ARCHIVEKEY.

SELECT-OPTIONS ACCT_NO FOR wa_skas-saknr.

SELECT-OPTIONS CUSTNO FOR wa_kna1-KUNNR.

REFRESH lt_cond .

IF acct_no-LOW <> ''.

SEARCHEDFLD = 'acct_no'.

EnteredValue = acct_no-LOW.

ENDIF.

IF CUSTNO-LOW <> ''.

SEARCHEDFLD = 'CUSTNO'.

EnteredValue = CUSTNO-LOW.

ENDIF.

IF ACCT_NO-LOW EQ '' AND CUSTNO-LOW EQ ''.

EXIT.

ENDIF.

SELECT aiscatalog INTO (TABLENAME) FROM zdoltarc02 WHERE SearchFld EQ SearchedFld.

CONCATENATE searchedfld 'EQ' enteredvalue '.'

INTO lt_cond-cond_line

SEPARATED BY space .

APPEND lt_cond .

SELECT SINGLE archivekey INTO varkey

FROM (tablename)

WHERE (lt_cond) .

ENDSELECT.

WRITE:/ 'VARKEY:', VARKEY.

Former Member
0 Kudos

Hi Natasha,

This is how you need to modify your code.


DATA: BEGIN OF lt_cond OCCURS 0 ,
        cond_line(60) TYPE c ,
      END OF lt_cond .

IF glacct-low IS INITIAL.
  searchedfld = 'GLACCT'.
  enteredvalue = glacct-low.
ELSEIF custno-low IS INITIAL.
  searchedfld = 'CUSTNO'.
  enteredvalue = custno-low.
ELSE.
  EXIT.
ENDIF.

SELECT catalogname fld INTO (tablename,fieldname)
                       FROM zcatfld
                      WHERE searchedfld EQ searchfld.
ENDSELECT.
REFRESH lt_cond.
CLEAR lt_cond.

CONCATENATE fieldname
            'EQ'
            enteredvalue
            '.'
       INTO lt_cond-cond_line SEPARATED BY space .
APPEND lt_cond .
CLEAR lt_cond.

SELECT key INTO varkey
           FROM (tablename)
          WHERE (lt_cond).
ENDSELECT.

Make sure that the field name and the table name are in uppercase.

Let us know if this helped.

Srinivas

0 Kudos

This still leaves me with the issue of how to declare fieldname. How should I declare it? For the (tablename) I declared it as:

tablename LIKE dd02t-tabname

N L

0 Kudos

Hi Natasha,

Sorry our replies overlapped so I didn't see you last post. You can declare your fieldname and table name as DD03L-FIELDNAME and DD03L-TABNAME.

Regards,

Srinivas

0 Kudos

Serdar & Srinivas,

You are awesome. It returns the key now. I think I can take this bull by the horns from here on out.

Thanks,

Tosh