Google Search - Blog...........

SAP ABAP:Finding User Exit For Any Program.

REPORT ztest .
TABLES : TSTC,
TADIR,
MODSAPT,
MODACT,
TRDIR,
TFDIR,
ENLFDIR,
SXS_ATTRT ,
TSTCT.

DATA : JTAB LIKE TADIR OCCURS 0 WITH HEADER LINE.
DATA : FIELD1(30).
DATA : V_DEVCLASS LIKE TADIR-DEVCLASS.

PARAMETERS : P_TCODE LIKE TSTC-TCODE,
P_PGMNA LIKE TSTC-PGMNA .




DATA wa_tadir type tadir.

START-OF-SELECTION.

IF NOT P_TCODE IS INITIAL.
SELECT SINGLE * FROM TSTC WHERE TCODE EQ P_TCODE.

ELSEIF NOT P_PGMNA IS INITIAL.
TSTC-PGMNA = P_PGMNA.
ENDIF.

IF SY-SUBRC EQ 0.
SELECT SINGLE * FROM TADIR
WHERE PGMID = 'R3TR'
AND OBJECT = 'PROG'
AND OBJ_NAME = TSTC-PGMNA.

MOVE : TADIR-DEVCLASS TO V_DEVCLASS.

IF SY-SUBRC NE 0.
SELECT SINGLE * FROM TRDIR
WHERE NAME = TSTC-PGMNA.
IF TRDIR-SUBC EQ 'F'.
SELECT SINGLE * FROM TFDIR
WHERE PNAME = TSTC-PGMNA.

SELECT SINGLE * FROM ENLFDIR
WHERE FUNCNAME = TFDIR-FUNCNAME.

SELECT SINGLE * FROM TADIR
WHERE PGMID = 'R3TR'
AND OBJECT = 'FUGR'
AND OBJ_NAME EQ ENLFDIR-AREA.

MOVE : TADIR-DEVCLASS TO V_DEVCLASS.
ENDIF.
ENDIF.

SELECT * FROM TADIR INTO TABLE JTAB
WHERE PGMID = 'R3TR'
AND OBJECT in ('SMOD', 'SXSD')
AND DEVCLASS = V_DEVCLASS.


SELECT SINGLE * FROM TSTCT
WHERE SPRSL EQ SY-LANGU
AND TCODE EQ P_TCODE.

FORMAT COLOR COL_POSITIVE INTENSIFIED OFF.
WRITE:/(19) 'Transaction Code - ',
20(20) P_TCODE,
45(50) TSTCT-TTEXT.
SKIP.
IF NOT JTAB[] IS INITIAL.
WRITE:/(105) SY-ULINE.
FORMAT COLOR COL_HEADING INTENSIFIED ON.

* Sorting the internal Table
sort jtab by OBJECT.
data : wf_txt(60) type c,
wf_smod type i ,
wf_badi type i ,
wf_object2(30) type C.
clear : wf_smod, wf_badi , wf_object2.
* Get the total SMOD.




LOOP AT JTAB into wa_tadir.
at first.
FORMAT COLOR COL_HEADING INTENSIFIED ON.

WRITE:/1 SY-VLINE,
2 'Enhancement/ Business Add-in',
41 SY-VLINE ,
42 'Description',
105 SY-VLINE.
WRITE:/(105) SY-ULINE.
endat.
clear wf_txt.
at new object.
if wa_tadir-object = 'SMOD'.
wf_object2 = 'Enhancement' .
elseif wa_tadir-object = 'SXSD'.
wf_object2 = ' Business Add-in'.

endif.
FORMAT COLOR COL_GROUP INTENSIFIED ON.

WRITE:/1 SY-VLINE,

2 wf_object2,
105 SY-VLINE.
endat.

case wa_tadir-object.
when 'SMOD'.
wf_smod = wf_smod + 1.
SELECT SINGLE MODTEXT into wf_txt
FROM MODSAPT
WHERE SPRSL = SY-LANGU
AND NAME = wa_tadir-OBJ_NAME.
FORMAT COLOR COL_NORMAL INTENSIFIED OFF.

when 'SXSD'.
* For BADis
wf_badi = wf_badi + 1 .
select single TEXT into wf_txt
from SXS_ATTRT
where sprsl = sy-langu
and EXIT_NAME = wa_tadir-OBJ_NAME.
FORMAT COLOR COL_NORMAL INTENSIFIED ON.

endcase.



WRITE:/1 SY-VLINE,
2 wa_tadir-OBJ_NAME hotspot on,
41 SY-VLINE ,
42 wf_txt,
105 SY-VLINE.
AT END OF object.
write : /(105) sy-ULINE.
ENDAT.


ENDLOOP.

WRITE:/(105) SY-ULINE.



SKIP.
FORMAT COLOR COL_TOTAL INTENSIFIED ON.
WRITE:/ 'No.of Exits:' , wf_smod.
WRITE:/ 'No.of BADis:' , wf_badi.

ELSE.
FORMAT COLOR COL_NEGATIVE INTENSIFIED ON.
WRITE:/(105) 'No userexits or BADis exist'.
ENDIF.
ELSE.
FORMAT COLOR COL_NEGATIVE INTENSIFIED ON.
WRITE:/(105) 'Transaction does not exist'.
ENDIF.

AT LINE-SELECTION.

data : wf_object type tadir-object.
clear wf_object.

GET CURSOR FIELD FIELD1.
CHECK FIELD1(8) EQ 'WA_TADIR'.
read table jtab with key obj_name = sy-lisel+1(20).
move jtab-object to wf_object.

case wf_object.
when 'SMOD'.
SET PARAMETER ID 'MON' FIELD SY-LISEL+1(10).

CALL TRANSACTION 'SMOD' AND SKIP FIRST SCREEN.
when 'SXSD'.
SET PARAMETER ID 'EXN' FIELD SY-LISEL+1(20).
CALL TRANSACTION 'SE18' AND SKIP FIRST SCREEN.
endcase.


RETURN TO MAIN INDEX:

- Sample Programs On Uploading & Downloading Files.

- Sample Programs On HR ABAP.

- Sample Report Programs On ALV List/ Grid Display.

- Sample Programs On Selection Screen.

- Sample Programs On BDC.

.....Back To MAIN INDEX.

ABAP - Downlaod Internal Table Data To Excel or TXT File Using FM GUI_DOWNLOAD.

TABLES : MARA.

*-------------INTERNAL TABLE DECLARATION
TYPES : BEGIN OF ITAB,
MATNR TYPE MARA-MATNR,
MEINS TYPE MARA-MEINS,
ERNAM LIKE MARA-ERNAM,
AENAM LIKE MARA-AENAM,
MTART LIKE MARA-MTART,
END OF ITAB.

DATA : IG_ITAB type ITAB OCCURS 0.
DATA : V_FILE TYPE STRING.
DATA : T_FILE TYPE RLGRAP-FILENAME.

*-------------RETRIVE DATA FROM DATABASE
SELECT
MATNR
MEINS ERNAM AENAM MTART FROM MARA
INTO CORRESPONDING FIELDS OF TABLE
IG_ITAB
up to 10 rows.


*-------------CALLING FUNCTION MODULES
CALL FUNCTION 'F4_FILENAME' "PASS THE FILE NAME AS U NEED.
EXPORTING
FIELD_NAME = 'T_FILE'
IMPORTING
FILE_NAME = T_FILE.

V_FILE = T_FILE. "STRING CONVERSION

*-------------CALLING GUI DOWNLOAD TO EXTRACT
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
filename = V_FILE
FILETYPE = 'ASC'
WRITE_FIELD_SEPARATOR = 'X'
WRITE_LF = 'X'
tables
data_tab = IG_ITAB .


ALSO READ:

- Sample Program To Open A File On The System Using FM WS_EXECUTE.

- Download Code To System Using FM RS_TESTFRAME_CALL.

- Download Code To The Desktop With Screens.

- Uploading Data From Excel Sheet To Internal Table Using FM ALSM_EXCEL_TO_INTERNAL_TABLE.

- Uploading Images From PC To An Internal Table Using FM BDS_BUSINESSDOCUMENT_CREATEF.




RETURN TO MAIN INDEX:

- Sample Programs On Uploading & Downloading Files.

- Sample Programs On HR ABAP.

- Sample Report Programs On ALV List/ Grid Display.

- Sample Programs On Selection Screen.

- Sample Programs On BDC.

.....Back To MAIN INDEX.

ABAP - Sample Program On Downloading From Internal Table To File Using GUI_DOWNLOAD.

REPORT ZVENKATTEST0.

data: begin of itab occurs 0,
a type c,
c type c,
b type i,
end of itab.

itab-a = 'a' .
itab-c = ':'.
itab-b = 1 .
append itab .


itab-a = 'b' .
itab-c = ':'.
itab-b = 2 .
append itab .


itab-a = 'c' .
itab-c = ':'.
itab-b = 3 .
append itab .

data: file type string .

file = 'C:\Documents and Settings\venkatapp\Desktop\test.txt'.


CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
FILENAME = file
FILETYPE = 'ASC'
WRITE_FIELD_SEPARATOR = ':'
TABLES
DATA_TAB = itab.



ALSO READ:

- Download Internal Table Data To Excel or TXT File Using FM GUI_DOWNLOAD.

- Sample Program To Open A File On The System Using FM WS_EXECUTE.

- Download Code To System Using FM RS_TESTFRAME_CALL.

- Download Code To The Desktop With Screens.

- Uploading Data From Excel Sheet To Internal Table Using FM ALSM_EXCEL_TO_INTERNAL_TABLE.

RETURN TO MAIN INDEX:

- Sample Programs On Uploading & Downloading Files.

- Sample Programs On HR ABAP.

- Sample Report Programs On ALV List/ Grid Display.

- Sample Programs On Selection Screen.

- Sample Programs On BDC.

.....Back To MAIN INDEX.

ABAP - Report Program On Uploading And Downloading Using FM BX_FAVOS_READ_ALL_NODES And BX_FAVOS_WRITE_ALL_NODES.

REPORT ZTEST.

DATA : G_FILE(128) TYPE C.
DATA : G_PATH(80) TYPE C.
DATA : G_END(8) TYPE C.
DATA : G_SUBRC LIKE SY-SUBRC.

PARAMETERS: P_UNAME LIKE SY-UNAME DEFAULT SY-UNAME.
PARAMETERS: P_LANGU LIKE SY-LANGU DEFAULT SY-LANGU.

SELECTION-SCREEN SKIP.

PARAMETERS: P_IMP RADIOBUTTON GROUP ACT .
PARAMETERS: P_EXP RADIOBUTTON GROUP ACT DEFAULT 'X'.

SELECTION-SCREEN ULINE.
SELECTION-SCREEN BEGIN OF BLOCK FIL.
PARAMETERS: P_TYPE LIKE RLGRAP-FILETYPE DEFAULT 'ASC'.
PARAMETERS: P_PATH LIKE G_PATH DEFAULT 'C:\TEMP\XXX.MEN'.

SELECTION-SCREEN END OF BLOCK FIL.

DATA: IT_USER_NODES TYPE TABLE OF BXMNODES1 WITH HEADER LINE.
DATA: IT_FAV_NODES TYPE TABLE OF BXMNODES WITH HEADER LINE.

DATA: BEGIN OF IT_PCF OCCURS 0,
LINE(4096) TYPE C.
DATA: END OF IT_PCF.

INITIALIZATION.
REPLACE 'XXX' WITH SY-UNAME INTO P_PATH.
CONDENSE P_PATH NO-GAPS.
SY-TITLE = 'UPLOAD/DOWNLOAD FAVORITE'.

START-OF-SELECTION.

CLEAR IT_USER_NODES[].
CLEAR IT_FAV_NODES[].

G_FILE = P_UNAME.

IF P_UNAME <> SY-UNAME.
MESSAGE W398(00) WITH 'CURRENT USER <> USER TO BE MODIFIED!'.
ENDIF.

IF P_IMP = 'X'.
PERFORM IMPORT_FAV TABLES IT_FAV_NODES.
PERFORM WRITE_FAV_DB TABLES IT_FAV_NODES.
ELSEIF P_EXP = 'X'.
PERFORM EXPORT_FAV TABLES IT_FAV_NODES.
ENDIF.

