WHAT IS THE DIFFERENCE BETWEEN STATIC VARIABLE AND INSTANCE VARIABLE

PARAMETERINSTANCE VARIABLESTATIC VARIABLE
AccessThe instance variable is accessed through the object of the class. An object must be created for the class in order to access the instance variableThe static variable is accessed through the class name. Object is not needed to access the variable value.
LifetimeInstance variables exists till the object exists. Once the object is destroyed,the instance variable is also destroyedDuring the start of the program, the static variables are created. As soon as the program stops, the static variable is also destroyed
Examplepr_object1->pv_inst_var = 10. Pv_inst_var is declared in public section. zcl_static_instance=>static_variable = 20.
valueEach object of the class holds their own value for the instance variableThere is only one static variable value for all the objects of the class.

EXAMPLE: STATIC AND INSTANCE VARIABLE

CLASS zcl_static_instance DEFINITION.

              PUBLIC SECTION.

                            METHODS set_instance_variable IMPORTING iv_instance_variable TYPE i.

                            METHODS get_instance_variable RETURNING VALUE(rv_instance_variable) TYPE i.

                            CLASS-DATA static_variable TYPE i.

              PROTECTED SECTION.

              PRIVATE SECTION.

                            DATA pv_instance_variable TYPE i.

ENDCLASS.

CLASS zcl_static_instance IMPLEMENTATION.

              METHOD set_instance_variable.

                            Pv_instance_variable = iv_instance_variable.

              ENDMETHOD.

              METHOD get_instance_variable.

                            Rv_instance_variable = pv_instance_variable.

              ENDMETHOD.

ENDCLASS.

REPORT 1.

DATA pr_object1 TYPE REF TO zcl_static_instance.

Pr_object1->set_instance_variable( iv_instance_variable = 10 ).

Lv_obj1_inst_val = pr_object1->get_instance_variable( ).

Zcl_static_instance=>static_variable = 10.

WRITE:/ ‘Object1 instance variable value:’, lv_obj1_inst_val.

WRITE:/ ‘Object1 static variable value:’, zcl_static_instance=>static_variable.

OUTPUT:

Object1 instance variable value: 10

Object1 static variable value: 10

REPORT 2.

DATA pr_object2 TYPE REF TO zcl_static_instance.

Pr_object2->set_instance_variable( iv_instance_value = 20 ).

Lv_obj2_inst_value = pr_object2->get_instance_variable( ).

Zcl_static_instance=>static_variable = 30.

WRITE:/ ‘Object2 instance variable value:’, lv_obj2_inst_value.

WRITE:/ ‘Object2 static value:’, zcl_static_instance=>static_variable.

OUTPUT:

Object2 instance variable value: 20

Object2 static value: 30

How to compare OPEN-SQL – SELECT and INTERNAL TABLE READ Query

Ztt_db_table2 is the table name

PARAMETEROPEN-SQL – SELECTINTERNAL TABLE – READ, Table expressions, LOOP AT
SINGLE RECORDSELECT SINGLE * FROM ztt_db_table2 INTO ls_contacta) READ TABLE lt_contact INDEX 1 INTO ls_contact.

b) READ TABLE lt_contact WITH KEY contact_id = 101 INTO ls_contact.

c) DATA lt_contact TYPE TABLE OF ztt_db_table2 WITH NON-UNIQUE KEY contact_id.

READ TABLE lt_contact WITH TABLE KEY contact_id = 102 INTO ls_contact.

d) READ TABLE lt_contact TRANSPORTING NO FIELDS WITH KEY contact_id = 101

e) ls_contact = lt_contact[ 1 ]

f) ASSIGN lt_contact[ contact_id = 101 ] TO FIELD-SYMBOL(<ls_contact>).

g) DATA lt_sorted_table TYPE SORTED TABLE OF ztt_db_table2 WITH UNIQUE KEY contact_id.

ls_sorted_contact = lt_sorted_contact[ KEY primary_key COMPONENTS contact_id = 101 ]
READING MORE RECORDSa) SELECT * FROM ztt_db_table2 INTO TABLE lt_contact

b) SELECT * FROM ztt_db_table2 APPENDING TABLE lt_contact

c) SELECT * FROM ztt_db_table2 INTO TABLE lt_contact UPTO 5 ROWS.

d) SELECT DISTINCT * FROM ztt_db_table2 INTO TABLE lt_contact

e) SELECT * FROM ztt_db_table2 INTO ls_contact.

