SAP ABAP Internal Tables

August 23, 2017 | Author: SAP JOBS Forum | Category: Control Flow, Subroutine, Sql, Parameter (Computer Programming), Data Type
Share Embed Donate


Short Description

DATA DECLARATION • pre-defined data types: • declaration keywords: • control keywords • logical statements • looping sta...

Description

Kavitha .A.S

DAY – CONTENTS PROGRAMMING TECHNIQUES

¾ INTRODUCTION ¾ DATA DECLARATION • • • • • • • • • • •

pre-defined data types: declaration keywords: control keywords logical statements looping statements write- the output statement data parameters tables types internal table o internal table manipulations: o internal table without header line: o internal table with header line with manipulations

¾ SQL STATEMENTS ¾ OTHER ABAP KEYWORDS ¾ QUESTIONS IN PROGRAMMING TECHNIQUES.

Kavitha .A.S

INTRODUCTION The ABAP Editor creates, tests and changes ABAP programs, function modules, screen flow logic and logical databases. The ABAP Editor provides functions to support program development, as well as normal text operations such as insert, find, and replace. You can use the ABAP Editor in the following modes: • • • •

Editor control mode PC mode with line numbering PC mode without line numbering Command mode

To open the ABAP EDITOR, U can either browse the screen as below or directly enter SE38.

Kavitha .A.S Enter the program name ZKA_SAMPLE_PGM and click on the CREATE button.

Title: SAMPLE PROGRAM. Type: EXECUTABLE PROGRAM Status: TEST PROGRAM. And click on SAVE.

Kavitha .A.S The screen below is the one where u can enter the code to be executed.

DATA DECLARATION: There are 2 kind of Data types. They are: 1. Pre-defined. 2. User defined. pre-defined data types: 1. 2. 3. 4. 5. 6.

Character ( C ) Numeric ( N ) Integer ( I ) Float ( F ) Date (DATS) Time ( TIMS).

Declaration keywords: 1. 2. 3. 4.

Data Parameters Tables Types

Kavitha .A.S Control keywords 1. Check 2. Exit Logical Statements 1. if-elseif-else-endif. 2. While-Endwhile 3. Do-Enddo. Looping statements 1. Loop-Endloop 2. Select-Endselect. WRITE- THE OUTPUT STATEMENT WRITE 'SAP'. WRITE:/ 'SAP','ABAP'. WRITE:/5 'SAP', /5 'ABAP'. WRITE:/5 'SAP'. WRITE:/5 'ABAP'.

Kavitha .A.S DATA Syntax: DATA: ( length ) TYPE < I,N,C,D > . (OR ) DATA: ( length ) TYPE < I,N,C,D > VALUE ‘’.

Eg: 1.

DATA: A TYPE I VALUE '123'. WRITE:/ A.

2.

DATA: B(3) TYPE N VALUE '123'. WRITE:/ B.

3.

DATA: C(5) TYPE C VALUE 'KAVI'. WRITE:/ C.

4.

DATA: D(5) TYPE P VALUE '123.45'. WRITE:/ D.

5.

DATA: E TYPE DATS VALUE '20060214'. WRITE:/ E.

Kavitha .A.S PARAMETERS Syntax: PARAMETERS: ( length ) TYPE < I,N,C,D > . ( OR ) PARAMETERS: ( length ) TYPE < I,N,C,D > DEFAULT ‘’. Eg: 1. PARAMETERS: A TYPE I. WRITE:/ A. 2.

PARAMETERS: B(3) TYPE N. WRITE:/ B.

3.

PARAMETERS: C(5) TYPE C DEFAULT 'KAVI'. WRITE:/ C.

4. PARAMETERS: D TYPE DATS DEFAULT '20060214'. WRITE:/ D. 5.

PARAMETERS: E TYPE DATS. WRITE:/ E.

6.

PARAMETERS: F(5) TYPE C OBLIGATORY. WRITE:/ F. ( If we use obligatory, mandatory we should enter the value. Otherwise cannot execute.)

7.

PARAMETERS: G(5) TYPE C LOWER CASE. WRITE:/ G.