FORM DATA_EXPORT TABLES PIT_PCF
USING P_FILE.
CALL FUNCTION 'DOWNLOAD'
EXPORTING
FILENAME = P_FILE
FILETYPE = P_TYPE
TABLES
DATA_TAB = PIT_PCF
EXCEPTIONS
FILE_OPEN_ERROR = 1
FILE_WRITE_ERROR = 2
INVALID_FILESIZE = 3
INVALID_TABLE_WIDTH = 4
INVALID_TYPE = 5
NO_BATCH = 6
UNKNOWN_ERROR = 7
GUI_REFUSE_FILETRANSFER = 8
OTHERS = 9.
IF SY-SUBRC NE 0.
WRITE: / 'FAILURE IN DOWNLOAD' , P_FILE.
ENDIF.
ENDFORM.

FORM DATA_IMPORT TABLES PIT_PCF
USING P_FILE P_SUBRC.
CALL FUNCTION 'UPLOAD'
EXPORTING
FILENAME = P_FILE
FILETYPE = P_TYPE
TABLES
DATA_TAB = PIT_PCF
EXCEPTIONS
CONVERSION_ERROR = 1
FILE_OPEN_ERROR = 2
FILE_READ_ERROR = 3
INVALID_TABLE_WIDTH = 4
INVALID_TYPE = 5
NO_BATCH = 6
UNKNOWN_ERROR = 7
GUI_REFUSE_FILETRANSFER = 8
OTHERS = 9.
IF SY-SUBRC NE 0.
WRITE: / 'UPLOAD FAILED' , P_FILE.
P_SUBRC = SY-SUBRC .
ENDIF.
ENDFORM.

FORM IMPORT_FAV TABLES PIT_FAV_NODES STRUCTURE IT_FAV_NODES.
G_FILE = P_PATH.
PERFORM DATA_IMPORT TABLES IT_FAV_NODES USING G_FILE G_SUBRC .
ENDFORM.

FORM EXPORT_FAV TABLES PIT_FAV_NODES STRUCTURE IT_FAV_NODES.
CALL FUNCTION 'BX_FAVOS_READ_ALL_NODES'
EXPORTING
USER_NAME = P_UNAME
LANGUAGE = P_LANGU
TABLES
OUTPUT_NODES_AND_TEXTS = IT_FAV_NODES.

G_FILE = P_PATH.
PERFORM DATA_EXPORT TABLES IT_FAV_NODES USING G_FILE .
ENDFORM.

FORM WRITE_FAV_DB TABLES PIT_FAV_NODES STRUCTURE IT_FAV_NODES.
CALL FUNCTION 'BX_FAVOS_WRITE_ALL_NODES'
EXPORTING
USER_NAME = P_UNAME
TARGET_CLIENT = SY-MANDT
TABLES
INPUT_NODES_AND_TEXTS = PIT_FAV_NODES.

ENDFORM.


ALSO READ:

- Sample Program On Downloading From Internal Table To File Using GUI_DOWNLOAD.

- Download Internal Table Data To Excel or TXT File Using FM GUI_DOWNLOAD.

- Sample Program To Open A File On The System Using FM WS_EXECUTE.

- Download Code To System Using FM RS_TESTFRAME_CALL.

- Download Code To The Desktop With Screens.

RETURN TO MAIN INDEX:

- Sample Programs On Uploading & Downloading Files.

- Sample Programs On HR ABAP.

- Sample Report Programs On ALV List/ Grid Display.

- Sample Programs On Selection Screen.

- Sample Programs On BDC.

.....Back To MAIN INDEX.


ABAP - F4 (Value On Request) For Month On Selection Screen.

report ztest.
tables: t247 , DFIES.

parameters:p_month LIKE T247-MNR.

data: begin of itab occurs 0,
mnr like t247-mnr,
ktx like t247-ktx,
end of itab .

DATA : LT_FIELDS TYPE TABLE OF DFIES,
LS_FIELD TYPE DFIES.

at selection-screen on value-request for p_month.

select MNR
KTX
from t247 into corresponding fields of table itab
where spras = 'EN'.

CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
EXPORTING
RETFIELD = 'P_MONTH'
DYNPPROG = sy-cprog
DYNPNR = sy-dynnr
DYNPROFIELD = 'MNR'
VALUE_ORG = 'S'
TABLES
VALUE_TAB = itab
FIELD_TAB = LT_FIELDS .


ALSO READ:

- F4 (Value On Request) For Month On Selection Screen.

- ListBox From User Values In Selection Screen.

- F4 Help (Value On Request) For Month And Year.

- Printing The Selection Screen Data On The Report Program.

- Changing Selection Screen Design Through Tcode SE51.

ABAP - F4 (Value On Request) For A Field Of Ztable On Selection Screen.

*--crete a z table with 2 fields and name(10) and age(2) and create entries..

REPORT MESSAGE-ID ZMSF.

TABLES:ZZZ000000 ,DFIES.

DATA: BEGIN OF ITAB OCCURS 0,
NAME(10) ,
AGE(2),
END OF ITAB.

DATA : LT_FIELDS TYPE TABLE OF DFIES WITH HEADER LINE,
LS_FIELD TYPE DFIES.

PARAMETER: P_NAME(10) TYPE C.


AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_NAME.

REFRESH ITAB.
REFRESH LT_FIELDS.

LT_FIELDS-TABNAME = 'ZZZ00000'.
LT_FIELDS-FIELDNAME = 'NAME'.
APPEND LT_FIELDS.
CLEAR LT_FIELDS .

LT_FIELDS-TABNAME = 'ZZZ00000'.
LT_FIELDS-FIELDNAME = 'AGE'.
LT_FIELDS-POSITION = '2'.

APPEND LT_FIELDS.
CLEAR LT_FIELDS .


SELECT NAME
AGE FROM ZZZ00000
INTO TABLE ITAB.


CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
EXPORTING
RETFIELD = LT_FIELDS-FIELDNAME
DYNPPROG = SY-CPROG
DYNPNR = SY-DYNNR
DYNPROFIELD = 'NAME'
VALUE_ORG = 'S'
TABLES
VALUE_TAB = ITAB
FIELD_TAB = LT_FIELDS.

ABAP - F4 (Value On Request) For Ztable On Selection Screen.

*--create a z table with fields pernr like pa0002-pernr
* vorna like pa0002-vorna
* nachn like pa0002-nachn
* and create entries for the table

report message-id zmsf.

tables:zzz000000 ,DFIES.

DATA: BEGIN OF itab OCCURS 0,
pernr like pa0002-pernr,
vorna like pa0002-vorna,
nachn like pa0002-nachn,
END OF itab.

DATA : LT_FIELDS TYPE TABLE OF DFIES,
LS_FIELD TYPE DFIES.

PARAMETER: pernr LIKE pa0002-pernr .


AT SELECTION-SCREEN ON VALUE-REQUEST FOR pernr.

select pernr
vorna
nachn
from zzz000000
into table itab .

CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
EXPORTING
RETFIELD = 'PERNR'
DYNPPROG = sy-cprog
DYNPNR = sy-dynnr
DYNPROFIELD = 'PERNR'
VALUE_ORG = 'S'
TABLES
VALUE_TAB = itab
FIELD_TAB = LT_FIELDS .


start-of-selection.

select pernr
vorna
nachn
from zzz000000
into table itab
where pernr = pernr.

loop at itab.
write:/ itab-pernr,
itab-vorna,
itab-nachn.
endloop.


ALSO READ:

- F4 (Value On Request) For A Field Of Ztable On Selection Screen.

- F4 (Value On Request) For Month On Selection Screen.

- F4 (Value On Request) For Month On Selection Screen.

- ListBox From User Values In Selection Screen.

- F4 Help (Value On Request) For Month And Year.

ABAP - F4 (Value On Request) For Date On Selection Screen.

report message-id zmsf.

DATA:
l_select_date LIKE workflds-gkday,
l_select_week LIKE scal-week.

* pop up calendar.
CALL FUNCTION 'F4_DATE'
EXPORTING
date_for_first_month = sy-datum
IMPORTING
select_date = l_select_date
select_week = l_select_week
EXCEPTIONS
calendar_buffer_not_loadable = 1
date_after_range = 2
date_before_range = 3
date_invalid = 4
factory_calendar_not_found = 5
holiday_calendar_not_found = 6
parameter_conflict = 7
OTHERS = 8.
IF sy-subrc <> 0.
EXIT.
ENDIF.


* Validate date - An invalid date is caused by using the cancel
* button on the calendar
CALL FUNCTION 'DATE_CHECK_PLAUSIBILITY'
EXPORTING
date = l_select_date
EXCEPTIONS
plausibility_check_failed = 1
OTHERS = 2.
IF sy-subrc <> 0.
* User has cancelled the calendar
EXIT.
ENDIF.


ALSO READ:

- F4 (Value On Request) For Ztable On Selection Screen.

- F4 (Value On Request) For A Field Of Ztable On Selection Screen.

- F4 (Value On Request) For Month On Selection Screen.

- F4 (Value On Request) For Month On Selection Screen.

- ListBox From User Values In Selection Screen.

ABAP : Alignment In Report Program Using WRITE Statement.

report LINE-SIZE 200.

WRITE:
/(140) 'THIS IS THE HEADER OF THE PAGE' CENTERED,
/(140) 'THIS IS THE VENKAT TEST HEADING' CENTERED,
/(140) 'HI FRIEND THIS IS WORKING' CENTERED,

/(10) 'date:' LEFT-JUSTIFIED,
(10) sy-datum LEFT-JUSTIFIED,
(90) 'CLIENT' RIGHT-JUSTIFIED,
(08) SY-MANDT RIGHT-JUSTIFIED,
/(10) 'time:' LEFT-JUSTIFIED,
(10) sy-uzeit LEFT-JUSTIFIED,
(92) 'USERNAME' RIGHT-JUSTIFIED,
(10) SY-UNAME RIGHT-JUSTIFIED.


RETURN TO MAIN INDEX:

- Sample Programs On Uploading & Downloading Files.

- Sample Programs On HR ABAP.

- Sample Report Programs On ALV List/ Grid Display.

- Sample Programs On Selection Screen.

- Sample Programs On BDC.

.....Back To MAIN INDEX.

SAP ABAP: Insert, Delete & Update ZTABLE From Workarea.

*--hi create a zdatabase table with 2 entries name(10) and age(2) .

report ztest .

tables:ZZZ00000.

data: begin of itab occurs 0,
name(10) type c,
age type i,
end of itab .
selection-screen begin of line .
parameters:p_wa(100) type c .
selection-screen end of line .
selection-screen skip 2.
selection-screen begin of line .
SELECTION-SCREEN:
PUSHBUTTON 2(10) but1 USER-COMMAND cli1,
PUSHBUTTON 15(20) but2 USER-COMMAND cli2 ,
PUSHBUTTON 40(20) but3 USER-COMMAND cli3 visible length 5 .

selection-screen end of line .

at selection-screen .

case sy-ucomm .

when 'CLI1'.
ZZZ00000-name = p_wa+0(10) .
ZZZ00000-age = p_wa+11(2).
insert ZZZ00000.

when 'CLI2'.
delete from ZZZ00000 where name = p_wa+0(10)..

when 'CLI3'.
update ZZZ00000 from p_wa .

endcase .


RETURN TO MAIN INDEX:

- Sample Programs On Uploading & Downloading Files.

- Sample Programs On HR ABAP.

- Sample Report Programs On ALV List/ Grid Display.

- Sample Programs On Selection Screen.

- Sample Programs On BDC.

.....Back To MAIN INDEX.

ABAP - ALV Example On Blocked List.

REPORT z_alv_list_block.

TYPE-POOLS: slis. " ALV Global types

SELECTION-SCREEN :
SKIP,
BEGIN OF LINE,
COMMENT 5(27) v_1 FOR FIELD p_max. "#EC NEEDED
PARAMETERS p_max(2) TYPE n DEFAULT '02' OBLIGATORY.
SELECTION-SCREEN END OF LINE.

DATA:

* 1st Table
BEGIN OF gt_kna1 OCCURS 0, " Data displayed
kunnr LIKE kna1-kunnr, " Customer number
ernam LIKE kna1-ernam, " Name of Person who Created
erdat LIKE kna1-erdat, " Creation date
name1 LIKE kna1-name1, " Name 1
END OF gt_kna1,