ENDSELECT.
LOOP AT lt_contact ASSIGNING FIELD-SYMBOL(<ls_contact>) FROM 1 TO 2 WHERE contact_id = 102.

WRITE:/ <ls_contact>-contact_id.

ENDLOOP.

How to compare OPEN-SQL INSERT and INTERNAL TABLE-INSERT Query

Ztt_db_table2 is the Database table name

PARAMETEROPEN-SQL-INSERTINTERNAL TABLE-INSERT
Structurei) INSERT ztt_db_table2 FROM ls_contact
ii) INSERT INTO ztt_db_table2 VALUES ls_contact
i) APPEND ls_contact TO lt_contact
ii) INSERT ls_contact INTO TABLE lt_contact
iii) INSERT ls_contact INTO lt_contact INDEX 1
Internal tablei) INSERT INTO ztt_db_table2 FROM TABLE lt_internal_table
ii) INSERT INTO ztt_db_table2 FROM TABLE lt_internal_table ACCEPTING DUPLICATE KEYS
i) INSERT LINES OF lt_contact2 INTO TABLE lt_contact
ii) INSERT LINES OF lt_contact2 INTO lt_contact INDEX 1.
iii) INSERT LINES OF lt_contact2 FROM 1 TO 3 INTO TABLE lt_contact
iv) INSERT LINES OF lt_contact2 USING KEY primary_key INTO TABLE lt_contact
v) APPEND LINES OF lt_contact2 TO lt_contact.
vi) APPEND LINES OF lt_contact2 FROM 1 TO 2 TO lt_contact
vii) DATA lt_contact2 TYPE SORTED TABLE OF ztt_db_table2 WITH UNIQUE KEY primary_key COMPONENTS contact_id.
APPEND LINES OF lt_contact2 USING KEY primary_key TO lt_contact
VALUElt_internal_table = VALUE #( ( contact_id = 101 contact_name = ‘Raj’ )
( contact_id = 102
contact_name = ‘Mike’ ) ).

Difference between OPEN-SQL MODIFY and INTERNAL TABLE MODIFY Query

ztt_db_table2 is the Database table name

ParameterOPEN-SQL MODIFYINTERNAL TABLE – MODIFY
structureMODIFY ztt_db_table2 FROM ls_structurea) MODIFY lt_internal_table FROM ls_structure TRANSPORTING contact_address WHERE contact_id = 102
b) MODIFY lt_internal_table FROM ls_structure INDEX 2 TRANSPORTING contact_name contact_address
Internal tableMODIFY ztt_db_table2 FROM TABLE lt_internal_table——-

Note: if the database table name and internal table name are same, then it will consider it as internal table. It will access the records of the internal table instead of the database table name

Difference between Open-SQL DELETE and Internal table DELETE query

ztt_db_table2 is the Database table name

ParameterOPEN-SQL – DELETEINTERNAL TABLE – DELETE
WHEREDELETE FROM ztt_db_table2 WHERE contact_id = 102DELETE lt_internal_table WHERE contact_id = 102.
STRUCTURE DELETE ztt_db_table2 FROM ls_structureDELETE TABLE lt_internal_table FROM ls_structure
INTERNAL TABLE DELETE ztt_db_table2 FROM TABLE lt_internal_table——-
INDEX——-DELETE lt_internal_table INDEX 2.
DELETE lt_internal_table FROM 2 to 3.

How to create Secondary Database connections in SAP ABAP

In addition to the primary database, other databases can be accessed using secondary database connections.

  • Using Open-SQL:

The keyword ‚CONNECTION’ and the database connection name is specified in the query. The connections are maintained in the Transaction ‘DBA COCKPIT’.

Syntax: SELECT ….. FROM db_table CONNECTION connection_name INTO ….

Example: The Secondary Database name is secondary_db

DATA ls_contact TYPE ztt_db_table2.

SELECT * FROM ztt_db_table2 CONNECTION secondary_db INTO ls_contact.

  • Using Native SQL or ADBC:

DATA lr_sql_statement TYPE REF TO cl_sql_statement.

DATA lr_sql_result_set TYPE REF TO cl_sql_result_set.

TRY.

    Lr_sql_statement = cl_sql_connection=>get_connection(‘secondary_db’)->create_statement( ).

    Lr_sql_result_set = lr_sql_statement->execute_query( l SELECT SINGLE * FROM ztt_db_table2 WHERE contact_id = 101 l ).

    CATCH cx_sql_exception.

ENDTRY.

