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: 

Insert data from more than one table in ALV Grid

Former Member
0 Kudos

Hello everybody!!!

I am an absolute beginner in SAP/ABAP development.

I ve seen various examples of displaying data from only one table in ALV Grid ( mostly they use sflight ).

What i want to do is to take data from three tables and do the same thing.

I have a screen with a custom container named "CC_ALV" and a button with fc "EXIT".

My code is the following :

First in the report:

&----


*& Report ZALVDEMO

*&

&----


*&

*&

&----


REPORT ZALVDEMO.

*data decleration

+tables : mkpf,mseg,makt.+

+data: begin of zAVLTest occurs 0,+

+mblnr like mkpf-mblnr,+

+mjahr like mkpf-mjahr,+

+matnr like mseg-matnr,+

+maktx like makt-maktx,+

+erfmg like mseg-erfmg,+

+erfme like mseg-erfme,+

+werks like mseg-werks,+

+lgort like mseg-lgort,+

+end of zAVLTest.+

+data ok_code TYPE sy-ucomm.+

+data GRID TYPE REF TO CL_GUI_ALV_GRID.+

+data G_CUSTOM_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER.+

+* SELECTION SCREEN+

selection-screen begin of block b1 with frame title text-001.

select-options: s_mblnr for mkpf-mblnr.

parameters: s_mjahr like mkpf-mjahr obligatory default sy-datum(4).

selection-screen skip.

select-options: s_matnr for mseg-matnr,

s_werks for mseg-werks,

s_lgort for mseg-lgort.

selection-screen end of block b1.

*database selection

start-of-selection.

*SET PF-STATUS 'MENU'.

perform select_mat_doc.

end-of-selection.

*display data

call screen 0001.

INCLUDE ZALVDEMO_SELECT_MAT_DOCF01.

Then the include file:

----


***INCLUDE ZALVDEMO_SELECT_MAT_DOCF01 .

----


&----


*& Form select_mat_doc

&----


+* text+

----


+* --> p1 text+

+* <-- p2 text+

----


+FORM select_mat_doc .+

+select * from mkpf INNER JOIN mseg on mkpfmblnr = msegmblnr and mkpfmjahr = msegmjahr+

inner join makt on maktmatnr = msegmatnr

into corresponding fields of table zAvlTest

where mseg~matnr in s_matnr and

mseg~werks in s_werks and

mseg~lgort in s_lgort and

mkpf~mblnr in s_mblnr and

mkpf~mjahr eq s_mjahr.

ENDFORM. " select_mat_doc

&----


*& Module pbo_stock OUTPUT

&----


* text

----


MODULE pbo_stock OUTPUT.

SET PF-STATUS 'MENU'.

IF G_CUSTOM_CONTAINER IS INITIAL.

CREATE OBJECT G_CUSTOM_CONTAINER

EXPORTING CONTAINER_NAME = 'CC_ALV'.

CREATE OBJECT GRID

EXPORTING I_PARENT = G_CUSTOM_CONTAINER.

CALL METHOD GRID->SET_TABLE_FOR_FIRST_DISPLAY

EXPORTING I_STRUCTURE_NAME ='zalvtEST'

CHANGING IT_OUTTAB = gt_outtab[].

ENDIF.

ENDMODULE. " pbo_stock OUTPUT

&----


*& Module pai_stock INPUT

&----


* text

----


MODULE pai_stock INPUT.

* to react on oi_custom_events:

call method cl_gui_cfw=>dispatch.

CASE OK_CODE.

WHEN 'EXIT'.

write: ' Exit Push Button'.

WHEN OTHERS.

* do nothing

ENDCASE.

CLEAR OK_CODE.

ENDMODULE. " pai_stock INPUT

Thanks in advance anyone who reads that and hope to get a response soon!!!

P.S : i didn't have time to go htrough every thread so if smth like this was asked before, plz forgive me for consuming your time.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

hi,

make sure the table name passed to the method is in upper case.


MODULE pbo_stock OUTPUT.
SET PF-STATUS 'MENU'.
IF G_CUSTOM_CONTAINER IS INITIAL.
CREATE OBJECT G_CUSTOM_CONTAINER
EXPORTING CONTAINER_NAME = 'CC_ALV'."   HERE ALSO SHOULD BE IN UPPER CASE
CREATE OBJECT GRID
EXPORTING I_PARENT = G_CUSTOM_CONTAINER.
CALL METHOD GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING I_STRUCTURE_NAME ='NAME_IN_UPPER_CASE'  " UPPER CASE HERE
CHANGING IT_OUTTAB = gt_outtab[].
ENDIF.
ENDMODULE. " pbo_stock OUTPUT