* 2nd Table
BEGIN OF gt_mara OCCURS 0,
ernam LIKE mara-ernam, " Name of Person who Created
matnr LIKE mara-matnr, " Material number
ersda LIKE mara-ersda, " Creation date
brgew LIKE mara-brgew, " Gross weight
END OF gt_mara,

* 3rd Table
BEGIN OF gt_vbak OCCURS 0,
vkorg LIKE vbak-vkorg, " Sales organization
kunnr LIKE vbak-kunnr, " Sold-to party
vbeln LIKE vbak-vbeln, " Sales document
netwr LIKE vbak-netwr, " Net Value of the Sales Order
waerk LIKE vbak-waerk, " SD document currency
END OF gt_vbak.


INITIALIZATION.
v_1 = 'Maximum of records to read'.


START-OF-SELECTION.

* Read data
SELECT * FROM kna1
UP TO p_max ROWS
INTO CORRESPONDING FIELDS OF TABLE gt_kna1.

SELECT * FROM mara
UP TO p_max ROWS
INTO CORRESPONDING FIELDS OF TABLE gt_mara.

SELECT * FROM vbak
UP TO p_max ROWS
INTO CORRESPONDING FIELDS OF TABLE gt_vbak.

CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_INIT'
EXPORTING
i_callback_program = sy-cprog
i_callback_user_command = 'USER_COMMAND'.

PERFORM list_append TABLES gt_kna1
USING '1'
'GT_KNA1'.

PERFORM list_append TABLES gt_mara
USING '2'
'GT_MARA'.

PERFORM list_append TABLES gt_vbak
USING '3'
'GT_VBAK'.

PERFORM f_list_display.
*---------------------------------------------------------------------*
* FORM USER_COMMAND *
*---------------------------------------------------------------------*
FORM user_command USING i_ucomm LIKE sy-ucomm
is_selfield TYPE slis_selfield. "#EC CALLED

CASE i_ucomm.
WHEN '&IC1'. " Pick
CASE is_selfield-tabname.
WHEN 'GT_MARA'.
WHEN 'GT_KNA1'.
WHEN 'GT_VBAK'.
READ TABLE gt_vbak INDEX is_selfield-tabindex.

IF sy-subrc EQ 0.
* Sales order number
SET PARAMETER ID 'AUN' FIELD gt_vbak-vbeln.

* Display Sales Order
CALL TRANSACTION 'VA03' AND SKIP FIRST SCREEN.
ENDIF.
ENDCASE.
ENDCASE.

ENDFORM. " USER_COMMAND
*---------------------------------------------------------------------*< /div>
* Form list_append
*---------------------------------------------------------------------*
FORM list_append TABLES ut_table
USING u_no TYPE char1
u_tabname TYPE slis_tabname.
* Macro definition
DEFINE m_fieldcat.
ls_fieldcat-fieldname = &1.
ls_fieldcat-ref_tabname = &2.
append ls_fieldcat to lt_fieldcat.
END-OF-DEFINITION.
DEFINE m_sort.
ls_sort-fieldname = &1.
ls_sort-up = 'X'.
append ls_sort to lt_sort.
END-OF-DEFINITION.
DATA :
ls_fieldcat TYPE slis_fieldcat_alv,
lt_fieldcat TYPE slis_t_fieldcat_alv, " Field catalog
ls_sort TYPE slis_sortinfo_alv,
lt_sort TYPE slis_t_sortinfo_alv. " Sort table
DATA:
lt_events TYPE slis_t_event,
ls_event TYPE slis_alv_event,
ls_layout TYPE slis_layout_alv.
ls_layout-group_change_edit = 'X'.
ls_layout-colwidth_optimize = 'X'.
ls_layout-zebra = 'X'.
ls_layout-detail_popup = 'X'.
ls_layout-get_selinfos = 'X'.
ls_layout-max_linesize = '200'.

CASE u_no.

WHEN '1'.
* Build field catalog and sort table
m_fieldcat 'KUNNR' 'KNA1'.
m_fieldcat 'ERNAM' 'KNA1'.
m_fieldcat 'ERDAT' 'KNA1'.
m_fieldcat 'NAME1' 'KNA1'.
m_sort 'KUNNR'.

WHEN '2'.
m_fieldcat 'MATNR' 'MARA'.
m_fieldcat 'ERNAM' 'MARA'.
m_fieldcat 'ERSDA' 'MARA'.
m_fieldcat 'BRGEW' 'MARA'.
m_sort 'MATNR'.

WHEN '3'.
m_fieldcat 'VBELN' 'VBAK'.
m_fieldcat 'VKORG' 'VBAK'.
m_fieldcat 'KUNNR' 'VBAK'.
m_fieldcat 'NETWR' 'VBAK'.
m_fieldcat 'WAERK' 'VBAK'.
m_sort 'VBELN'.

ENDCASE.

IF u_no CA '13'.
MOVE 'TOP_OF_PAGE' TO ls_event-name.
CONCATENATE 'TOP_OF_PAGE' u_no INTO ls_event-form.
APPEND ls_event TO lt_events.
ELSE.
MOVE 'TOP_OF_LIST' TO ls_event-name.
CONCATENATE 'TOP_OF_LIST' u_no INTO ls_event-form.
APPEND ls_event TO lt_events.
ENDIF.

CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_APPEND'
EXPORTING
it_fieldcat = lt_fieldcat
is_layout = ls_layout
i_tabname = u_tabname
it_events = lt_events
it_sort = lt_sort
TABLES
t_outtab = ut_table .

ENDFORM. " LIST_APPEND
*---------------------------------------------------------------------*
* Form f_list_display
*---------------------------------------------------------------------*
FORM f_list_display.

DATA ls_print TYPE slis_print_alv.

ls_print-no_print_selinfos = 'X'. " Display no selection infos
ls_print-no_print_listinfos = 'X'. " Display no listinfos
ls_print-reserve_lines = 2. " Lines reserved for end of page

CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_DISPLAY'
EXPORTING
i_interface_check = ' '
is_print = ls_print .

ENDFORM. " F_LIST_DISPLAY
*---------------------------------------------------------------------*
* FORM top_of_page1 *
*---------------------------------------------------------------------*
FORM top_of_page1. "#EC CALLED
PERFORM top_of_page.
ENDFORM.
*---------------------------------------------------------------------*
* FORM top_of_page3 *
*---------------------------------------------------------------------*
FORM top_of_page3. "#EC CALLED
PERFORM top_of_page.
ENDFORM.
*---------------------------------------------------------------------*
* FORM top_of_page *
*---------------------------------------------------------------------*
FORM top_of_page.
ULINE.
WRITE : sy-uname, sy-title(56) CENTERED, sy-datum.
ULINE.
ENDFORM.
*---------------------------------------------------------------------*
* FORM top_of_list2 *
*---------------------------------------------------------------------*
FORM top_of_list2. "#EC CALLED
WRITE 'TOP OF LIST2'.
ENDFORM.


ALSO READ:

- Simple ALV Grid/List With Save Layout.

- Checkbox In ALV Program.

- ALV Header With Logo.

- Including Line After Subtotal In ALV Display.

..... Back To Index On ALV List/ Grid Display.

..... Back To MAIN INDEX.


ABAP - ALV Interactive Report With Variant Option.

REPORT ztest NO STANDARD PAGE HEADING LINE-SIZE 650
MESSAGE-ID ZZ_9838 .

TYPE-POOLS: SLIS.
*type declaration for values from ekko
TYPES: BEGIN OF I_EKKO,
EBELN LIKE EKKO-EBELN,
AEDAT LIKE EKKO-AEDAT,
BUKRS LIKE EKKO-BUKRS,
BSART LIKE EKKO-BSART,
LIFNR LIKE EKKO-LIFNR,
END OF I_EKKO.

DATA: IT_EKKO TYPE STANDARD TABLE OF I_EKKO INITIAL SIZE 0,
WA_EKKO TYPE I_EKKO.

*type declaration for values from ekpo
TYPES: BEGIN OF I_EKPO,
EBELN LIKE EKPO-EBELN,
EBELP LIKE EKPO-EBELP,
MATNR LIKE EKPO-MATNR,
MENGE LIKE EKPO-MENGE,
MEINS LIKE EKPO-MEINS,
NETPR LIKE EKPO-NETPR,
END OF I_EKPO.

DATA: IT_EKPO TYPE STANDARD TABLE OF I_EKPO INITIAL SIZE 0,
WA_EKPO TYPE I_EKPO .

*variable for Report ID
DATA: V_REPID LIKE SY-REPID .

*declaration for fieldcatalog
DATA: I_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV,
WA_FIELDCAT TYPE SLIS_FIELDCAT_ALV.


DATA: IT_LISTHEADER TYPE SLIS_T_LISTHEADER.

* declaration for events table where user comand or set PF status will
* be defined
DATA: V_EVENTS TYPE SLIS_T_EVENT,
WA_EVENT TYPE SLIS_ALV_EVENT.

* declartion for layout
DATA: ALV_LAYOUT TYPE SLIS_LAYOUT_ALV.

* declaration for variant(type of display we want)
DATA: I_VARIANT TYPE DISVARIANT,
I_VARIANT1 TYPE DISVARIANT,
I_SAVE(1) TYPE C.

*PARAMETERS : p_var TYPE disvariant-variant.

*Title displayed when the alv list is displayed
DATA: I_TITLE_EKKO TYPE LVC_TITLE VALUE 'FIRST LIST DISPLAYED'.
DATA: I_TITLE_EKPO TYPE LVC_TITLE VALUE 'SECONDRY LIST DISPLAYED'.

INITIALIZATION.
V_REPID = SY-REPID.
PERFORM BUILD_FIELDCATLOG.
PERFORM EVENT_CALL.
PERFORM POPULATE_EVENT.

START-OF-SELECTION.
PERFORM DATA_RETRIEVAL.
PERFORM BUILD_LISTHEADER USING IT_LISTHEADER.
PERFORM DISPLAY_ALV_REPORT.

*&--------------------------------------------------------------------*
*& Form BUILD_FIELDCATLOG
*&--------------------------------------------------------------------*
* Fieldcatalog has all the field details from ekko
*---------------------------------------------------------------------*
FORM BUILD_FIELDCATLOG.
WA_FIELDCAT-TABNAME = 'IT_EKKO'.
WA_FIELDCAT-FIELDNAME = 'EBELN'.
WA_FIELDCAT-SELTEXT_M = 'PO NO.'.
APPEND WA_FIELDCAT TO I_FIELDCAT.
CLEAR WA_FIELDCAT.

WA_FIELDCAT-TABNAME = 'IT_EKKO'.
WA_FIELDCAT-FIELDNAME = 'AEDAT'.
WA_FIELDCAT-SELTEXT_M = 'DATE.'.
APPEND WA_FIELDCAT TO I_FIELDCAT.
CLEAR WA_FIELDCAT.

WA_FIELDCAT-TABNAME = 'IT_EKKO'.
WA_FIELDCAT-FIELDNAME = 'BUKRS'.
WA_FIELDCAT-SELTEXT_M = 'COMPANY CODE'.
APPEND WA_FIELDCAT TO I_FIELDCAT.
CLEAR WA_FIELDCAT.

WA_FIELDCAT-TABNAME = 'IT_EKKO'.
WA_FIELDCAT-FIELDNAME = 'BUKRS'.
WA_FIELDCAT-SELTEXT_M = 'DOCMENT TYPE'.
APPEND WA_FIELDCAT TO I_FIELDCAT.
CLEAR WA_FIELDCAT.

WA_FIELDCAT-TABNAME = 'IT_EKKO'.
WA_FIELDCAT-FIELDNAME = 'LIFNR'.
WA_FIELDCAT-NO_OUT = 'X'.
WA_FIELDCAT-SELTEXT_M = 'VENDOR CODE'.
APPEND WA_FIELDCAT TO I_FIELDCAT.
CLEAR WA_FIELDCAT.


ENDFORM. "BUILD_FIELDCATLOG