How to use MODIFY to modify or insert records in Database table in SAP ABAP

Using the MODIFY statement, we can modify the existing records in a Database table. If the records does not exist in a Database table, then it can insert records in to Database table. This can be achieved in the following two ways.

  1. Through Structure.
  2. Through Internal table.
  • Structure

It is used to modify a single line of record in a Database table. If the line does not exist in the Database table, then a new line will be inserted to the Database table. The contents of the structure must always follow the primary key values. If the component values of the structure is exactly similar to line of record in the Database table, then there will be no changes after the execution of this query. The structure is specified after the ‘FROM’ keyword. It is always recommended to define the structure of type (DB table) like below

Syntax: MODIFY DB_TABLE FROM structure

              DATA ls_contact TYPE ztt_db_table2.

              ls_contact – contact_id = 104.

              ls_contact – contact_name = ‘Sanjay’.

              ls_contact – contact_id = ‘Street 88’.

              MODIFY ztt_db_Table2 FROM ls_contact.

If the contact_id = 104 does not exist, then a new line of record will be inserted into the Database table ztt_db_table2.

If anyone line of record is changed or inserted, then the system field sy-subrc will be set to 0. Otherwise, it will be set to 4.

If some of the components of the structure are not mentioned, then initial values will be set to those fields in the Database table. For example, contact_age is not mentioned in the structure, therefore it will hold initial values in the Database table.

  • Through Internal table:

More than one line of record can be modified or inserted using this option. It is done through the following syntax.

Syntax: MODIFY DB_table FROM lt_int_tab.

DATA ls_contact TYPE ztt_db_table2.

DATA lt_contact TYPE TABLE OF ztt_db_table2.

ls_contact-contact_id = 101.

ls_contact-contact_name = ‘Mahesh’.

ls_contact-contact_address = ‘Street 10’.

APPEND ls_contact TO lt_contact.

ls_contact-contact_id = 110.

ls_contact-contact_name = ‘Andreas’.

ls_contact-contact_address = ‘Street 55’.

APPEND ls_contact TO lt_contact.

MODIFY ztt_db_table2 FROM TABLE lt_contact.

The line of record with contact_id = 101 will be modified in the Database table, as the line already exists. The line of record with contact_id = 110 will be inserted in the Database table, as the line of record does not exists.

If anyone of the operation, that is either modifying or inserting is successful, then the system field sy-subrc is set to 0. The system field sy-dbcnt will print out the lines of record which are changed.

How to update Data records in a Database table in SAP ABAP

Using the ‘UPDATE’ keyword, we can modify one or more Data records in an internal table.

The data records can be modified in the following different ways.

  1. Through Structure.
  2. Through Internal table.
  3. Through ‚WHERE‘ Clause.
  • Through Structure:

Single line of record can be modified using structure. Therefore define a structure with reference to the Database table name. The line of record must correspond to the primary key of the structure. Structure name is given after the ‘FROM’ clause. If the data records are updated successfully, then the system field sy-subrc is set to 0. If the data records are not updated successfully, then the system field sy-subrc is set to 4.

Now let’s look at the syntax.

DATA ls_contact TYPE ztt_db_table2.

ls_contact-contact_id = 102.

ls_contact-contact_name = ‘Sai’.

UPDATE ztt_db_table2 FROM ls_contact.

If all the components of the structure are not assigned with values, the initial values will be set in Database table for those components.

  • Through Internal table.

If more than one line of record must be updated, then we can use an Internal table to achieve that. The structure of the Internal table must be similar to the Database table. All lines of records will be updated which have the same primary key values. The lines of record which are not found will not be updated. At the same time, it will not influence the rest of the data records. If all the lines of record are updated successfully, then the system field sy-subrc is set to 0. If atleast one line of record is not updated successfully, then the system field sy-subrc is set to 4. The system field sy-dbcnt will print out the number of records updated successfully.

UPDATE ztt_db_table2 FROM TABLE lt_int_table.

  • Through ‘WHERE’ clause

Through this option we can change more number of records targeted at a particular column. The ‘WHERE’ clause can further be added, to constrain the updated records. Through the addition of the keyword ‘SET’, we can specify which columns of the Database table should be changed.

If atleast one of the data record is updated successfully, then the system field sy-subrc is set to 0. If none of the data records is updated successfully, then the system field is set to sy-subrc = 4. The system field sy-dbcnt prints out the number of Data records which are updated successfully.

The following statement is the syntax for ‘WHERE’ clause.

