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 Concatenation

Former Member
0 Kudos

Hi all

How to have a dynamic concat where user can specify wad fields to concat?

Below code are static and hardcoded where i specify the fields in my program. How to make the field dynamic in the sql statement?

code----


Select <b>fname lname</b> into <b>(value1, value2)</b> from ZSTUDGARY where

admino = '012345'.

endselect.

CONCATENATE value1 value2 INTO NAME.

write:/ name.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

HI,

see this code.

PARAMETERS:field1(30).

PARAMETERS:field2(30).

DATA:fld_list type string,value1(30),value2(30),val type string.

CONCATENATE field1 field2 INTO fld_list SEPARATED BY space.

SELECT (fld_list) into (value1,value2) from mara.

CONCATENATE value1 value2 into val.

write:/ val.

ENDSELECT.

rgds,

bharat.

25 REPLIES 25

former_member189059
Active Contributor
0 Kudos

one method i can think of is to set a flag for each value (value1, value2 etc)

use if conditions in the concatenate statement

name = ''.
if name1_flag = 'X'.
concatenate name name1 into name.
endif.
if name2_flag = 'X'.
concatenate name name2 into name.
endif.

former_member189059
Active Contributor
0 Kudos

sorry, because of my unstable net connection, the reply got posted twice

Message was edited by:

Kris Donald

0 Kudos

I think u mistaken my requirement.

I want to concat the field dynamic

select <b>fname lname</b> into (value1, value2) from student where admino = '123'

i wan to make the bold to be dynamic where user can specify the field

fname and lname is not the table field. There are the parameter value.

fname = 'firstname'

lname = 'lastname'

firstname and lastname is the table fields.

0 Kudos

Hi,

Put the field which you want into an internal table. then use the select query, like this

select (itab1) from into (value1, value2) from student where admino = '123'.

For example.

Data: f_bsec_sel(40) type c,

lt_bsec_sel like table of f_bsec_sel.

append 'BUKRS' to lt_bsec_sel.

append 'BELNR' to lt_bsec_sel.

append 'GJAHR' to lt_bsec_sel.

append 'BUZEI' to lt_bsec_sel.

select (lt_bsec_sel) from bsec

into corresponding fields of table lt_bsec

for all entries in it_items

where bukrs = it_items-bukrs

and belnr = it_items-belnr

and gjahr = it_items-gjahr

and buzei = it_items-buzei.

Regards,

Niyaz

Former Member
0 Kudos

HI,

see this code.

PARAMETERS:field1(30).

PARAMETERS:field2(30).

DATA:fld_list type string,value1(30),value2(30),val type string.

CONCATENATE field1 field2 INTO fld_list SEPARATED BY space.

SELECT (fld_list) into (value1,value2) from mara.

CONCATENATE value1 value2 into val.

write:/ val.

ENDSELECT.

rgds,

bharat.

0 Kudos

Hi i check yr code but i receive this error

"FLD_LIST" is not an internal table.

How to resolve it?

0 Kudos

HI,

check the code as it is.i have checked it on my system it is working fine.

if u have a problem still post ur code here.

rgds,

bharat.

0 Kudos

Below is my code. I execute but still receive the syntax error say "FLD_LIST" is not an internal table.

REPORT ZGARY_CONCAT1 .

PARAMETERS:field1(30).

PARAMETERS:field2(30).

DATA:fld_list type string,value1(30),value2(30),val type string.

CONCATENATE field1 field2 INTO fld_list SEPARATED BY space.

SELECT (fld_list) into (value1,value2) from ZSTUDGARY.

CONCATENATE value1 value2 into val.

write:/ val.

ENDSELECT.

0 Kudos

HI,

here it is working fine for me.is ZSTUDGARY a data dictionary table.

if so it will work surely.

rgds,

bharat.

0 Kudos

hi

ZSTUDGARY is my create table in database.

I using R3 for my abap.

Is there a possibility that the sql statement cant recognize the below statement

SELECT (fld_list) into (value1, value2) from ZSTUDGARY.

0 Kudos

HI,

no chance.it will surely work.

i used it many times.check using some system table like MARA.

rgds,

bharat.

0 Kudos

Hi,

I think the problem is not with the ZSTUDGARY table. it with the query. try to create an internal table and use the select query like this.

data: lt_ZSTUDGARY type standard table of ZSTUDGARY.