*&--------------------------------------------------------------------*
*& Form EVENT_CALL
*&--------------------------------------------------------------------*
* we get all events - TOP OF PAGE or USER COMMAND in table v_events
*---------------------------------------------------------------------*
FORM EVENT_CALL.
CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
EXPORTING
I_LIST_TYPE = 0
IMPORTING
ET_EVENTS = V_EVENTS .

ENDFORM. "EVENT_CALL

*&--------------------------------------------------------------------*
*& Form POPULATE_EVENT
*&--------------------------------------------------------------------*
* Events populated for TOP OF PAGE & USER COMAND
*---------------------------------------------------------------------*
FORM POPULATE_EVENT.
READ TABLE V_EVENTS INTO WA_EVENT WITH KEY NAME = 'TOP_OF_PAGE'.
IF SY-SUBRC EQ 0.
WA_EVENT-FORM = 'TOP_OF_PAGE'.
MODIFY V_EVENTS FROM WA_EVENT TRANSPORTING FORM WHERE NAME =
WA_EVENT-FORM.
ENDIF.

READ TABLE V_EVENTS INTO WA_EVENT WITH KEY NAME = 'USER_COMMAND'.
IF SY-SUBRC EQ 0.
WA_EVENT-FORM = 'USER_COMMAND'.
MODIFY V_EVENTS FROM WA_EVENT TRANSPORTING FORM WHERE NAME =
WA_EVENT-NAME.
ENDIF.
ENDFORM. "POPULATE_EVENT


*&--------------------------------------------------------------------*
*& Form data_retrieval
*&--------------------------------------------------------------------*
* retreiving values from the database table ekko
*---------------------------------------------------------------------*
FORM DATA_RETRIEVAL.
SELECT EBELN AEDAT BUKRS BSART LIFNR FROM EKKO INTO TABLE IT_EKKO.

ENDFORM. "data_retrieval
*&--------------------------------------------------------------------*
*& Form bUild_listheader
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
* -->I_LISTHEADEtext
*---------------------------------------------------------------------*
FORM BUILD_LISTHEADER USING I_LISTHEADER TYPE SLIS_T_LISTHEADER.
DATA HLINE TYPE SLIS_LISTHEADER.
HLINE-INFO = 'this is my first alv pgm'.
HLINE-TYP = 'H'.
ENDFORM. "build_listheader

*&--------------------------------------------------------------------*
*& Form display_alv_report
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
FORM DISPLAY_ALV_REPORT.
V_REPID = SY-REPID.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = V_REPID
I_CALLBACK_USER_COMMAND = 'USER_COMMAND'
I_CALLBACK_TOP_OF_PAGE = 'TOP_OF_PAGE'
I_GRID_TITLE = I_TITLE_EKKO
IT_FIELDCAT = I_FIELDCAT[]
I_SAVE = 'A'
IT_EVENTS = V_EVENTS
TABLES
T_OUTTAB = IT_EKKO .

ENDFORM. "display_alv_report




*&--------------------------------------------------------------------*
*& Form TOP_OF_PAGE
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
FORM TOP_OF_PAGE.

CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
EXPORTING
IT_LIST_COMMENTARY = IT_LISTHEADER .

ENDFORM. "TOP_OF_PAGE

*&--------------------------------------------------------------------*
*& Form USER_COMMAND
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
* -->R_UCOMM text
* -->, text
* -->RS_SLEFIELDtext
*---------------------------------------------------------------------*
FORM USER_COMMAND USING R_UCOMM LIKE SY-UCOMM
RS_SELFIELD TYPE SLIS_SELFIELD.
CASE R_UCOMM.
WHEN '&IC1'.
READ TABLE IT_EKKO INTO WA_EKKO INDEX RS_SELFIELD-TABINDEX.
PERFORM BUILD_FIELDCATLOG_EKPO.
PERFORM EVENT_CALL_EKPO.
PERFORM POPULATE_EVENT_EKPO.
PERFORM DATA_RETRIEVAL_EKPO.
PERFORM BUILD_LISTHEADER_EKPO USING IT_LISTHEADER.
PERFORM DISPLAY_ALV_EKPO.
ENDCASE.
ENDFORM. "user_command
*&--------------------------------------------------------------------*
*& Form BUILD_FIELDCATLOG_EKPO
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
FORM BUILD_FIELDCATLOG_EKPO.

WA_FIELDCAT-TABNAME = 'IT_EKPO'.
WA_FIELDCAT-FIELDNAME = 'EBELN'.
WA_FIELDCAT-SELTEXT_M = 'PO NO.'.
APPEND WA_FIELDCAT TO I_FIELDCAT.
CLEAR WA_FIELDCAT.

WA_FIELDCAT-TABNAME = 'IT_EKPO'.
WA_FIELDCAT-FIELDNAME = 'EBELP'.
WA_FIELDCAT-SELTEXT_M = 'LINE NO'.
APPEND WA_FIELDCAT TO I_FIELDCAT.
CLEAR WA_FIELDCAT.

WA_FIELDCAT-TABNAME = 'I_EKPO'.
WA_FIELDCAT-FIELDNAME = 'MATNR'.
WA_FIELDCAT-SELTEXT_M = 'MATERIAL NO.'.
APPEND WA_FIELDCAT TO I_FIELDCAT.
CLEAR WA_FIELDCAT.

WA_FIELDCAT-TABNAME = 'I_EKPO'.
WA_FIELDCAT-FIELDNAME = 'MENGE'.
WA_FIELDCAT-SELTEXT_M = 'QUANTITY'.
APPEND WA_FIELDCAT TO I_FIELDCAT.
CLEAR WA_FIELDCAT.

WA_FIELDCAT-TABNAME = 'I_EKPO'.
WA_FIELDCAT-FIELDNAME = 'MEINS'.
WA_FIELDCAT-SELTEXT_M = 'UOM'.
APPEND WA_FIELDCAT TO I_FIELDCAT.
CLEAR WA_FIELDCAT.

WA_FIELDCAT-TABNAME = 'I_EKPO'.
WA_FIELDCAT-FIELDNAME = 'NETPR'.
WA_FIELDCAT-SELTEXT_M = 'PRICE'.
APPEND WA_FIELDCAT TO I_FIELDCAT.
CLEAR WA_FIELDCAT.


ENDFORM. "BUILD_FIELDCATLOG_EKPO

*&--------------------------------------------------------------------*
*& Form event_call_ekpo
*&--------------------------------------------------------------------*
* we get all events - TOP OF PAGE or USER COMMAND in table v_events
*---------------------------------------------------------------------*
FORM EVENT_CALL_EKPO.
CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
EXPORTING
I_LIST_TYPE = 0
IMPORTING
ET_EVENTS = V_EVENTS .

ENDFORM. "event_call_ekpo


*&--------------------------------------------------------------------*
*& Form POPULATE_EVENT
*&--------------------------------------------------------------------*
* Events populated for TOP OF PAGE & USER COMAND
*---------------------------------------------------------------------*
FORM POPULATE_EVENT_EKPO.
READ TABLE V_EVENTS INTO WA_EVENT WITH KEY NAME = 'TOP_OF_PAGE'.
IF SY-SUBRC EQ 0.
WA_EVENT-FORM = 'TOP_OF_PAGE'.
MODIFY V_EVENTS FROM WA_EVENT TRANSPORTING FORM WHERE NAME =
WA_EVENT-FORM.
ENDIF.

ENDFORM. "POPULATE_EVENT

*&--------------------------------------------------------------------*
*& Form TOP_OF_PAGE
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
FORM F_TOP_OF_PAGE.
CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
EXPORTING
IT_LIST_COMMENTARY = IT_LISTHEADER .

ENDFORM. "TOP_OF_PAGE

*&--------------------------------------------------------------------*
*& Form USER_COMMAND
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
* -->R_UCOMM text
* -->, text
* -->RS_SLEFIELDtext
*---------------------------------------------------------------------*

*retreiving values from the database table ekko
FORM DATA_RETRIEVAL_EKPO.
SELECT EBELN EBELP MATNR MENGE MEINS NETPR FROM EKPO INTO TABLE IT_EKPO.
ENDFORM.

FORM BUILD_LISTHEADER_EKPO USING I_LISTHEADER TYPE SLIS_T_LISTHEADER.
DATA: HLINE1 TYPE SLIS_LISTHEADER.
HLINE1-TYP = 'H'.
HLINE1-INFO = 'CHECKING PGM'.
ENDFORM.


FORM DISPLAY_ALV_EKPO.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = V_REPID
I_CALLBACK_TOP_OF_PAGE = 'TOP_OF_PAGE'
I_GRID_TITLE = I_TITLE_EKPO
IT_FIELDCAT = I_FIELDCAT[]
I_SAVE = 'A'
IT_EVENTS = V_EVENTS
TABLES
T_OUTTAB = IT_EKPO .
ENDFORM.


ALSO READ:

- Simple ALV Grid/List With Save Layout.

- Checkbox In ALV Program.

- ALV Header With Logo.

- Including Line After Subtotal In ALV Display.

..... Back To Index On ALV List/ Grid Display.

..... Back To MAIN INDEX.


ABAP - Sample ALV Grid Display Report Program.

REPORT ZTUFI091 .

*& Report ZDEMO_ALVGRID
*& Example of a simple ALV Grid Report
*& The basic requirement for this demo is to display a number of fields from the EKKO table.


REPORT zdemo_alvgrid .

TABLES: ekko.

type-pools: slis. "ALV Declarations

* Data Declaration
*----------------
TYPES: BEGIN OF t_ekko,
ebeln TYPE ekpo-ebeln,
ebelp TYPE ekpo-ebelp,
statu TYPE ekpo-statu,
aedat TYPE ekpo-aedat,
matnr TYPE ekpo-matnr,
menge TYPE ekpo-menge,
meins TYPE ekpo-meins,
netpr TYPE ekpo-netpr,
peinh TYPE ekpo-peinh,
END OF t_ekko.

DATA: it_ekko TYPE STANDARD TABLE OF t_ekko INITIAL SIZE 0,
wa_ekko TYPE t_ekko.

*ALV data declarations

data: fieldcatalog type slis_t_fieldcat_alv with header line,
gd_tab_group type slis_t_sp_group_alv,
gd_layout type slis_layout_alv,
gd_repid like sy-repid,
gt_events type slis_t_event,
gd_prntparams type slis_print_alv.


************************************************************************
*Start-of-selection.
START-OF-SELECTION.

perform data_retrieval.
perform build_fieldcatalog.
perform build_layout.
perform build_events.
perform build_print_params.
perform display_alv_report.


*&---------------------------------------------------------------------*
*& Form BUILD_FIELDCATALOG
*&---------------------------------------------------------------------*
* Build Fieldcatalog for ALV Report
*----------------------------------------------------------------------*
form build_fieldcatalog.

* There are a number of ways to create a fieldcat.
* For the purpose of this example i will build the fieldcatalog manualy
* by populating the internal table fields individually and then
* appending the rows. This method can be the most time consuming but can
* also allow you more control of the final product.

* Beware though, you need to ensure that all fields required are
* populated. When using some of functionality available via ALV, such as
* total. You may need to provide more information than if you were
* simply displaying the result
* I.e. Field type may be required in-order for
* the 'TOTAL' function to work.

fieldcatalog-fieldname = 'EBELN'.
fieldcatalog-seltext_m = 'Purchase Order'.
fieldcatalog-col_pos = 0.
fieldcatalog-outputlen = 10.
fieldcatalog-emphasize = 'X'.
fieldcatalog-key = 'X'.
* fieldcatalog-do_sum = 'X'.
* fieldcatalog-no_zero = 'X'.
append fieldcatalog to fieldcatalog.
clear fieldcatalog.

fieldcatalog-fieldname = 'EBELP'.
fieldcatalog-seltext_m = 'PO Item'.
fieldcatalog-col_pos = 1.
append fieldcatalog to fieldcatalog.
clear fieldcatalog.

fieldcatalog-fieldname = 'STATU'.
fieldcatalog-seltext_m = 'Status'.
fieldcatalog-col_pos = 2.
append fieldcatalog to fieldcatalog.
clear fieldcatalog.

fieldcatalog-fieldname = 'AEDAT'.
fieldcatalog-seltext_m = 'Item change date'.
fieldcatalog-col_pos = 3.
append fieldcatalog to fieldcatalog.
clear fieldcatalog.