UPDATE ztt_db_table2 SET contact_address = ‘Street 10’ WHERE contact_name = ‘Mike’.

The execution of above query will set the contact_address to ‘Street 10’ for all the contacts with the contact_name = ‘Mike’.

How to insert Data records in to Database table in SAP ABAP

With the help of ‘INSERT’ keyword, we can insert one or more records in to an internal table.

Data records can be inserted in the following two different ways:

  1. Through structure.
  2. Through Internal table.
  • Through structure.

DATA ls_contact TYPE ztt_db_table2.

ls_contact – contact_id = 104.

ls_contact – contact_name = ‘Bala’.

ls_contact – contact_address = ‘Street 55’.

The data records can be inserted in following two different ways from a structure.

  1. INSERT ztt_db_table2 FROM ls_contact.
  2. INSERT INTO ztt_db_table2 VALUES ls_contact.

Both the above INSERT operations are very much similar. If the Data records are inserted successfully, then the system field sy-subrc is set to 0. If it is not inserted successfully, then the system field sy-subrc is set to 4.

  • Through Internal table.

More than one data record can be inserted into Database table through Internal tables. If the line of record is already present in an internal table, then it results in runtime error. But this can be avoided through the keywords ACCEPTING DUPLICATE KEYS. Through the above keyword, already existing lines of record will be ignored.

If the records are inserted successfully, then the system field sy-subrc is set to 0. If the data records are not inserted successfully, then the system field sy-subrc is set 4. The system field sy-dbcnt will print out the number of records inserted in to the Database table.

DATA lt_int_table TYPE TABLE OF ztt_db_table2.

DATA ls_contact TYPE ztt_db_table2.

ls_contact -contact_id = 105.

ls_contact -contact_name = ‘Suresh’.

ls_contact -contact_address = ‘Street 88’.

APPEND ls_contact TO lt_int_table.

CLEAR ls_contact.

ls_contact -contact_id = 106.

ls_contact -contact_name = ‘Sanjay’.

ls_contact -contact_address = ‘Street 98’.

APPEND ls_contact TO lt_int_table.

INSERT INTO ztt_db_table2 FROM TABLE lt_int_table.

The Duplicate records can be ignored by using the following INSERT statement.

INSERT INTO ztt_db_table2 FROM TABLE lt_int_table ACCEPTING DUPLICATE KEYS.

How to delete records from a Database table in SAP ABAP

Using the DELETE statement, you can delete one or more records from a Database table. It can be deleted in the following three different ways.

  • Through ‘WHERE’ clause.
  • Through structure (work area).
  • Through Internal table.

  • Through ‘WHERE’ clause.

The data records from the Database table are deleted based on the condition mentioned in the ‘WHERE’ clause. If at least one record is deleted from the Database table, then the system field is set to sy-subrc = 0. If the deletion is unsuccessful, then system field sy-subrc is set to 4. The system field sy-dbcnt will give out the count of the records deleted.

DELETE FROM ztt_db_table2 WHERE contact_id = 102.

  • Through Structure (Work area).

With the help of the structure, we can delete exactly a single line of record from the Database table. The keyword ‘FROM’ will be used after the table name. If the single line of record is found and deleted, then system field sy-subrc will be set to 0. If the record is not found and therefore not deleted, then the system field sy-subrc will be set to 4.

DATA ls_contact TYPE ztt_db_table2.

ls_contact – contact_id = 101.

ls_contact – contact_name = ‘Thiru’.

ls_contact – contact_name = ‘Street 10’.

DELETE ztt_db_table2 FROM ls_contact.

  • Through Internal table.

The Data records from the Database table can also be deleted through Internal table. Initially, the internal table is loaded with all the data records to be deleted. ‘FROM’ keyword is used after the Database table name and then it is followed by ‘TABLE’ keyword. The internal table name is mentioned after the ‘TABLE’ keyword.

DATA lt_int_table TYPE TABLE OF ztt_db_table2.

DATA ls_data_record TYPE ztt_db_table2.

ls_data_record – contact_id = 103.

ls_data_record – contact_name = ‘Mahesh’.

ls_data_record – contact_address = ‘Street 10’.

APPEND ls_data_record TO lt_int_table.

ls_data_record – contact_id = 102.

ls_data_record – contact_name = ‘Mani’.

ls_data_record – contact_address = ‘Street 11’.

APPEND ls_data_record TO lt_int_table.

DELETE ztt_db_table2 FROM TABLE lt_int_table.