9 REPLIES 9

Former Member
0 Kudos

John,

You are stil only displaying one table.

What seems to be missing is your Field Catalog and Layout Options.

Something along these lines for your field catalog.



  DATA:
* Layout
    wa_layout_action_0104 TYPE lvc_s_layo,
* Field catalog
    it_fcat_action_0104 TYPE lvc_t_fcat.


*   Build field catalog
    PERFORM build_fcat_action_0104 CHANGING it_fcat_action_0104.

*   Build layout
    PERFORM build_layout_action_0104 CHANGING wa_layout_action_0104.

*&---------------------------------------------------------------------*
*&      Form  build_fcat_action_0104
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      <--P_IT_FCAT_ACTION  text
*----------------------------------------------------------------------*
FORM build_fcat_action_0104 CHANGING it_fcat TYPE lvc_t_fcat.
  DATA:
    wa_fcat TYPE lvc_s_fcat.

* init
  REFRESH it_fcat.

* First Name
  CLEAR wa_fcat.
  wa_fcat-fieldname = 'FNAME'.
  wa_fcat-ref_table = 'WT_ZLMFIN'.
  wa_fcat-ref_field = 'FNAME'.
  wa_fcat-outputlen = 15.
*  wa_fcat-hotspot   = 'X'.
  wa_fcat-coltext   = 'First Name'.
  APPEND wa_fcat TO it_fcat.

* Middle Name
  CLEAR wa_fcat.
  wa_fcat-fieldname = 'MNAME'.
  wa_fcat-ref_table = 'WT_ZLMFIN'.
  wa_fcat-ref_field = 'MNAME'.
*  wa_fcat-hotspot   = 'X'.
  wa_fcat-outputlen = 10.
  wa_fcat-coltext   = 'Middle'.
  APPEND wa_fcat TO it_fcat.

* Last Name
  CLEAR wa_fcat.
  wa_fcat-fieldname = 'LNAME'.
  wa_fcat-ref_table = 'WT_ZLMFIN'.
  wa_fcat-ref_field = 'LNAME'.
*  wa_fcat-hotspot   = 'X'.
  wa_fcat-outputlen = 20.
  wa_fcat-coltext   = 'Last Name'.
  APPEND wa_fcat TO it_fcat.

* Applicant Type
  CLEAR wa_fcat.
  wa_fcat-fieldname = 'APPTYPDESC'.
  wa_fcat-ref_table = 'WT_ZLMFIN'.
  wa_fcat-ref_field = 'APPTYPDESC'.
*  wa_fcat-hotspot   = 'X'.
  wa_fcat-outputlen = 12.
  wa_fcat-coltext   = 'Appl Type'.
  APPEND wa_fcat TO it_fcat.

* Employment Type
  CLEAR wa_fcat.
  wa_fcat-fieldname = 'EMPLOYDESC'.
  wa_fcat-ref_table = 'WT_ZLMFIN'.
  wa_fcat-ref_field = 'EMPLOYDESC'.
*  wa_fcat-hotspot   = 'X'.
  wa_fcat-outputlen = 12.
  wa_fcat-coltext   = 'Employ Type'.
  APPEND wa_fcat TO it_fcat.

* Employer
  CLEAR wa_fcat.
  wa_fcat-fieldname = 'EMPLOY'.
  wa_fcat-ref_table = 'WT_ZLMFIN'.
  wa_fcat-ref_field = 'EMPLOY'.
*  wa_fcat-hotspot   = 'X'.
  wa_fcat-outputlen = 20.
  wa_fcat-coltext   = 'Employer'.
  APPEND wa_fcat TO it_fcat.

* Business Type
  CLEAR wa_fcat.
  wa_fcat-fieldname = 'BUSTYP'.
  wa_fcat-ref_table = 'WT_ZLMFIN'.
  wa_fcat-ref_field = 'BUSTYP'.
*  wa_fcat-hotspot   = 'X'.
  wa_fcat-outputlen = 20.
  wa_fcat-coltext   = 'Position'.
  APPEND wa_fcat TO it_fcat.

* Employer Phone Number
  CLEAR wa_fcat.
  wa_fcat-fieldname = 'EPHONE'.
  wa_fcat-ref_table = 'WT_ZLMFIN'.
  wa_fcat-ref_field = 'EPHONE'.