SELECT (fld_list) into corresponding fields of table lt_zstudgary from ZSTUDGARY.

regards,

Niyaz

0 Kudos

HI,

can u write ur full code.i am curious to know how ur code works.

rgds,

bharat.

0 Kudos

Hi Gary ,

The replace of the fields in the where clause must be an internal table ,.

Here is a small example for the same

data : select1 type table of EDPLINE ,
        wa_sel type edpline .

DATA : V1(30) ,
      V2(3).

wa_sel = 'MATNR'.
APPEND WA_SEL TO SELECT1.

wa_sel = 'WERKS'.
APPEND WA_SEL TO SELECT1.


SELECT (SELECT1) INTO (V1 , V2)
FROM MARC.
BREAK-POINT.
ENDSELECT

.

for more info you can also check the link

https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/docs/library/uuid/840ad679-0601-0010-cd8e-998...

Regards

Arun

0 Kudos

This is my entire code. My sytnax prompt for "FLD_LIST" is not an internal table.

REPORT ZGARY_CONCAT1.

PARAMETERS:field1(30).

PARAMETERS:field2(30).

DATA: fld_list type string,

value1(30),

value2(30),

val type string.

CONCATENATE field1 field2 INTO fld_list SEPARATED BY space.

SELECT (fld_list) into (value1, value2) from ZSTUDGARY.

CONCATENATE value1 value2 into val.

write:/ val.

ENDSELECT.

0 Kudos

Hi,

try this code otherwise.

TABLES: VBAK.

DATA: it_vbak TYPE TABLE OF vbak WITH HEADER LINE,

wa_fld(72) TYPE C,

it_fldtab LIKE TABLE OF wa_fld.

wa_fld = 'VBELN'.

APPEND wa_fld TO it_fldtab.

wa_fld = 'VBTYP'.

APPEND wa_fld TO it_fldtab.

wa_fld = 'AUDAT'.

APPEND wa_fld TO it_fldtab.

SELECT (it_fldtab)

INTO CORRESPONDING FIELDS OF TABLE it_vbak

FROM VBAK.

IF SY-SUBRC = 0.

LOOP AT it_vbak.

write:/ it_vbak-vbeln, it_vbak-vbtyp, it_vbak-audat.

ENDLOOP.

ENDIF.

rgds,

bharat.

0 Kudos

yes is working but hw to take in from the parameter dynamic. like i only wan the admino and fname only not the lname.

REPORT ZGARY_CONCAT1.

TABLES: ZSTUDGARY.

DATA: it_vbak TYPE TABLE OF ZSTUDGARY WITH HEADER LINE,

wa_fld(72) TYPE C,

it_fldtab LIKE TABLE OF wa_fld.

wa_fld = 'ADMINO'.

APPEND wa_fld TO it_fldtab.

wa_fld = 'FNAME'.

APPEND wa_fld TO it_fldtab.

wa_fld = 'LNAME'.

APPEND wa_fld TO it_fldtab.

SELECT (it_fldtab)

INTO CORRESPONDING FIELDS OF TABLE it_vbak

FROM ZSTUDGARY.

IF SY-SUBRC = 0.

LOOP AT it_vbak.

write:/ it_vbak-admino, it_vbak-fname, it_vbak-lname.

ENDLOOP.

ENDIF.

0 Kudos

Hi,

try this code.

TABLES: ZSTUDGARY.

PARAMETERS:field1(30).

PARAMETERS:field2(30).

DATA: it_vbak TYPE TABLE OF ZSTUDGARY WITH HEADER LINE,

wa_fld(72) TYPE C,

it_fldtab LIKE TABLE OF wa_fld.

if not field1 is initial.

wa_fld = field1.

APPEND wa_fld TO it_fldtab.

endif.

if not field1 is initial.

wa_fld = 'ield2.

APPEND wa_fld TO it_fldtab.

endif.

SELECT (it_fldtab)

INTO CORRESPONDING FIELDS OF TABLE it_vbak

FROM ZSTUDGARY.

IF SY-SUBRC = 0.

LOOP AT it_vbak.

write:/ it_vbak-admino, it_vbak-fname, it_vbak-lname.

ENDLOOP.

ENDIF.

0 Kudos

HI,

try this.

TABLES: ZSTUDGARY.

PARAMETERS:field1(30).

PARAMETERS:field2(30).