fieldcatalog-fieldname = 'MATNR'.
fieldcatalog-seltext_m = 'Material Number'.
fieldcatalog-col_pos = 4.
append fieldcatalog to fieldcatalog.
clear fieldcatalog.

fieldcatalog-fieldname = 'MENGE'.
fieldcatalog-seltext_m = 'PO quantity'.
fieldcatalog-col_pos = 5.
append fieldcatalog to fieldcatalog.
clear fieldcatalog.

fieldcatalog-fieldname = 'MEINS'.
fieldcatalog-seltext_m = 'Order Unit'.
fieldcatalog-col_pos = 6.
append fieldcatalog to fieldcatalog.
clear fieldcatalog.

fieldcatalog-fieldname = 'NETPR'.
fieldcatalog-seltext_m = 'Net Price'.
fieldcatalog-col_pos = 7.
fieldcatalog-outputlen = 15.
fieldcatalog-datatype = 'CURR'.
append fieldcatalog to fieldcatalog.
clear fieldcatalog.

fieldcatalog-fieldname = 'PEINH'.
fieldcatalog-seltext_m = 'Price Unit'.
fieldcatalog-col_pos = 8.
append fieldcatalog to fieldcatalog.
clear fieldcatalog.
endform. " BUILD_FIELDCATALOG


*&---------------------------------------------------------------------*
*& Form BUILD_LAYOUT
*&---------------------------------------------------------------------*
* Build layout for ALV grid report
*----------------------------------------------------------------------*
form build_layout.
gd_layout-no_input = 'X'.
gd_layout-colwidth_optimize = 'X'.
gd_layout-totals_text = 'Totals'(201).
* gd_layout-totals_only = 'X'.
* gd_layout-f2code = 'DISP'. "Sets fcode for when double
* "click(press f2)
* gd_layout-zebra = 'X'.
* gd_layout-group_change_edit = 'X'.
* gd_layout-header_text = 'helllllo'.
endform. " BUILD_LAYOUT


*&---------------------------------------------------------------------*
*& Form DISPLAY_ALV_REPORT
*&---------------------------------------------------------------------*
* Display report using ALV grid
*----------------------------------------------------------------------*
form display_alv_report.
gd_repid = sy-repid.
call function 'REUSE_ALV_GRID_DISPLAY'
exporting
i_callback_program = gd_repid
i_callback_top_of_page = 'TOP-OF-PAGE' "see FORM
i_callback_user_command = 'USER_COMMAND'
* i_grid_title = outtext
is_layout = gd_layout
it_fieldcat = fieldcatalog[]
* it_special_groups = gd_tabgroup
it_events = gt_events
is_print = gd_prntparams
i_save = 'X'
* is_variant = z_template
tables
t_outtab = it_ekko
exceptions
program_error = 1
others = 2.
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. " DISPLAY_ALV_REPORT


*&---------------------------------------------------------------------*
*& Form DATA_RETRIEVAL
*&---------------------------------------------------------------------*
* Retrieve data form EKPO table and populate itab it_ekko
*----------------------------------------------------------------------*
form data_retrieval.

select ebeln ebelp statu aedat matnr menge meins netpr peinh
up to 10 rows
from ekpo
into table it_ekko.
endform. " DATA_RETRIEVAL


*-------------------------------------------------------------------*
* Form TOP-OF-PAGE *
*-------------------------------------------------------------------*
* ALV Report Header *
*-------------------------------------------------------------------*
Form top-of-page.
*ALV Header declarations
data: t_header type slis_t_listheader,
wa_header type slis_listheader,
t_line like wa_header-info,
ld_lines type i,
ld_linesc(10) type c.

* Title
wa_header-typ = 'H'.
wa_header-info = 'EKKO Table Report'.
append wa_header to t_header.
clear wa_header.

* Date
wa_header-typ = 'S'.
wa_header-key = 'Date: '.
CONCATENATE sy-datum+6(2) '.'
sy-datum+4(2) '.'
sy-datum(4) INTO wa_header-info. "todays date
append wa_header to t_header.
clear: wa_header.

* Total No. of Records Selected
describe table it_ekko lines ld_lines.
ld_linesc = ld_lines.
concatenate 'Total No. of Records Selected: ' ld_linesc
into t_line separated by space.
wa_header-typ = 'A'.
wa_header-info = t_line.
append wa_header to t_header.
clear: wa_header, t_line.

call function 'REUSE_ALV_COMMENTARY_WRITE'
exporting
it_list_commentary = t_header.
* i_logo = 'Z_LOGO'.
endform.


*------------------------------------------------------------------*
* FORM USER_COMMAND *
*------------------------------------------------------------------*
* --> R_UCOMM *
* --> RS_SELFIELD *
*------------------------------------------------------------------*
FORM user_command USING r_ucomm LIKE sy-ucomm
rs_selfield TYPE slis_selfield.

* Check function code
CASE r_ucomm.
WHEN '&IC1'.
* Check field clicked on within ALVgrid report
IF rs_selfield-fieldname = 'EBELN'.
* Read data table, using index of row user clicked on
READ TABLE it_ekko INTO wa_ekko INDEX rs_selfield-tabindex.
* Set parameter ID for transaction screen field
SET PARAMETER ID 'BES' FIELD wa_ekko-ebeln.
* Sxecute transaction ME23N, and skip initial data entry screen
CALL TRANSACTION 'ME23N' AND SKIP FIRST SCREEN.
ENDIF.
ENDCASE.
ENDFORM.


*&---------------------------------------------------------------------*
*& Form BUILD_EVENTS
*&---------------------------------------------------------------------*
* Build events table
*----------------------------------------------------------------------*
form build_events.
data: ls_event type slis_alv_event.

call function 'REUSE_ALV_EVENTS_GET'
exporting
i_list_type = 0
importing
et_events = gt_events[].
read table gt_events with key name = slis_ev_end_of_page
into ls_event.
if sy-subrc = 0.
move 'END_OF_PAGE' to ls_event-form.
append ls_event to gt_events.
endif.

read table gt_events with key name = slis_ev_end_of_list
into ls_event.
if sy-subrc = 0.
move 'END_OF_LIST' to ls_event-form.
append ls_event to gt_events.
endif.
endform. " BUILD_EVENTS


*&---------------------------------------------------------------------*
*& Form BUILD_PRINT_PARAMS
*&---------------------------------------------------------------------*
* Setup print parameters
*----------------------------------------------------------------------*
form build_print_params.
gd_prntparams-reserve_lines = '3'. "Lines reserved for footer
gd_prntparams-no_coverpage = 'X'.
endform. " BUILD_PRINT_PARAMS


*&---------------------------------------------------------------------*
*& Form END_OF_PAGE
*&---------------------------------------------------------------------*
form END_OF_PAGE.
data: listwidth type i,
ld_pagepos(10) type c,
ld_page(10) type c.

write: sy-uline(50).
skip.
write:/40 'Page:', sy-pagno .
endform.


*&---------------------------------------------------------------------*
*& Form END_OF_LIST
*&---------------------------------------------------------------------*
form END_OF_LIST.
data: listwidth type i,
ld_pagepos(10) type c,
ld_page(10) type c.

skip.
write:/40 'Page:', sy-pagno .
endform.


ALSO READ:

- Simple ALV Grid/List With Save Layout.

- Checkbox In ALV Program.

- ALV Header With Logo.

- Including Line After Subtotal In ALV Display.

..... Back To Index On ALV List/ Grid Display.

..... Back To MAIN INDEX.



ABAP - Displaying Icon's In ALV Display.

*&---------------------------------------------------------------------*
*& Report ZALV_ICON *
*& *
*&---------------------------------------------------------------------*

REPORT ZALV_ICON .
************************************************************************
* Include Programs
************************************************************************
INCLUDE .

************************************************************************
* Database Tables
************************************************************************
TABLES: kna1. "Customer Master

************************************************************************
* Types
************************************************************************
TYPE-POOLS: kkblo.

************************************************************************
* Structures
************************************************************************
* Structure to hold the Color Information
DATA: BEGIN OF st_color,
color(3) TYPE c,
END OF st_color.

* Structure to hold the Icon Information
DATA: BEGIN OF st_icon,
icon(4) TYPE c,
END OF st_icon.

* ALV Field Catalog Structure
DATA: st_fieldcat TYPE slis_fieldcat_alv.

* ALV Layout Structure
DATA: st_layout TYPE slis_layout_alv.

************************************************************************
* Internal Tables
************************************************************************
* Output Table
DATA: BEGIN OF tbl_kna1 OCCURS 0.
INCLUDE STRUCTURE st_icon. "Icon Structure
INCLUDE STRUCTURE kna1. "Customer Master Structure
INCLUDE STRUCTURE st_color. "Color Structure
DATA: END OF tbl_kna1.

* ALV Field Catalog Table
DATA: tbl_fieldcat TYPE slis_t_fieldcat_alv.

************************************************************************
* Variables
************************************************************************
DATA: fieldname(30) TYPE c,
g_repid LIKE sy-repid.

************************************************************************
* Start of Selection
************************************************************************
START-OF-SELECTION.

g_repid = sy-repid.

PERFORM get_data.

************************************************************************
* End of Selection
************************************************************************
END-OF-SELECTION.

PERFORM do_fancy_stuff.
PERFORM get_layout.
PERFORM get_fieldcat.
PERFORM create_report.


*&---------------------------------------------------------------------*
*& Form CREATE_REPORT
*&---------------------------------------------------------------------*
* Learn to read the subroutine name!
*----------------------------------------------------------------------*
FORM create_report.

CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
EXPORTING
i_interface_check = ' '
i_callback_program = g_repid
i_callback_user_command = 'PROCESS_USER_COMMANDS'
it_fieldcat = tbl_fieldcat
i_default = 'X'
i_save = ' '
is_layout = st_layout
TABLES
t_outtab = tbl_kna1
EXCEPTIONS
program_error = 1
OTHERS = 2.
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. " CREATE_REPORT

*&---------------------------------------------------------------------*
*& Form GET_FIELDCAT
*&---------------------------------------------------------------------*
* Build the Field Catalog
*----------------------------------------------------------------------*
FORM get_fieldcat.
* Here the field catalog is created. To display more fields simply
* 'uncomment' the additional lines and add the field name. Also note
* that the field catalog is much more powerful than this. You can
* intensify fields, change the colour, assign reference fields, etc.
* Look at type slis_fieldcat_alv for more options.
PERFORM write_fieldcat USING 'ICON' 'TBL_KNA1' ' ' 'X' 1 '2' 'X'
' '.
PERFORM write_fieldcat USING 'KUNNR' 'TBL_KNA1' 'KNA1' 'X' 2 ' ' ' '
' '.
PERFORM write_fieldcat USING 'NAME1' 'TBL_KNA1' 'KNA1' ' ' 3 '10' ' '
'X'.
PERFORM write_fieldcat USING 'STRAS' 'TBL_KNA1' 'KNA1' ' ' 4 ' ' ' '
' '.
PERFORM write_fieldcat USING 'TELF1' 'TBL_KNA1' 'KNA1' ' ' 5 ' ' ' '
' '.
PERFORM write_fieldcat USING 'ORT01' 'TBL_KNA1' 'KNA1' ' ' 6 ' ' ' '
' '.
PERFORM write_fieldcat USING 'PSTLZ' 'TBL_KNA1' 'KNA1' ' ' 7 ' ' ' '
' '.
PERFORM write_fieldcat USING 'SORTL' 'TBL_KNA1' 'KNA1' ' ' 8 ' ' ' '
' '.
PERFORM write_fieldcat USING 'ERNAM' 'TBL_KNA1' 'KNA1' ' ' 9 ' ' ' '
' '.
PERFORM write_fieldcat USING 'SPRAS' 'TBL_KNA1' 'KNA1' ' ' 10 ' ' ' '
' '.
* perform write_fieldcat using ' ' 'TBL_KNA1' 'KNA1' ' ' 10 ' '.
* perform write_fieldcat using ' ' 'TBL_KNA1' 'KNA1' ' ' 11 ' '.
* perform write_fieldcat using ' ' 'TBL_KNA1' 'KNA1' ' ' 12 ' '.

ENDFORM. " GET_FIELDCAT

