Sie sind auf Seite 1von 10

SAP Query

Using ABAP

Submitted By
Kiran Subramaniam S
III Year BE/CSE
AC07UCS033
SAP Query using ABAP

Building an SAP Query using ABAP Code


There are 4 different ways to write SAP queries – depending on the approach of data
retrieval strategy:
1. Using table join
2. Directly reading from tables
3. Using logical databases (LDB) e.g. PNP
4. Using some programs

nd
Here in this document, we’ll demonstrate building SAP query using 2 method (Direct
read of table).
1. Create InfoSet by using t-code SQ02:

2. Press ‘Create’ button.

2
SAP Query using ABAP

Press enter.

Press enter.
3. Press ‘Code’.

3
SAP Query using ABAP

3. Select the ‘DATA’ coding section.

4. Write the following code in this section.


TYPE-POOLS: slis.
TABLES: scarr.
* Type for taking data from tables SFLIGHT and SCARR
TYPES: BEGIN OF ty_flight,
carrid TYPE s_carr_id,
connid TYPE s_conn_id,
fldate TYPE s_date,
seatsmax TYPE s_seatsmax,
seatsocc TYPE s_seatsocc,
carrname TYPE s_carrname,
currcode TYPE s_currcode,
END OF ty_flight.
* Internal table and work area declaration
DATA: wa_flight TYPE ty_flight,
i_flight TYPE STANDARD TABLE OF ty_flight INITIAL SIZE 0,
i_fldcat TYPE slis_t_fieldcat_alv,
wa_fldcat TYPE slis_fieldcat_alv.

4
SAP Query using ABAP

5. Now go to START-OF-SELECTION section

5
SAP Query using ABAP

And write the following code in START-OF-SELECTION section to display a few field
data in ALV grid display report format.

* Retrieve data from tables SFLIGHT and SCARR


SELECT m1~carrid
m1~connid
m1~fldate
m1~seatsmax
m1~seatsocc
m2~carrname
m2~currcode
INTO TABLE i_flight
FROM sflight AS m1 INNER JOIN scarr AS m2
ON m1~carrid = m2~carrid.
CLEAR: wa_flight.
* Populate the field catalogs
wa_fldcat-col_pos = 1.
wa_fldcat-fieldname = 'CARRID'.
wa_fldcat-seltext_l = 'Airline carrier ID'.
APPEND wa_fldcat TO i_fldcat.
CLEAR: wa_fldcat.
wa_fldcat-col_pos = 2.
wa_fldcat-fieldname = 'CONNID'.
wa_fldcat-seltext_l = 'Flight connection Id'.
APPEND wa_fldcat TO i_fldcat.
CLEAR: wa_fldcat.
wa_fldcat-col_pos = 3.
wa_fldcat-fieldname = 'FLDATE'.
wa_fldcat-seltext_l = 'Flight date'.
APPEND wa_fldcat TO i_fldcat.
CLEAR: wa_fldcat.
wa_fldcat-col_pos = 4.
wa_fldcat-fieldname = 'SEATSMAX'.
wa_fldcat-seltext_l = 'Maximum capacity'.
APPEND wa_fldcat TO i_fldcat.
CLEAR: wa_fldcat.
wa_fldcat-col_pos = 5.
wa_fldcat-fieldname = 'SEATSOCC'.
wa_fldcat-seltext_l = 'Occupied seats'.
APPEND wa_fldcat TO i_fldcat.
CLEAR: wa_fldcat.
wa_fldcat-col_pos = 6.
wa_fldcat-fieldname = 'CARRNAME'.
wa_fldcat-seltext_l = 'Airline name'.
APPEND wa_fldcat TO i_fldcat.
CLEAR: wa_fldcat.
wa_fldcat-col_pos = 7.
wa_fldcat-fieldname = 'CURRCODE'.
wa_fldcat-seltext_l = 'Local currency of airline'.
APPEND wa_fldcat TO i_fldcat.
CLEAR: wa_fldcat.
* ALV grid display report
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
it_fieldcat = i_fldcat
TABLES
t_outtab = i_flight
EXCEPTIONS
program_error = 1

6
SAP Query using ABAP

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.
EXIT.

6. Generate the InfoSet.


7. Now create a user group.

8. Now assign the user group ZUGRP_BEJ to the InfoSet ZINFOSET_BEJ. I’ve assigned
two users in my user group.

7
SAP Query using ABAP

Save it.
9. Now create the query.

8
SAP Query using ABAP

10. Now execute the query from SQ01.

The following selection screen will appear where we need to put the flight
date.

Enter the flight date as 11.06.2008 and execute the query; we’ll get the
following ALV grid display report.

9
SAP Query using ABAP

10

Das könnte Ihnen auch gefallen