(If we enter the characters in lower case, we get the o/p in lower case.)

8.

PARAMETERS: H AS CHECKBOX. PARAMETERS: I AS CHECKBOX DEFAULT 'X'.

(The checkbox will appear to be set if we set the flag.)

9. PARAMETERS: J RADIOBUTTON GROUP G1, K RADIOBUTTON GROUP G1, L RADIOBUTTON GROUP G1 DEFAULT 'X'. (By default the first one will be set. If we want any other radio-button to be selected we need to use the flag set.)

Kavitha .A.S SELECTION SCREEN

OUTPUT SCREEN

Kavitha .A.S TABLES Syntax TABLES: . TABLES: < structure-name>. Eg: 1. TABLES: MARA. 2. TABLES: ZKA_CENTER.

TYPES: Syntax TYPES: ( length ) TYPE < I,N,C,D > . TYPES: A TYPE I . DATA: B TYPE A VALUE '123', C TYPE A VALUE '345', D TYPE A VALUE '567'. WRITE:/ B,C,D.

Kavitha .A.S

INTERNAL TABLE: An internal table is a temporary table stored in RAM of the application server. It is created and filled by a program during execution and is discarded when the program ends. Types of Internal Table: 1. Internal table without Header line. 2. Internal Table with header line.

INTERNAL TABLE MANIPULATIONS: 1. READ: READ TABLE WITH KEY < fieldname = ‘’>. 2. INSERT: INSERT index . 3. MODIFY: MODIFY index 4. DESCRIBE: DESCRIBE TABLE lines OCCURS . 5. APPEND: APPEND . 6. CLEAR CLEAR . 7. REFRESH REFRESH .

Kavitha .A.S INTERNAL TABLE WITHOUT HEADER LINE: DATA: BEGIN OF HEADER, BOOKNO(4) TYPE N, BOOKNAME(5) TYPE C, BOOKADD(10) TYPE C, END OF HEADER. DATA: BODY LIKE HEADER OCCURS 0. HEADER-BOOKNO = '1234'. HEADER-BOOKNAME = 'SAP'. HEADER-BOOKADD = 'TNAGAR'. APPEND HEADER TO BODY. CLEAR HEADER. HEADER-BOOKNO = '1235'. HEADER-BOOKNAME = 'ABAP'. HEADER-BOOKADD = 'ANNANAGAR'. APPEND HEADER TO BODY. CLEAR HEADER. HEADER-BOOKNO = '1236'. HEADER-BOOKNAME = 'ERP'. HEADER-BOOKADD = 'ADYAR'. APPEND HEADER TO BODY. CLEAR HEADER. LOOP AT BODY INTO HEADER. WRITE:/ HEADER-BOOKNO,HEADER-BOOKNAME,HEADER-BOOKADD. ENDLOOP.

Kavitha .A.S INTERNAL TABLE WITH HEADER LINE WITH MANIPULATIONS: REPORT ZKA_INTTABWITHITAB . DATA: BEGIN OF ITAB OCCURS 0, BOOKNO(4) TYPE N, BOOKNAME(5) TYPE C, BOOKADD(10) TYPE C, END OF ITAB. ITAB-BOOKNO = '1234'. ITAB-BOOKNAME = 'SAP'. ITAB-BOOKADD = 'TNAGAR'. APPEND ITAB. CLEAR ITAB. ITAB-BOOKNO = '1235'. ITAB-BOOKNAME = 'ABAP'. ITAB-BOOKADD = 'ANNANAGAR'. APPEND ITAB. CLEAR ITAB. ITAB-BOOKNO = '1236'. ITAB-BOOKNAME = 'ERP'. ITAB-BOOKADD = 'ADYAR'. APPEND ITAB. CLEAR ITAB. LOOP AT ITAB. WRITE:/ ITAB-BOOKNO,ITAB-BOOKNAME,ITAB-BOOKADD. ENDLOOP. SKIP 2. READ TABLE ITAB WITH KEY BOOKNO = '1235'. WRITE:/ ITAB-BOOKNO,ITAB-BOOKNAME,ITAB-BOOKADD. SKIP 2. READ TABLE ITAB INDEX 3. WRITE:/ ITAB-BOOKNO,ITAB-BOOKNAME,ITAB-BOOKADD. SKIP 2. ITAB-BOOKNO = '9876'. ITAB-BOOKNAME = 'ORACLE'. ITAB-BOOKADD = 'TAMBARAM'. INSERT ITAB INDEX 2. LOOP AT ITAB. WRITE:/ ITAB-BOOKNO,ITAB-BOOKNAME,ITAB-BOOKADD. ENDLOOP. SKIP 2. ITAB-BOOKNO = '4567'. ITAB-BOOKNAME = 'BASIS'. ITAB-BOOKADD = 'AVADI'.