DATA: it_vbak TYPE TABLE OF ZSTUDGARY WITH HEADER LINE,

wa_fld(72) TYPE C,

it_fldtab LIKE TABLE OF wa_fld.

APPEND field1 TO it_fldtab.

APPEND field2 TO it_fldtab.

SELECT (it_fldtab)

INTO CORRESPONDING FIELDS OF TABLE it_vbak

FROM ZSTUDGARY.

IF SY-SUBRC = 0.

LOOP AT it_vbak.

write:/ it_vbak-admino, it_vbak-fname, it_vbak-lname.

ENDLOOP.

ENDIF.

rgds,

bharat.

0 Kudos

Solve the dynamic field part.

Now let say i have 10 fields in ZSTUDGARY table.

I want to concat the fields together base on the number of parameter field i enter.

Assume i had enter 3 fields. How to concat them dynmic base on check of the fields i enter

0 Kudos

Hi,

just check whether the field is blank. if its blank, dont concatenate.

PS: Make sure that the fields passed are in the same order as of DB table

Reward points for usefull answers

Regards,

Niyaz

0 Kudos

HI,

if field1 is NOT INITIAL.

append field1 to it_fldtab.

endif.

write like this for all 10 fields.

they don't need to be in the same order as they are in data base

rgds,

bharat.

0 Kudos

Hi

How to concat the fields togther if i have 3 field value?

REPORT ZGARY_CONCAT1.

PARAMETERS:field1(30).

PARAMETERS:field2(30).

PARAMETERS:field3(30).

PARAMETERS:field4(30).

PARAMETERS:field5(30).

TABLES: ZSTUDGARY.

DATA: it_vbak TYPE TABLE OF ZSTUDGARY WITH HEADER LINE,

wa_fld(72) TYPE C,

it_fldtab LIKE TABLE OF wa_fld.

if field1 <> ''.

append field1 to it_fldtab.

endif.

if field2 <> ''.

append field2 to it_fldtab.

endif.

if field3 <> ''.

append field3 to it_fldtab.

endif.

if field4 <> ''.

append field4 to it_fldtab.

endif.

if field5 <> ''.

append field5 to it_fldtab.

endif.

SELECT (it_fldtab)

INTO CORRESPONDING FIELDS OF TABLE it_vbak

FROM ZSTUDGARY.

IF SY-SUBRC = 0.

LOOP AT it_vbak.

write:/ it_vbak.

ENDLOOP.

ENDIF.

0 Kudos

I happen to work on dynamic table these days, too.

following code works with dbtab, you can see if it works with itab (Open SQL part may need change)

field-symbols: <lfs_src_tbl> type any, " FS represents line structure of source table

<lfs_des_tbl> type any. " FS represents line structure of destination table

data: l_src_tblname type tabname, " table name of source table

l_des_tblname type tabname, " table name of destination table

lr_src_tbl type ref to data, " line structure of source table

lr_des_tbl type ref to data. " line structure of destination table

" dynamically create table structure

create data lr_src_tbl type (l_src_tblname).

create data lr_des_tbl type (l_des_tblname).

" assign to field-symbols

assign lr_src_tbl->* to <lfs_src_tbl>.

assign lr_des_tbl->* to <lfs_des_tbl>.

" copy data

select * from (l_src_tblname) into <lfs_src_tbl>.

move-corresponding <lfs_src_tbl> to <lfs_des_tbl>.

insert into (l_des_tblname)

values <lfs_des_tbl>.

endselect.

Former Member
0 Kudos

Hi,

Try this..Changes mentioned with the wrapper.



PARAMETERS:field1(30).
PARAMETERS:field2(30).
*DATA: fld_list type string,  " Commented.
DATA: t_fields type standard table of char72,
          s_fields type char72,
value1(30),
value2(30),
val type string.

data: t_data type standard table of ZSTUDGARY. " Inserted here.
data: s_data type ZSTUDGARY. " Inserted here.

*CONCATENATE field1 field2 INTO fld_list SEPARATED BY space.  " Commented

S_FIELDS = field1. APPEND s_fields to t_fields.  " Inserted here
S_FIELDS = field2. APPEND s_fields to t_fields.  " Inserted here

* Changed the following sql.
SELECT (t_fields) into corresponding fields of table T_DATA from ZSTUDGARY.

loop at t_data into s_data.
  write: / s_data.
endloop.


Thanks

Naren