*&---------------------------------------------------------------------*
*& Form WRITE_FIELDCAT
*&---------------------------------------------------------------------*
* Write the Field Catalog data to the Field Catalog Table
*----------------------------------------------------------------------*
* -->name Field name
* -->tab Table name
* -->st Structure Name
* -->key Is this field a Key?
* -->pos Position Number
* -->length Field Length
* -->icon Display as Icon
* -->hot Hotspot
*----------------------------------------------------------------------*
FORM write_fieldcat USING name tab st key pos length icon hot.

st_fieldcat-fieldname = name.
st_fieldcat-tabname = tab.
st_fieldcat-ref_tabname = st.
st_fieldcat-key = key.
st_fieldcat-col_pos = pos.
st_fieldcat-outputlen = length.
st_fieldcat-icon = icon.
st_fieldcat-hotspot = hot.
APPEND st_fieldcat TO tbl_fieldcat.
CLEAR st_fieldcat.

ENDFORM. " WRITE_FIELDCAT

*&---------------------------------------------------------------------*
*& Form PROCESS_USER_COMMANDS
*&---------------------------------------------------------------------*
* Interactive Reporting Commands
*----------------------------------------------------------------------*
FORM process_user_commands USING syst-ucomm LIKE syst-ucomm
selfield TYPE slis_selfield.

* This subroutine is called when there is user interaction in the output
* In this case if the user double clicks the Customer Number then the
* program will call transaction XD03 and display the Customer Master
* Data

CASE syst-ucomm.
WHEN '&IC1'.
* get cursor field fieldname.
READ TABLE tbl_kna1 INDEX selfield-tabindex.
SET PARAMETER ID 'KUN' FIELD tbl_kna1-kunnr.
CALL TRANSACTION 'XD03' AND SKIP FIRST SCREEN.
ENDCASE.

ENDFORM. " PROCESS_USER_COMMANDS

*&---------------------------------------------------------------------*
*& Form GET_LAYOUT
*&---------------------------------------------------------------------*
* set the layout of the ALV.
* add color to the row?
*----------------------------------------------------------------------*
FORM get_layout.

st_layout-info_fieldname = 'COLOR'.
st_layout-colwidth_optimize = 'X'.
st_layout-get_selinfos = 'X'.

ENDFORM. " GET_LAYOUT
*&---------------------------------------------------------------------*
*& Form get_data
*&---------------------------------------------------------------------*
* Get some data to play with
*----------------------------------------------------------------------*
FORM get_data.

SELECT * FROM kna1 INTO CORRESPONDING FIELDS OF TABLE tbl_kna1
UP TO 30 ROWS.

ENDFORM. " get_data
*&---------------------------------------------------------------------*
*& Form do_fancy_stuff
*&---------------------------------------------------------------------*
* Do some fancy pants stuff for example changing the color of
* lines and adding icons
*----------------------------------------------------------------------*
FORM do_fancy_stuff.

* Here we will demonstrate changing the color of ALV Record lines as
* well as displaying Icons
LOOP AT tbl_kna1.
* All records where NAME1 begins with 'M', will be displayed in Bluish
* Green
IF tbl_kna1-name1(1) EQ 'M'.
tbl_kna1-color = 'C41'. "Bluish Green
MODIFY tbl_kna1 TRANSPORTING color.
ENDIF.

* All records with no TELF1 will be displayed in White and have a
* Warning Icon
IF tbl_kna1-telf1 IS INITIAL.
tbl_kna1-color = 'C00'. "White
tbl_kna1-icon = '@AH@'. "Warning Icon
MODIFY tbl_kna1 TRANSPORTING icon color.
ENDIF.
ENDLOOP.

ENDFORM. " do_fancy_stuff



ABAP - Fetch Data From Database Tables Using Select Queries.

REPORT YTABFLDS .
TABLES: DD02L, DD03L, DD04T.

PARAMETERS THETABLE LIKE DD02L-TABNAME OBLIGATORY.

DATA: BEGIN OF MYTABLE,
TABNAME LIKE DD02L-TABNAME,
AS4LOCAL LIKE DD02L-AS4LOCAL,
AS4VERS LIKE DD02L-AS4VERS,
END OF MYTABLE.

SELECT SINGLE TABNAME AS4LOCAL AS4VERS
INTO MYTABLE FROM DD03L
WHERE TABNAME = THETABLE.

WRITE: / MYTABLE-TABNAME, 11 MYTABLE-AS4LOCAL, 13 MYTABLE-AS4VERS.

SELECT * FROM DD03L
WHERE TABNAME = MYTABLE-TABNAME
AND AS4LOCAL = MYTABLE-AS4LOCAL AND AS4VERS = MYTABLE-AS4VERS.

WRITE: / DD03L-FIELDNAME, 11 DD03L-KEYFLAG, 13 DD03L-ROLLNAME,
24 DD03L-CHECKTABLE, 35 DD03L-INTTYPE, 37 DD03L-REFTABLE,
48 DD03L-DATATYPE.

SELECT SINGLE * FROM DD04T
WHERE ROLLNAME = DD03L-ROLLNAME AND
AS4LOCAL = MYTABLE-AS4LOCAL AND AS4VERS = MYTABLE-AS4VERS AND
DDLANGUAGE = 'E'.

WRITE: 53 DD04T-DDTEXT.
ENDSELECT.

SAP ABAP: Improve Performance Using Appropriate Select Queries.

For all entries

The for all entries creates a where clause, where all the entries in the driver table are combined with OR. If the number of
entries in the driver table is larger than rsdb/max_blocking_factor, several similar SQL statements are executed to limit the
length of the WHERE clause.
The plus

Large amount of data
Mixing processing and reading of data
Fast internal reprocessing of data
Fast
The Minus
Difficult to program/understand
Memory could be critical (use FREE or PACKAGE size)
Some steps that might make FOR ALL ENTRIES more efficient:
Removing duplicates from the the driver table
Sorting the driver table
If possible, convert the data in the driver table to ranges so a BETWEEN statement is used instead of and OR statement:
FOR ALL ENTRIES IN i_tab
WHERE mykey >= i_tab-low and
mykey <= i_tab-high. Nested selects

The plus:
Small amount of data
Mixing processing and reading of data
Easy to code - and understand
The minus:
Large amount of data
when mixed processing isn’t needed
Performance killer no. 1

Select using JOINS

The plus
Very large amount of data
Similar to Nested selects - when the accesses are planned by the programmer
In some cases the fastest
Not so memory critical
The minus
Very difficult to program/understand
Mixing processing and reading of data not possible

Use the selection criteria

SELECT * FROM SBOOK.
CHECK: SBOOK-CARRID = 'LH' AND
SBOOK-CONNID = '0400'.
ENDSELECT.
SELECT * FROM SBOOK
WHERE CARRID = 'LH' AND
CONNID = '0400'.
ENDSELECT.

Use the aggregated functions

C4A = '000'.
SELECT * FROM T100
WHERE SPRSL = 'D' AND
ARBGB = '00'.
CHECK: T100-MSGNR > C4A.
C4A = T100-MSGNR.
ENDSELECT.

SELECT MAX( MSGNR ) FROM T100 INTO C4A
WHERE SPRSL = 'D' AND
ARBGB = '00'.

Select with view

SELECT * FROM DD01L
WHERE DOMNAME LIKE 'CHAR%'
AND AS4LOCAL = 'A'.
SELECT SINGLE * FROM DD01T
WHERE DOMNAME = DD01L-DOMNAME
AND AS4LOCAL = 'A'
AND AS4VERS = DD01L-AS4VERS
AND DDLANGUAGE = SY-LANGU.
ENDSELECT.

SELECT * FROM DD01V
WHERE DOMNAME LIKE 'CHAR%'
AND DDLANGUAGE = SY-LANGU.
ENDSELECT.

Select with index support

SELECT * FROM T100
WHERE ARBGB = '00'
AND MSGNR = '999'.
ENDSELECT.

SELECT * FROM T002.
SELECT * FROM T100
WHERE SPRSL = T002-SPRAS
AND ARBGB = '00'
AND MSGNR = '999'.
ENDSELECT.
ENDSELECT.


Select … Into table

REFRESH X006.
SELECT * FROM T006 INTO X006.
APPEND X006.
ENDSELECT

SELECT * FROM T006 INTO TABLE X006.


Select with selection list

SELECT * FROM DD01L
WHERE DOMNAME LIKE 'CHAR%'
AND AS4LOCAL = 'A'.
ENDSELECT

SELECT DOMNAME FROM DD01L
INTO DD01L-DOMNAME
WHERE DOMNAME LIKE 'CHAR%'
AND AS4LOCAL = 'A'.
ENDSELECT

Key access to multiple lines

LOOP AT TAB.
CHECK TAB-K = KVAL.
" ...
ENDLOOP.

LOOP AT TAB WHERE K = KVAL.
" ...
ENDLOOP.


Copying internal tables

REFRESH TAB_DEST.
LOOP AT TAB_SRC INTO TAB_DEST.
APPEND TAB_DEST.
ENDLOOP.

TAB_DEST[] = TAB_SRC[].

Modifying a set of lines

LOOP AT TAB.
IF TAB-FLAG IS INITIAL.
TAB-FLAG = 'X'.
ENDIF.
MODIFY TAB.
ENDLOOP.

TAB-FLAG = 'X'.
MODIFY TAB TRANSPORTING FLAG
WHERE FLAG IS INITIAL.

Deleting a sequence of lines

DO 101 TIMES.
DELETE TAB_DEST INDEX 450.
ENDDO.

DELETE TAB_DEST FROM 450 TO 550.


Linear search vs. binary

READ TABLE TAB WITH KEY K = 'X'.

READ TABLE TAB WITH KEY K = 'X' BINARY SEARCH.
Comparison of internal tables
DESCRIBE TABLE: TAB1 LINES L1,
TAB2 LINES L2.

IF L1 <> L2.
TAB_DIFFERENT = 'X'.
ELSE.
TAB_DIFFERENT = SPACE.
LOOP AT TAB1.
READ TABLE TAB2 INDEX SY-TABIX.
IF TAB1 <> TAB2.
TAB_DIFFERENT = 'X'. EXIT.
ENDIF.
ENDLOOP.
ENDIF.

IF TAB_DIFFERENT = SPACE.
" ...
ENDIF.

IF TAB1[] = TAB2[].
" ...
ENDIF.

Modify selected components

LOOP AT TAB.
TAB-DATE = SY-DATUM.
MODIFY TAB.
ENDLOOP.

WA-DATE = SY-DATUM.
LOOP AT TAB.
MODIFY TAB FROM WA TRANSPORTING DATE.
ENDLOOP.

Appending two internal tables

LOOP AT TAB_SRC.
APPEND TAB_SRC TO TAB_DEST.
ENDLOOP

APPEND LINES OF TAB_SRC TO TAB_DEST.

Deleting a set of lines

LOOP AT TAB_DEST WHERE K = KVAL.
DELETE TAB_DEST.
ENDLOOP

DELETE TAB_DEST WHERE K = KVAL.


Tools available in SAP to pin-point a performance problem

The runtime analysis (SE30)
SQL Trace (ST05)
Tips and Tricks tool
The performance database

Optimizing the load of the database

Using table buffering
Using buffered tables improves the performance considerably. Note that in some cases a stament can not be used with a buffered table, so when using these staments the buffer will be bypassed. These staments are:
Select DISTINCT
ORDER BY / GROUP BY / HAVING clause
Any WHERE clasuse that contains a subquery or IS NULL expression
JOIN s
A SELECT... FOR UPDATE
If you wnat to explicitly bypass the bufer, use the BYPASS BUFFER addition to the SELECT clause.

Use the ABAP SORT Clause Instead of ORDER BY
The ORDER BY clause is executed on the database server while the ABAP SORT statement is executed on the application server. The datbase server will usually be the bottleneck, so sometimes it is better to move thje sort from the datsbase server to the application server.
If you are not sorting by the primary key ( E.g. using the ORDER BY PRIMARY key statement) but are sorting by another key, it could be better to use the ABAP SORT stament to sort the data in an internal table. Note however that for very large result sets it might not be a feasible solution and you would want to let the datbase server sort it.

Avoid ther SELECT DISTINCT Statement