Kavitha .A.S MODIFY ITAB INDEX 2. LOOP AT ITAB. WRITE:/ ITAB-BOOKNO,ITAB-BOOKNAME,ITAB-BOOKADD. ENDLOOP. SKIP 2. DELETE ITAB INDEX 2. LOOP AT ITAB. WRITE:/ ITAB-BOOKNO,ITAB-BOOKNAME,ITAB-BOOKADD. ENDLOOP. SKIP 2. CLEAR ITAB. WRITE:/ ITAB-BOOKNO,ITAB-BOOKNAME,ITAB-BOOKADD. SKIP 2. FREE ITAB. WRITE:/ ITAB-BOOKNO,ITAB-BOOKNAME,ITAB-BOOKADD. SKIP 2. REFRESH ITAB. LOOP AT ITAB. WRITE:/ ITAB-BOOKNO,ITAB-BOOKNAME,ITAB-BOOKADD. ENDLOOP.

Kavitha .A.S

SQL STATEMENTS: OPEN SQL: Subset of standard SQL statements. To avoid conflicts between database tables and to keep ABAP programs independent from the database system used, SAP has generated its own set of SQL statements known as Open SQL. Using Open SQL allows you to access all database tables available in the R/3 System, regardless of the manufacturer. Database language that allows you to include database-specific SQL statements in an ABAP program.

NATIVE SQL To enable the execution of ABAP Native SQL statements in ABAP programs, you use the statement EXEC. Most ABAP programs containing database-specific SQL statements do not run with different databases. If programs are meant to run against variosu databases, you should use ABAP Open SQL.

EXERCISE: TABLES: ZKA_CENTER. DATA: ITAB LIKE ZKA_CENTER OCCURS 0 WITH HEADER LINE. SELECT * FROM ZKA_CENTER. WRITE:/ ZKA_CENTER-CENTERNO,ZKA_CENTER-CENTERNAME,ZKA_CENTERCENTERPHONE,ZKA_CENTER-CENTERID,ZKA_CENTER-CENTERMAN. ENDSELECT.

SELECT SINGLE * FROM ZKA_CENTER WHERE CENTERNAME = 'INEST'. WRITE:/ ZKA_CENTER-CENTERNO,ZKA_CENTER-CENTERNAME,ZKA_CENTERCENTERPHONE,ZKA_CENTER-CENTERID,ZKA_CENTER-CENTERMAN.

SELECT * FROM ZKA_CENTER INTO TABLE ITAB. LOOP AT ITAB. WRITE:/ ITAB-CENTERNO,ITAB-CENTERNAME,ITAB-CENTERPHONE,ITABCENTERID,ITAB-CENTERMAN. ENDLOOP.

Kavitha .A.S

SELECT CENTERNAME CENTERMAN FROM ZKA_CENTER INTO CORRESPONDING FIELDS OF TABLE ITAB. LOOP AT ITAB. WRITE:/ ITAB-CENTERNO,ITAB-CENTERNAME,ITAB-CENTERPHONE,ITABCENTERID,ITAB-CENTERMAN. ENDLOOP.

Kavitha .A.S

MORE ABAP KEYWORDS ADD ADD n TO m. Effect Adds the contents of n to the contents of M and stores the result in m . This is equivalent to: m = m + n. Example: DATA: NUMBER TYPE I VALUE 3, SUM TYPE I VALUE 5. ADD NUMBER TO SUM. The field SUM now contains 8, whilst the contents of the field NUMBER remains unchanged at 3.