*  wa_fcat-hotspot   = 'X'.
  wa_fcat-outputlen = 13.
  wa_fcat-coltext   = 'Emp. Phone'.
  APPEND wa_fcat TO it_fcat.

* Employer Phone Extention
  CLEAR wa_fcat.
  wa_fcat-fieldname = 'EPHONE_EXT'.
  wa_fcat-ref_table = 'WT_ZLMFIN'.
  wa_fcat-ref_field = 'EPHONE_EXT'.
*  wa_fcat-hotspot   = 'X'.
  wa_fcat-outputlen = 6  .
  wa_fcat-coltext   = 'Ext.'.
  APPEND wa_fcat TO it_fcat.

ENDFORM.                    " build_fcat_action_0104

and the Layout


*&---------------------------------------------------------------------*
*&      Form  build_layout_action_0104
*&---------------------------------------------------------------------*
*      <--P_WA_LAYOUT_ACTION  text
*----------------------------------------------------------------------*
FORM build_layout_action_0104 CHANGING wa_layout TYPE lvc_s_layo.

*  Init
  CLEAR wa_layout.

*  wa_layout-sel_mode = 'A'.
*  wa_layout-edit       = 'X'.
  wa_layout-no_toolbar = 'X'.
  wa_layout-grid_title = 'Financial Contacts'.
  wa_layout-smalltitle = 'X'.

ENDFORM.                    " build_layout_action

0 Kudos

Thanks a lot!!! I ll try that and give some feedback soon! Thanks again for your time!

0 Kudos

Hello again! I tried what Paul wrote, but my problem persists...

I think the problem is that i don't know what to put where the bold letters are, as it asks for a system table, whereas 'zALVTest' is my custom table for receiving data from system tables(makt,mseg,mkpf)...

IF G_CUSTOM_CONTAINER IS INITIAL.

CREATE OBJECT G_CUSTOM_CONTAINER

EXPORTING CONTAINER_NAME = 'CC_ALV'.

CREATE OBJECT GRID

EXPORTING I_PARENT = G_CUSTOM_CONTAINER.

CALL METHOD GRID->SET_TABLE_FOR_FIRST_DISPLAY

EXPORTING I_STRUCTURE_NAME = 'zalvtEST'

CHANGING IT_OUTTAB = gt_outtab[].

If anyone could explain in detail what this function should look like, i would be grateful!!!!

Thanks again in advance!!!!

Former Member
0 Kudos

hi John,

Please check out the name of internal table which you have declared in program. You have define

it by name zAVLTest but you are passing it in method SET_TABLE_FOR_FIRST_DISPLAY as

'zalvtEST' .

Also make sure that the name of internal table in quotes should be in upper case.

Former Member
0 Kudos

hi,

make sure the table name passed to the method is in upper case.


MODULE pbo_stock OUTPUT.
SET PF-STATUS 'MENU'.
IF G_CUSTOM_CONTAINER IS INITIAL.
CREATE OBJECT G_CUSTOM_CONTAINER
EXPORTING CONTAINER_NAME = 'CC_ALV'."   HERE ALSO SHOULD BE IN UPPER CASE
CREATE OBJECT GRID
EXPORTING I_PARENT = G_CUSTOM_CONTAINER.
CALL METHOD GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING I_STRUCTURE_NAME ='NAME_IN_UPPER_CASE'  " UPPER CASE HERE
CHANGING IT_OUTTAB = gt_outtab[].
ENDIF.
ENDMODULE. " pbo_stock OUTPUT


0 Kudos

Thanks you for answering and for your patience!

To be even more specific and clear, i have the internal table created by using a select of fields of system tables, and my question is what do i put in

*----


CALL METHOD <ref. var. to CL_GUI_ALV_GRID>->set_table_for_first_display

EXPORTING I_BUFFER_ACTIVE = <any type (ANY)>

I_STRUCTURE_NAME = <string of type DD02L-TABNAME> <-here

IS_VARIANT = <structure of type DISVARIANT>

I_SAVE = <var. of type CHAR01>

I_DEFAULT = <var. of type CHAR01>

IS_LAYOUT = <structure of type LVC_S_LAYO>

IS_PRINT = <structure of type LVC_S_PRNT>

IT_SPECIAL_GROUPS = <internal table of type LVC_T_SGRP>

IT_TOOLBAR_EXCLUDING = <internal table of type UI_FUNCTIONS>

