*&---------------------------------------------------------------------*
*& Report ZPOPENSQL_1
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZPOPENSQL_1.
*SELECT
*INSERT
*UPDATE
*MODIFY
*DELETE
TABLES ZEMPLOYEES.
* This is a work area is like zemployees taking the same structure as our zemployees table.
DATA WA_EMPLOYEES LIKE ZEMPLOYEES. " It is a record structure.
* =====================================================================================================
* Insert ----------------------------------------------------------------------------------------------
* =====================================================================================================
WA_EMPLOYEES-EMPLOYEE = '10000006'.
WA_EMPLOYEES-SURNAME = 'WESTMORE'.
WA_EMPLOYEES-FORENAME = 'BRUCE'.
WA_EMPLOYEES-TITLE = 'MR'.
WA_EMPLOYEES-DOB = '19921213'.
INSERT ZEMPLOYEES FROM WA_EMPLOYEES.
*INSERT (TABLE_VARIABLE) FROM WA_EMPLOYEES. " When you use variable instead of using table name.
* Ctrl + Y to use copy multiple fields in the table.
IF SY-SUBRC = 0.
WRITE:/ 'Record Inserted Correctly'.
ELSE.
WRITE:/ 'Record inserted Failed: ', SY-SUBRC.
ENDIF.
* =====================================================================================================
* Clear ----------------------------------------------------------------------------------------------
* =====================================================================================================
* Default CLEAR REMOVE all fields.
* You can specify what field you would like to remove.
CLEAR WA_EMPLOYEES-EMPLOYEE.
*CLEAR WA_EMPLOYEES.
WA_EMPLOYEES-EMPLOYEE = '10000007'.
WA_EMPLOYEES-SURNAME = 'WESTMORE'.
WA_EMPLOYEES-FORENAME = 'BRUCE'.
WA_EMPLOYEES-TITLE = 'MR'.
WA_EMPLOYEES-DOB = '19921213'.
*INSERT ZEMPLOYEES FROM WA_EMPLOYEES.
*INSERT (TABLE_VARIABLE) FROM WA_EMPLOYEES. " When you use variable instead of using table name.
* Ctrl + Y to use copy multiple fields in the table.
IF SY-SUBRC = 0.
WRITE:/ 'Record Inserted Correctly'.
ELSE.
WRITE:/ 'Record inserted Failed: ', SY-SUBRC.
ENDIF.
* =====================================================================================================
* UPDATE ----------------------------------------------------------------------------------------------
* =====================================================================================================
WA_EMPLOYEES-EMPLOYEE = '10000006'.
WA_EMPLOYEES-SURNAME = 'EASTMORE'.
WA_EMPLOYEES-FORENAME = 'ANDY'.
WA_EMPLOYEES-TITLE = 'MR'.
WA_EMPLOYEES-DOB = '19921213'.
UPDATE ZEMPLOYEES FROM WA_EMPLOYEES.
IF SY-SUBRC = 0.
WRITE:/ 'Record Updated Correctly'.
ELSE.
WRITE:/ 'Record Updated Failed: ', SY-SUBRC.
ENDIF.
* =====================================================================================================
* Modify ----------------------------------------------------------------------------------------------
* =====================================================================================================
* With Modify statement, If the record (clarified by key) does exist, It will update,
* If it doesn't exist, insert a new record in the end.
* Modify existing record
WA_EMPLOYEES-EMPLOYEE = '10000006'.
WA_EMPLOYEES-SURNAME = 'NORTHMORE'.
WA_EMPLOYEES-FORENAME = 'PETER'.
WA_EMPLOYEES-TITLE = 'MR'.
WA_EMPLOYEES-DOB = '19921213'.
MODIFY ZEMPLOYEES FROM WA_EMPLOYEES.
IF SY-SUBRC = 0.
WRITE:/ 'Record Modified Correctly'.
ELSE.
WRITE:/ 'Record Modified Failed: ', SY-SUBRC.
ENDIF.
CLEAR WA_EMPLOYEES.
* Modify will insert a new record since there is no matahing record in the table.
WA_EMPLOYEES-EMPLOYEE = '10000007'.
WA_EMPLOYEES-SURNAME = 'SOUTHMORE'.
WA_EMPLOYEES-FORENAME = 'SUSAN'.
WA_EMPLOYEES-TITLE = 'MRS'.
WA_EMPLOYEES-DOB = '19921213'.
MODIFY ZEMPLOYEES FROM WA_EMPLOYEES.
IF SY-SUBRC = 0.
WRITE:/ 'Record Modified Correctly'.
ELSE.
WRITE:/ 'Record Modified Failed: ', SY-SUBRC.
ENDIF.
CLEAR WA_EMPLOYEES.
* =====================================================================================================
* Delete ----------------------------------------------------------------------------------------------
* =====================================================================================================
* DELETE record using key
WA_EMPLOYEES-EMPLOYEE = '10000007'.
DELETE ZEMPLOYEES FROM WA_EMPLOYEES.
IF SY-SUBRC = 0.
WRITE:/ 'Record Removed Correctly'.
ELSE.
WRITE:/ 'Record Removed Failed: ', SY-SUBRC.
ENDIF.
CLEAR WA_EMPLOYEES.
* DELETE record using logic
DELETE FROM ZEMPLOYEES WHERE SURNAME = 'BROWN'.
IF SY-SUBRC = 0.
WRITE:/ '2 RecordS Removed Correctly'.
ELSE.
WRITE:/ 'RecordS Removed Failed: ', SY-SUBRC.
ENDIF.
CLEAR WA_EMPLOYEES.
'SAP > ABAP 코드' 카테고리의 다른 글
ABAP - LOOP (0) | 2021.05.24 |
---|---|
ABAP - IF & CASE (0) | 2021.05.24 |
ABAP - DATE & TIME & CURRENCY & QUANTITY (0) | 2021.05.24 |
Character & String (0) | 2021.05.24 |
ABAP - Arithmetic (0) | 2021.05.24 |