As with the ORDER BY clause it could be better to avoid using SELECT DISTINCT, if some of the fields are not part of an index. Instead use ABAP SORT + DELETE ADJACENT DUPLICATES on an internal table, to delete duplciate rows.



RETURN TO MAIN INDEX:

- Sample Programs On Uploading & Downloading Files.

- Sample Programs On HR ABAP.

- Sample Report Programs On ALV List/ Grid Display.

- Sample Programs On Selection Screen.

- Sample Programs On BDC.

.....Back To MAIN INDEX.

SAP ABAP:Sample Select Query Using Left Outerjoin.

report ztest.
DATA: CUSTOMER TYPE SCUSTOM,
BOOKING TYPE SBOOK.

SELECT SCUSTOM~NAME
SCUSTOM~POSTCODE
SCUSTOM~CITY
SBOOK~FLDATE
SBOOK~CARRID
SBOOK~CONNID
SBOOK~BOOKID

INTO (CUSTOMER-NAME, CUSTOMER-POSTCODE, CUSTOMER-CITY,
BOOKING-FLDATE, BOOKING-CARRID, BOOKING-CONNID,
BOOKING-BOOKID)

FROM SCUSTOM LEFT OUTER JOIN SBOOK
ON SCUSTOM~ID = SBOOK~CUSTOMID AND
SBOOK~FLDATE = '20081015'

ORDER BY SCUSTOM~NAME SBOOK~FLDATE.

WRITE: / CUSTOMER-NAME, CUSTOMER-POSTCODE, CUSTOMER-CITY,
BOOKING-FLDATE, BOOKING-CARRID, BOOKING-CONNID,
BOOKING-BOOKID.

ENDSELECT.



RETURN TO MAIN INDEX:

- Sample Programs On Uploading & Downloading Files.

- Sample Programs On HR ABAP.

- Sample Report Programs On ALV List/ Grid Display.

- Sample Programs On Selection Screen.

- Sample Programs On BDC.

.....Back To MAIN INDEX.

SAP ABAP -Retrieve Details Regarding Any Function Module.

PROGRAM yclip3jnc.

TYPE-POOLS : slis.

PARAMETERS: p_func LIKE fupararef-funcname. " Name of Function Module

DATA : BEGIN OF i_tab OCCURS 0,
funcname LIKE fupararef-funcname, " Name of Function Module
paramtype LIKE fupararef-paramtype, " Parameter type
pposition LIKE fupararef-pposition, " Internal Table, Current Line Index
optional LIKE fupararef-optional, " Optional parameters
parameter LIKE fupararef-parameter, " Parameter name
defaultval LIKE fupararef-defaultval, " Default value for import parameter
structure LIKE fupararef-structure, " Associated Type of an Interface Parameter
stext LIKE funct-stext, " Short text
END OF i_tab.

DATA: BEGIN OF mtab_new_prog OCCURS 0,
line(172) TYPE c,
END OF mtab_new_prog.

DATA: funcdesc LIKE tftit-stext, " Short text for function module
mylen TYPE i,
myrc TYPE i.

CONSTANTS: myhats(40) VALUE '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^'.

TRANSLATE p_func TO UPPER CASE.

SELECT SINGLE
tftit~stext " Short text for function module
INTO funcdesc
FROM tftit
WHERE tftit~funcname = p_func
AND tftit~spras = sy-langu.

TRANSLATE p_func TO LOWER CASE.

CONCATENATE `CALL FUNCTION ` p_func ` " ` funcdesc INTO mtab_new_prog-line.
APPEND mtab_new_prog.

TRANSLATE p_func TO UPPER CASE.

SELECT
fupararef~funcname " Name of Function Module
fupararef~paramtype " Parameter type
fupararef~pposition " Internal Table, Current Line Index
fupararef~optional " Optional parameters
fupararef~parameter " Parameter name
fupararef~defaultval " Default value for import parameter
fupararef~structure " Associated Type of an Interface Parameter
funct~stext " Short text
INTO TABLE i_tab
FROM fupararef
INNER JOIN funct
ON fupararef~funcname = funct~funcname
AND fupararef~parameter = funct~parameter
AND funct~spras = sy-langu
WHERE fupararef~funcname = p_func
AND fupararef~r3state = 'A'
ORDER BY fupararef~paramtype
fupararef~pposition.


LOOP AT i_tab.
AT NEW paramtype.
CASE i_tab-paramtype.
WHEN 'C'.
MOVE ' CHANGING' TO mtab_new_prog-line.
WHEN 'E'.
MOVE ' IMPORTING' TO mtab_new_prog-line.
WHEN 'I'.
MOVE ' EXPORTING' TO mtab_new_prog-line.
WHEN 'T'.
MOVE ' TABLES' TO mtab_new_prog-line.
WHEN 'X'.
MOVE ' EXCEPTIONS' TO mtab_new_prog-line.
ENDCASE.
APPEND mtab_new_prog.
ENDAT.

IF i_tab-optional = 'X'.
mtab_new_prog-line = `*^^^`.
ELSE.
mtab_new_prog-line = `^^^^`.
ENDIF.

IF i_tab-paramtype = 'X'.
MOVE i_tab-pposition TO i_tab-defaultval.
CONDENSE i_tab-defaultval.
ELSE.
TRANSLATE i_tab-parameter TO LOWER CASE.
ENDIF.

CONCATENATE mtab_new_prog-line i_tab-parameter '^=^' INTO mtab_new_prog-line.

IF i_tab-defaultval IS NOT INITIAL.
CONCATENATE mtab_new_prog-line i_tab-defaultval INTO mtab_new_prog-line.
ENDIF.

mylen = STRLEN( mtab_new_prog-line ).

IF mylen <>COMPUTE mylen = 31 - mylen.

ELSE.
MOVE 1 TO mylen.

ENDIF.

TRANSLATE i_tab-structure TO LOWER CASE.
CONCATENATE mtab_new_prog-line myhats+0(mylen) ` " ` i_tab-structure INTO mtab_new_prog-line.

mylen = STRLEN( mtab_new_prog-line ).
IF mylen <>
COMPUTE mylen = 47 - mylen.
ELSE.
MOVE 1 TO mylen.
ENDIF.

CONCATENATE mtab_new_prog-line myhats+0(mylen) ` ` i_tab-stext INTO mtab_new_prog-line.
APPEND mtab_new_prog.

ENDLOOP.

" LOOP AT I_TAB

CONCATENATE ` . " ` p_func INTO mtab_new_prog-line.
APPEND mtab_new_prog.

LOOP AT mtab_new_prog.
TRANSLATE mtab_new_prog-line USING `^ `.
MODIFY mtab_new_prog.
IF mtab_new_prog = space.
SKIP 1.
ENDIF.

WRITE: / mtab_new_prog.

ENDLOOP.

" LOOP AT MTAB_NEW_PROG

* Write the beautiful program code to ClipBoard from internal table

CALL METHOD cl_gui_frontend_services=>clipboard_export
IMPORTING
data = mtab_new_prog[]
CHANGING
rc = myrc.



RETURN TO MAIN INDEX:

- Sample Programs On Uploading & Downloading Files.

- Sample Programs On HR ABAP.

- Sample Report Programs On ALV List/ Grid Display.

- Sample Programs On Selection Screen.

- Sample Programs On BDC.

.....Back To MAIN INDEX.

SAP ABAP: Report Program To Transfer (Tetxs, Code, etc.) Onto Clipboard.

REPORT yclipjnc. .

TABLES:
e071 , " Change & Transport System: Object Entries of Requests/Tasks
tadir , " Directory of Repository Objects
trdir , " Generated Table for View TRDIR
dd02t . " R/3 DD: SAP table texts

DATA: BEGIN OF mtab_old_prog OCCURS 0,
line(172) TYPE c,
END OF mtab_old_prog.

DATA: BEGIN OF mtab_new_prog OCCURS 0,
line(172) TYPE c,
END OF mtab_new_prog.

DATA: BEGIN OF mtab_jnc_prog OCCURS 0,
line(172) TYPE c,
END OF mtab_jnc_prog.

DATA:
* Hold an entire statement, even if it spans multiple lines
BEGIN OF mtab_long_line OCCURS 0,
start TYPE i,
end TYPE i,
code(9999) TYPE c, "For type "C", a maximum length specification of 65535 is allowed.
END OF mtab_long_line.

DATA: BEGIN OF mtab_tabname OCCURS 0,
tabname LIKE dd02t-tabname, " Table name
tabdesc LIKE dd02t-ddtext, " Short text describing ABAP/4
*Dictio
END OF mtab_tabname.

* Queue to hold list of internal table names for commenting the ENDLOOP
* line
DATA: BEGIN OF mtab_itab_names OCCURS 0,
tabname(40) TYPE c,
END OF mtab_itab_names.

* Queue to hold list of table names for commenting the ENDSELECT line
DATA: BEGIN OF mtab_tab_names OCCURS 0,
tabname(40) TYPE c,
END OF mtab_tab_names.

DATA: BEGIN OF mtab_form_names OCCURS 0,
tabname(40) TYPE c,
END OF mtab_form_names.

DATA: mylength TYPE i,
myrc TYPE i.

CONSTANTS: myhats(40) VALUE '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^'.

* START of EXECUTION

* Read the program code in ClipBoard into an internal table
CALL METHOD cl_gui_frontend_services=>clipboard_import
IMPORTING
data = mtab_old_prog[]
length = mylength.

IF sy-subrc NE 0.
WRITE: / `Unable to read ClipBoard`.
WRITE: / `Exiting program`.
ENDIF.

PERFORM create_condensed_table TABLES mtab_old_prog mtab_long_line.

PERFORM format_program.

CALL FUNCTION `PRETTY_PRINTER`
EXPORTING
inctoo = space
TABLES
ntext = mtab_jnc_prog
otext = mtab_new_prog
EXCEPTIONS
enqueue_table_full = 1
include_enqueued = 2
include_readerror = 3
include_writeerror = 4
OTHERS = 5.

* Write the beautiful program code to ClipBoard from internal table
CALL METHOD cl_gui_frontend_services=>clipboard_export
IMPORTING
data = mtab_jnc_prog[]
CHANGING
rc = myrc.

LOOP AT mtab_jnc_prog.
IF mtab_jnc_prog = space.
SKIP 1.
ENDIF.
WRITE: / mtab_jnc_prog.
ENDLOOP. " LOOP AT MTAB_JNC_PROG


*---------------------------------------------------------------------*
* FORM CREATE_CONDENSED_TABLE *
*---------------------------------------------------------------------*
* Create a table that has all statements condensed onto 1 line *
*---------------------------------------------------------------------*
FORM create_condensed_table
TABLES ftab_old_prog STRUCTURE mtab_old_prog
ftab_long_line STRUCTURE mtab_long_line.

DATA:
* Structure to hold program code/comment
BEGIN OF fstr_line,
code(172) TYPE c, " Program Code
comment(172) TYPE c, " Inline comments
END OF fstr_line.

LOOP AT ftab_old_prog.

IF ftab_long_line-start = 0.
ftab_long_line-start = ftab_long_line-end + 1.
CLEAR ftab_long_line-end.
ENDIF.

* Strip off any inline comments so they do not get in the way
* If comments are not separated, then words in the comments could
* look like keywords, and cause problems
SPLIT ftab_old_prog-line AT `"` INTO fstr_line-code fstr_line-comment.

* Align all statements to be left justified
SHIFT fstr_line-code LEFT DELETING LEADING space.

* Put all lines that make up a single statement into one field
* This will make it easier to isolate key words. For example, if you
* want to process a TABLES statement, but exclude the TABLES part of a
* function call, or a subroutine call.
CONCATENATE ftab_long_line-code fstr_line-code
INTO ftab_long_line-code SEPARATED BY space.

IF fstr_line-code CA `.` OR " Period means end of statement
fstr_line-code(1) = `*` OR " Comment Line
fstr_line-code CO space. " Blank Line
* Keep track of the table index that the statement ends on
ftab_long_line-end = sy-tabix.
* Remove delimiter from concatenation of fields
SHIFT ftab_long_line-code LEFT BY 1 PLACES.

APPEND ftab_long_line.

CLEAR: ftab_long_line-code, ftab_long_line-start.

