Friday, August 7, 2015

Select Dynamic Column

We can select the columns dynamically in a select statement. The syntax is like this:
SELECT (local_internal_table)
  FROM database_table INTO TABLE internal_table.

Here the local internal table contains the field names dynamically. This table also has a line type which holds the data of field names like this.
DATAline TYPE char100,
      itab TYPE TABLE OF line.

line = 'ebeln ebelp matnr werks lgort'.
APPEND line TO itab.

Now after appending the text to the itab it can be used dynamically in select statement. Here the WHERE clause is optional. If we don’t use it then the total rows/records of the fields will have been fetched by the system.
  
REPORT  zabap_gui.

TABLES: ekpo.

* Creating a custom structure of Item Table
TYPES:
      BEGIN OF ty_ekpo,
        ebeln TYPE ekpo-ebeln,
        ebelp TYPE ekpo-ebelp,
        matnr TYPE ekpo-matnr,
        werks TYPE ekpo-werks,
        lgort TYPE ekpo-lgort,
      END OF ty_ekpo.

* Creating a line type of predefined structure
DATA:
      wa_ekpo TYPE ty_ekpo,
      it_ekpo TYPE STANDARD TABLE OF ty_ekpo,

* Creating a line type and internal table
* to use as dynamic columns specification
      line TYPE char100,
      itab TYPE TABLE OF line.

line = 'ebeln ebelp matnr werks lgort'.
APPEND line TO itab.

SELECT (itab)
  FROM ekpo INTO TABLE it_ekpo
  WHERE ebeln = '3000000232'.

WRITE:/    'PO No.',
        15 'Item No',
        28 'Material',
        48 'Plant',
        55 'Storage'.
ULINE.
SKIP.

LOOP AT it_ekpo INTO wa_ekpo.
  WRITE:/    wa_ekpo-ebeln,
          15 wa_ekpo-ebelp,
          28 wa_ekpo-matnr,
          48 wa_ekpo-werks,
          55 wa_ekpo-lgort.
ENDLOOP.

Here is the output.



5 comments:

Shalini said...


Hi, This is shalini from Chennai learned SAP Training in Chennai from mr.karthick. The training really was good and i got selected in leading mnc company as SAP Consultant.

You can contact 8122241286 for Best SAP Training in Chennai or reach www.thecreatingexperts.com

Anonymous said...

SAP Success Factors Real Time Hands on Training in Chennai...

Don't always Depend on Training Institute Alone and so please aware of Best Trainers too..

http://thecreatingexperts.com/sap-successfactors-training-in-chennai/

If You need a Best Trainer over SAP Success Factors Means??? Please ready for an DEMO From the Trainer MR.Karthick
CONTACT:8122241286

Both Classroom/Online Training is Available!!!!!!

kavi kalyan said...

Best SAP Success Factor Training in Chennai

http://thecreatingexperts.com/sap-training-in-chennai/
http://thecreatingexperts.com/sap-successfactors-training-in-chennai/
http://thecreatingexperts.com/sap-mm-training-in-chennai/
http://thecreatingexperts.com/sap-fico-training-in-chennai/

Anonymous said...

Informative Blog...For a long time I was craving for a career growth in programming and then I came to know that THE CREATING EXPERTS is the one who provide training with hands on training and real time scenarios

http://thecreatingexperts.com/sap-abap-training-in-chennai/

contact 8122241286

Unknown said...

Hi Sandeep,
Its very nice and informative blog.
I am trying to do in the same fashion.
here im trying to create a dynamic column specification nearly 50 fields.


But I am getting an error 'Literals that take up more than one line are not permitted'.

Can you guide me.. how to resolve this issue.

Below is the piece of logic which i have define.
DATA: ls_line(500) TYPE c,
lt_fields_glpct TYPE TABLE OF line.
ls_line = 'RBUKRS DRCRK RPRCTR RFAREA RACCT RASSC SPRCTR HSLVT HSL01 HSL02 HSL03 HSL04 HSL05 HSL06 HSL07 HSL08 HSL09 HSL10 HSL11 HSL12 HSL13 HSL14 HSL15 HSL16 KSLVT KSL01 KSL02 KSL03 KSL04 KSL05 KSL05 KSL06 KSL07 KSL08 KSL09 KSL10 KSL11 KSL12 KSL13 KSL14 KSL15 KSL16 RMVCT'.
APPEND ls_line to lt_fields_glpct.
CLEAR ls_line.

Thanks in Advance.
Thulasi