IT_HYPERLINK = <internal table of type LVC_T_HYPE>

IT_ALV_GRAPHICS = <internal table of type DTC_T_TC>

CHANGING IT_OUTTAB = <internal table> <-and here

IT_FIELDCATALOG = <internal table of type LVC_T_FCAT>

IT_SORT = <internal table of type LVC_T_SORT>

IT_FILTER = <internal table of type LVC_T_FILT>

if we assume this is the basic structure of set_table_for_first_display.

Also : is " I_STRUCTURE_NAME" obligatory?

Many thanks once again!!!!

0 Kudos

Ok!!! After hours of searching ( as i have no book at the moment for SAP) i found that I_STRUCTURE_NAME is not obligatory. In addition I only used : is_layout, it_outtab (where i defined my internal table), and it_fieldcatalog., in set_table_for_first_display.

Thank u all for your advices and for your time!!!!

0 Kudos

hi,


CALL METHOD <ref. var. to CL_GUI_ALV_GRID>->set_table_for_first_display" your_grid_object
          EXPORTING 
                  I_BUFFER_ACTIVE = <any type (ANY)>" optional
                  I_STRUCTURE_NAME = <string of your ddic table name>  "this parameter is not obligatory
                  IS_VARIANT = <structure of type DISVARIANT>" optional
                  I_SAVE = <var. of type CHAR01>" optional
                  I_DEFAULT = <var. of type CHAR01>" optional
                  IS_LAYOUT = <structure of type LVC_S_LAYO>"  alv grid layout
                  IS_PRINT = <structure of type LVC_S_PRNT>" optional
                  IT_SPECIAL_GROUPS = <internal table of type LVC_T_SGRP>" optional
                  IT_TOOLBAR_EXCLUDING = <internal table of type UI_FUNCTIONS>" optional
                  IT_HYPERLINK = <internal table of type LVC_T_HYPE>" optional
                  IT_ALV_GRAPHICS = <internal table of type DTC_T_TC>" optional
          CHANGING
                  IT_OUTTAB = <internal table> " internal table with the data you want to display in the alv grid
                  IT_FIELDCATALOG = <internal table of type LVC_T_FCAT>
                                                                              " usually we use this instead of I_STRUCTURE_NAME
                  IT_SORT = <internal table of type LVC_T_SORT>" if you want to the data in the alv to be sorted 
                  IT_FILTER = <internal table of type LVC_T_FILT>" if you want to filt some 

yes this is a very detailed information about this method, but usually ,we do not use all of the parameters, you just use some of them accordingly.

in order to give you a clear idea, check the code below.


types:  begin of emp,
    id(10) type c,
    name(20) type c,
end of emp.

data: wa_emp type emp,
      employee type standard table of emp.

data: layout type lvc_s_layo,
      fcat type lvc_t_fcat,
      w_fcat type lvc_s_fcat,
      grid type ref to cl_gui_alv_grid,
      container type ref to cl_gui_custom_container.

call screen 9000." create screen 9000 and make a custom container named 'MYCONT'

form sub_get_data.
   " code for getting data into internal table employee
endform.

form sub_build_layout.
	clear layout.
  	layout-grid_title = 'TABLES'.
  	layout-zebra = 'X'.
  	layout-no_toolbar = 'X'.
endform.


form sub_build_fcat.
	clear w_fcat.
	w_fcat-fieldname = 'ID'." upper case here
        w_fcat-coltext = 'Employee id'.
        append w_fcat to fcat.
	clear w_fcat.
	w_fcat-fieldname = 'NAME'." upper case here
        w_fcat-coltext = 'Employee name'.
        append w_fcat to fcat.
	clear w_fcat.

endform.

form sub_call_alv.
	if container is initial.
		create object container
		exporting
                 container_name = 'MYCONT'." UPPER CASE HERE
        endfi.
        if grid is initial.
                create object grid
		exporting
		  i_parent = container.
        endif.
        perform sub_get_data.
        perform sub_build_fcat.
        perform sub_build_layout.
        call method grid->set_table_for_first_display
         EXPORTING
            is_layout       = layout
         CHANGING
            it_outtab       = employee
            it_fieldcatalog = fcat.
      if sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
     endif.             
endform.

module pbo output.
  perform sub_call_alv.
endmodule." remember to call this module in the pbo module of your screen 

for the rest of the parameters in the method, if you want to know the details , just double click the method in you abap editor, the system will navigate you .

0 Kudos

Great tutorial!!! Thanks!!!