* Don`t clear out fstr_long_line-end yet. It is used to calc
* fstr_long_line-start.
ENDIF.

ENDLOOP. " LOOP AT FTAB_OLD_PROG
ENDFORM. " FORM CREATE_CONDENSED_TABLE

*---------------------------------------------------------------------*
* FORM FORMAT_PROGRAM *
*---------------------------------------------------------------------*
FORM format_program.

DATA: lstr_old_prog LIKE LINE OF mtab_old_prog.

LOOP AT mtab_long_line.

TRANSLATE mtab_long_line-code TO UPPER CASE.

IF mtab_long_line-code(1) EQ `*`. " Do not modify Comment Lines

LOOP AT mtab_old_prog FROM mtab_long_line-start
TO mtab_long_line-end.

mtab_new_prog-line = mtab_old_prog-line.

APPEND mtab_new_prog.

ENDLOOP. " LOOP AT MTAB_OLD_PROG

ELSEIF mtab_long_line-code(6) EQ `TABLES`.

* Reformat any TABLES statements. Will only reformat when TABLES
* is at the start of the statement. Will not try to get table
* descriptions for CALL FUNCTIONS or FORM/PERFORMs

* Get the table names from mstr_long_line.
PERFORM get_table_names_from_statement TABLES mtab_tabname
USING mtab_long_line-code.
* Find the descriptions for each table
PERFORM get_table_descriptions TABLES mtab_tabname.

* create the new statement
PERFORM build_new_tables_statement USING mtab_long_line.

ELSE. " All other modifications to the code handled here

LOOP AT mtab_old_prog FROM mtab_long_line-start
TO mtab_long_line-end.

* Remove extra spaces from line for comparisons
lstr_old_prog-line = mtab_old_prog-line.
CONDENSE lstr_old_prog-line.
TRANSLATE lstr_old_prog-line TO UPPER CASE.

IF lstr_old_prog-line CS `"`. " Comments
mtab_new_prog-line = mtab_old_prog-line.
ELSE.
IF lstr_old_prog-line CS ` LIKE ` OR
lstr_old_prog-line CS ` TYPE ` OR
lstr_old_prog-line CS ` FOR ` OR
lstr_old_prog-line CS `~`. "jnc OpenSQL table~column


PERFORM get_for_like_comment USING mtab_old_prog
CHANGING mtab_new_prog.

ELSEIF lstr_old_prog-line(8) = 'LOOP AT'.

* save table name into a queue
PERFORM enqueue_itab_name USING mtab_long_line-code.
mtab_new_prog-line = mtab_old_prog-line.

ELSEIF lstr_old_prog-line(7) = `ENDLOOP`.

* get name off of queue and add it as a comment to the ENDLOOP line
PERFORM add_comment_to_endloop USING mtab_old_prog-line
CHANGING mtab_new_prog-line.

ELSEIF lstr_old_prog-line(7) EQ 'SELECT' AND
lstr_old_prog-line(13) NE 'SELECT SINGLE'.

* save table name into a queue
PERFORM enqueue_tab_name USING mtab_old_prog-line.
mtab_new_prog-line = mtab_old_prog-line.
ELSEIF lstr_old_prog-line(9) = `ENDSELECT`.

* get name off of queue and add it as a comment to the ENDSELECT
PERFORM add_comment_to_select USING mtab_old_prog-line
CHANGING mtab_new_prog-line.

ELSEIF lstr_old_prog-line(5) = 'FORM'.

* save form name into a queue
PERFORM enqueue_form_name USING mtab_old_prog-line.
mtab_new_prog-line = mtab_old_prog-line.

ELSEIF lstr_old_prog-line(7) = `ENDFORM`.

* get name off of queue and add it as a comment to the ENDFORM
PERFORM add_comment_to_endform USING mtab_old_prog-line
CHANGING mtab_new_prog-line.
ELSE. " Any other lines
mtab_new_prog-line = mtab_old_prog-line.
ENDIF.
ENDIF.

APPEND mtab_new_prog.

ENDLOOP. " LOOP AT MTAB_OLD_PROG
ENDIF.
ENDLOOP. " LOOP AT MTAB_LONG_LINE

ENDFORM. " FORM FORMAT_PROGRAM


*---------------------------------------------------------------------*
* FORM GET_TABLE_NAMES_FROM_STATEMENT *
*---------------------------------------------------------------------*
FORM get_table_names_from_statement TABLES ftab_tabname
STRUCTURE mtab_tabname
USING fc_statement.

CLEAR ftab_tabname.
REFRESH ftab_tabname.

REPLACE `TABLES` WITH space INTO fc_statement.
TRANSLATE fc_statement USING `. `. " Replace periods
TRANSLATE fc_statement USING `, `. " Replace commas
TRANSLATE fc_statement USING `: `. " Replace colons

CONDENSE fc_statement. " Remove all extra spaces

SPLIT fc_statement AT space INTO TABLE ftab_tabname.

ENDFORM. " FORM GET_TABLE_NAMES_FROM_STATEMENT


*---------------------------------------------------------------------*
* FORM GET_TABLE_DESCRIPTIONS *
*---------------------------------------------------------------------*
FORM get_table_descriptions TABLES ftab_tabname STRUCTURE mtab_tabname.

LOOP AT ftab_tabname.
SELECT SINGLE * FROM dd02t
WHERE tabname = ftab_tabname-tabname
AND ddlanguage = sy-langu.

IF sy-subrc = 0.
ftab_tabname-tabdesc = dd02t-ddtext.
MODIFY ftab_tabname.
ENDIF.

ENDLOOP. " LOOP AT FTAB_TABNAME
ENDFORM. " FORM GET_TABLE_DESCRIPTIONS


*---------------------------------------------------------------------*
* FORM BUILD_NEW_TABLES_STATEMENT *
*---------------------------------------------------------------------*
FORM build_new_tables_statement USING fstr_long_line LIKE
mtab_long_line.

DATA: lc_sep(1) TYPE c,
li_rows TYPE i,
wordlen TYPE i.

DESCRIBE TABLE mtab_tabname LINES li_rows.

mtab_new_prog-line = `TABLES:`.
APPEND mtab_new_prog.

LOOP AT mtab_tabname.
IF sy-tabix = li_rows.
lc_sep = `.`.
ELSE.
lc_sep = `,`.
ENDIF.

wordlen = STRLEN( mtab_tabname-tabname ).

IF wordlen < wordlen =" 12">
wordlen =" 1."
line =" f_old_prog."
f_new_prog =" f_old_prog."
f_new_prog =" lstr_old_prog-line."

tabname =" `SY`."
tabname =" `SYST`."
tabname =" lstr_field-tabname"
nametab =" ltab_nametab"
internal_error =" 1"
table_has_no_fields =" 2"
table_not_activ =" 3"
no_texts_found =" 4"
others =" 5."> 0.
* MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
* WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

READ TABLE ltab_nametab
WITH KEY tabname = lstr_field-tabname
fieldname = lstr_field-fldname.

IF sy-subrc = 0.

wordlen = STRLEN( lstr_old_prog-line ).

IF wordlen <> wordlen = 45 - wordlen.
ELSE.
wordlen = 1.
ENDIF.

CONCATENATE lstr_old_prog-line myhats+0(wordlen) ` " ` ltab_nametab-fieldtext
INTO f_new_prog.
TRANSLATE mtab_new_prog USING `^ `.
ELSE.
f_new_prog = lstr_old_prog-line.
ENDIF.
ENDFORM.

" FORM GET_TABLE_DESCRIPTIONS *---------------------------------------------------------------------* * FORM ENQUEUE_ITAB_NAME * *---------------------------------------------------------------------*

FORM enqueue_itab_name USING value(f_line) LIKE mtab_long_line-code.
DATA: lc_dummy(1) TYPE c, lc_itab(40) TYPE c.
TRANSLATE f_line TO UPPER CASE.
SPLIT f_line AT `LOOP AT ` INTO lc_dummy lc_itab.
SPLIT lc_itab AT space INTO lc_itab lc_dummy.
TRANSLATE lc_itab USING `. `.
CONDENSE lc_itab.
mtab_itab_names = lc_itab.
* Always have the most recent LOOP AT table as the first entry in the
* queue

INSERT mtab_itab_names INDEX 1.
ENDFORM.

" FORM GET_TABLE_DESCRIPTIONS *---------------------------------------------------------------------* * FORM ADD_COMMENT_TO_ENDLOOP * *---------------------------------------------------------------------*

FORM add_comment_to_endloop
USING fstr_long_line LIKE mtab_old_prog-line
CHANGING f_prog_line LIKE mtab_new_prog-line .

IF mtab_old_prog-line NA `"`. " No comments

* Get the internal table from the queue
READ TABLE mtab_itab_names INDEX 1.
CONCATENATE mtab_old_prog-line ` "` `LOOP AT` mtab_itab_names-tabname INTO f_prog_line SEPARATED BY space.
* Dequeue the itab name
DELETE mtab_itab_names INDEX 1.
ELSE.
f_prog_line = mtab_old_prog-line.
ENDIF.
ENDFORM.

" FORM GET_TABLE_DESCRIPTIONS *---------------------------------------------------------------------* * FORM ENQUEUE_TAB_NAME * *---------------------------------------------------------------------*

FORM enqueue_tab_name USING f_line LIKE mtab_old_prog-line.
DATA: lc_dummy(1) TYPE c, lc_tab(40) TYPE c.
TRANSLATE f_line TO UPPER CASE.
SPLIT f_line AT ` FROM ` INTO lc_dummy lc_tab.
CONDENSE lc_tab.
" Remove leading/trailing extra spaces

SPLIT lc_tab AT space INTO lc_tab lc_dummy. TRANSLATE lc_tab USING `. `.

CONDENSE lc_tab. mtab_tab_names = lc_tab.

* Always have the most recent LOOP AT table as the first entry in the
* queue

INSERT mtab_tab_names INDEX 1.
ENDFORM.

" FORM GET_TABLE_DESCRIPTIONS *---------------------------------------------------------------------* * FORM ADD_COMMENT_TO_SELECT * *---------------------------------------------------------------------*

FORM add_comment_to_select USING fstr_long_line
LIKE mtab_old_prog-line
CHANGING f_prog_line.

IF mtab_old_prog-line NA `"`. " No comments

* Get the table from the queue
READ TABLE mtab_tab_names INDEX 1.
CONCATENATE mtab_old_prog-line ` "` `SELECT FROM` mtab_tab_names-tabname INTO f_prog_line SEPARATED BY space.

* Dequeue the tab name

DELETE mtab_tab_names INDEX 1.
ELSE.
f_prog_line = mtab_old_prog-line.
ENDIF.
ENDFORM.

" FORM GET_TABLE_DESCRIPTIONS *---------------------------------------------------------------------* * FORM ADD_COMMENT_TO_ENDFORM * *---------------------------------------------------------------------*

FORM add_comment_to_endform USING fstr_long_line
LIKE mtab_old_prog-line CHANGING f_prog_line.

IF mtab_old_prog-line NA `"`. " No comments

* Get the table from the queue

READ TABLE mtab_form_names INDEX 1.
CONCATENATE mtab_old_prog-line ` "` `FORM` mtab_form_names-tabname
INTO f_prog_line SEPARATED BY space.

* Dequeue the form name
DELETE mtab_form_names INDEX 1.
ELSE.
f_prog_line = mtab_old_prog-line.
ENDIF.
ENDFORM.

" FORM GET_TABLE_DESCRIPTIONS *---------------------------------------------------------------------* * FORM ENQUEUE_FORM_NAME * *---------------------------------------------------------------------*

FORM enqueue_form_name USING f_line.

DATA: lc_dummy(1) TYPE c, lc_tab(40) TYPE c.
TRANSLATE f_line TO UPPER CASE.
SPLIT f_line AT `FORM ` INTO lc_dummy lc_tab.
CONDENSE lc_tab. " Remove leading/trailing extra spaces SPLIT lc_tab AT space
INTO lc_tab lc_dummy.
TRANSLATE lc_tab USING `. `.
CONDENSE lc_tab. mtab_form_names = lc_tab.
* Always have the most recent LOOP AT table as the first entry in the
* queue
INSERT mtab_form_names INDEX 1.
ENDFORM.