ASSIGN ASSIGN f TO . Additions 1. ... TYPE typ 2. ... DECIMALS dec 3. ... LOCAL COPY OF ... Effect Assigns the field f to the field symbol . The field symbol "points to" the contents of the field f at runtime, i.e. every change to the contents of f is reflected in and vice versa. If the field symbol is not typed (see FIELD-SYMBOLS ), the field symbol adopts the type and atrributes of the field f at runtime, particularly the conversion exit. Otherwise, when the assignment is made, the system checks whether the type of the field f matches the type of the field symbol .

CASE CASE f. Effect Case distinction. Depending on the current contents of a field, this statement executes one of several alternative processing branches. The field whose contents determine how the subsequent processing is specified after CASE ; the individual processing branches are introduced by

Kavitha .A.S WHEN , followed by the value to be tested. The entire block is concluded by ENDCASE . The structure of the CASE statement is as follows: CASE f. WHEN f1. ... WHEN f2. ... ... ENDCASE. On reaching such a CASE statement, the processor compares f with f1 . If f = f1 , it executes the processing block between " WHEN f1. " and the next WHEN statement. If there are no further WHEN statements, it executes the processing block up to the ENDCASE statement and then continues with any subsequent processing. If f f1 , the processor compares the field f2 in the next WHEN statement with f and proceeds as with f1 and so on. Although f should be a variable, f1 can be a variable or a literal. For the comparison " f = f1 ", the rules are the same as for IF . There is a second variant of the WHEN statement: WHEN OTHERS. No more than one such WHEN statement is allowed within a CASE block. The " WHEN OTHERS " processing block is always concluded by ENDCASE , i.e. no further WHEN statements can follow. The " WHEN OTHERS " processing block is executed only if none of the preceding WHEN blocks have been executed, i.e. if all previous comparisons (" f = ... ) have returned a negative result. Example DATA: ONE TYPE I VALUE 1, THREE TYPE P VALUE 3. DO 5 TIMES. CASE SY-INDEX. WHEN ONE. WRITE / 'That is'. WHEN 2. WRITE 'a'. WHEN THREE. WRITE 'good'. WRITE 'example'. WHEN OTHERS. WRITE '!'. ENDCASE. ENDDO. Output: " That is a good example ! ! "

Kavitha .A.S CHECK - within loops Basic form CHECK logexp. Effect CHECK evaluates the subsequent logical expression . If it is true, the processing continues with the next statement. In loop structures like DO ... ENDDO WHILE ... ENDWHILE LOOP ... ENDLOOP SELECT ... ENDSELECT CHECK with a negative outcome terminates the current loop pass and goes back to the beginning of the loop to start the next pass, if there is one. In structures like FORM ... ENDFORM FUNCTION ... ENDFUNCTION MODULE ... ENDMODULE AT CHECK with a negative outcome terminates the routine or modularization unit. If CHECK is not in a loop or a routine or a modularization unit, a negative logical expression terminates the current event. In contrast, the statement REJECT terminates the current event, even from loops or subroutines.

COLLECT Basic form COLLECT [wa INTO] itab. Addition ... SORTED BY f Effect COLLECT is used to create unique or compressed datsets. The key fields are the default key fields of the internal table itab . If you use only COLLECT to fill an internal table, COLLECT makes sure that the internal table does not contain two entries with the same default key fields.

Kavitha .A.S

If, besides its default key fields, the internal table contains number fields (see also ABAP/4 number types ), the contents of these number fields are added together if the internal table already contains an entry with the same key fields. If the default key of an internal table processed with COLLECT is blank, all the values are added up in the first table line. If you specify wa INTO , the entry to be processed is taken from the explicitly specified work area wa . If not, it comes from the header line of the internal table itab . After COLLECT , the system field SY-TABIX contains the index of the - existing or new table entry with default key fields which match those of the entry to be processed.

CONCATENATE Basic form CONCATENATE f1 ... fn INTO g. Addition .. SEPARATED BY h Effect Places the fields f1 to fn after g . With the fields fi (1
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF