How to set the column as editable in SAP ALV Grid

One of the key advantage of the old ALV Models is that it enables the user to edit the values in the table.

There are two different ways to set the ALV column as editable.

a) Field Catalog

b) SET_READY_FOR_INPUT

a) Field Catalog: The fields such as field name is used to set the column name of the ALV. The field ‘EDIT’ of the field catalog structure must be set to abap_true. Let us look at the following example.

FIELD-SYMBOLS <ls_field_cat> TYPE lvc_s_fcat.

LOOP AT lt_field_cat ASSIGNING <ls_field_cat>.

IF <ls_field_cat>-fieldname = ‘contact_address’.

<ls_field_cat>-edit = abap_true.

ENDIF.

ENDLOOP.

Thus the editing mode of the ALV Grid for the respective columns are activated. The ALV Grid facilitates

additional buttons for editing data.

b) The other way of editing data is through the SET_READY_FOR_INPUT method of the CL_GUI_ALV_GRID class. The editing mode is activated or deactivated through the parameter i_ready_for_input.

For activation:

CALL METHOD lr_gui_alv_grid->set_ready_for_input

EXPORTING

i_ready_for_input = 1.

For deactivation:

CALL METHOD lr_gui_alv_grid->set_ready_for_input

EXPORTING

i_ready_for_input = 0.

The data is modified through the above technique. It is much similar to the conventional

DISPLAY <-> CHANGE options of the ABAP Editor.

once the data is edited, it must be saved initially to the internal table. Based on the needs, the data can be saved to the Database table. The method check_changed_data of the class CL_GUI_ALV_GRID is used to check if the data of the ALV Grid has been changed.

DATA lv_alv_changed TYPE c.

CALL METHOD lr_gui_alv_grid->check_changed_data

IMPORTING

c_valid = lv_alv_changed.

The variable lv_alv_changed holds the value ‘X’ or initial. If the data of the grid is changed, then it is ‘X’. If it is not changed, it’s value is INITIAL. Therefore the changes are saved to the Internal table. If it is required, it can also be saved to the Database table.

How to create an ALV List based on class CL_GUI_ALV_GRID

It is the only ALV variant which is editable. We need an internal table which is based on a global structure to show the data.

There are two Display modes to display the data.

  • Full screen display : The output of the ALV List occupies the whole screen. It does not contain Container and Dynpro.
  • Container mode: It needs a Dynpro and a container.

ALV Table is built based on the following steps

  1. Loading the data in the internal table
  2. Creation of Containers. This step is optional.
  3. Assign the structure of the internal table to the Field catalog. This step is also optional.
  4. Creation of ALV Object.
  5. Definition of layout. This step is optional.
  6. Display ALV Table contents.
  • Loading data in an internal table: The data from the database table are loaded in to the internal table.

DATA lt_contact TYPE TABLE OF ztt_db_table2.

SELECT * FROM ztt_db_table2 INTO TABLE lt_contact UP TO 50 ROWS.

  • Creation of Containers

Container object is created with an unique ID. It is used for the created Dynpro. This is mainly used to show the ALV List in a particular area of the screen. If the display is not a requirement, then you can opt for the full screen display and skip this step. But it is necessary to provide the parameter cl_gui_custom_container=>default_screen in the constructor during the creation of the ALV List.

DATA lr_container TYPE REF TO cl_gui_custom_container.

CREATE OBJECT lr_container

EXPORTING

container_name = ‘container_id’.

  • Assignment of Structure to Field catalog

The structure of the internal table is assigned to the field catalog. It includes important elements such as column heading, Display size, Data type of the column. It is always recommended to use the global structure created from Transaction SE11.

DATA lt_field_cat TYPE lvc_t_fcat.

CALL FUNCTION ‘LVC_FIELDCATALOG_MERGE’

EXPORTING

i_structure_name = ‘ZTT_DB_TABLE2’

CHANGING

ct_fieldcat = lt_field_cat.

Note: This step can be skipped , if you would not like to modify the columns in the display. But the parameter i_structure_name must be specified during the call of the method SET_TABLE_FOR_FIRST_DISPLAY of the ALV Object. The field catalog is automatically assigned in the ALV.

  • Creation of ALV Object

An object is created for the class CL_GUI_ALV_GRID. Assign the created container object in the parameter of the constructor. For the full screen display, instead of the container object, cl_gui_custom_container=>default_screen is assigned to the parameter.

DATA lr_gui_alv_grid TYPE REF TO cl_gui_alv_grid.

CREATE OBJECT lr_gui_alv_grid

EXPORTING i_parent = lr_container.

  • Layout definition

The layout of the ALV Table can be changed further using this option. The various fields of the layout structure is used to design the layout of the ALV table in different formats.

DATA ls_struc_layout TYPE lvc_s_layo.

ls_struc_layout-no_toolbar = abap_true.

If this field is set, then the ALV Grid Toolbar is hidden. There are also other fields such as GRID_TITLE which displays the title on the ALV Grid.

  • Display of ALV Table.

The SET_TABLE_FOR_FIRST_DISPLAY( ) method of the ALV object is called in the final step. The layout structure is given as the exporting parameter, the internal table and field catalog is specified as the changing parameter. After this method call, the Dynpro is called using the command CALL SCREEN.

CALL METHOD lr_gui_alv_grid->set_table_for_first_display

EXPORTING

is_layout = ls_struc_layout

CHANGING

it_outtab = lt_contact

it_fieldcatalog = lt_field_cat.

CALL SCREEN 1000.

What are the list of available models to program the SAP ALV Tables

Displaying Table contents is one of the important requirement in any application. ALV is the short form of ABAP List viewer. ALV is used to display the table of contents. It generally shows the data of the internal table. It can also be used to export data in to Excel file.

REUSE_ALV_GRID_DISPLAY: It is based on the function module. DDIC Structure, Field catalog und merged field catalog are parameters of the function module.

CL_GUI_ALV_GRID: It is one of the old models of the SAP ALV List. This old model facilitates users to edit and input data in the list. Therefore the user must have authorization.

CL_SALV_TABLE: It is the new model of SAP ALV List. It has a simplified user interface. The User cannot edit or input data. The functions are very similar to old models. The new ALV models can be editable by using the old ALV Types and Adapter Class.

CL_SALV_GUI_TABLE_IDA: It is the newly introduced ALV with Integrated data access(IDA). It has a very high performance. It is directly connected to a database or view.

Most important variables used in ALV List:

  1. Internal table : It contains the table of data which is read from the Database table.
  2. Containers: It is used to show the ALV Table in a certain area of the screen.
  3. Field Catalog: It contains information such as the Column heading, breadth of the column and the data type of the column.
  4. ALV Object: It is the reference variable which specfies the ALV Object.
  5. Layout: It is variable of type structure ( lvc_s_layo ). As the name clearly states that it is used to change the layout of the table.

Creation of ALV List is an easy task. But to bring additional features in the ALV List such as editing fields, disabling fields, Column changes, Highlighting rows or columns, Icons, sorting data, functions, events are more complicated.

ALV List models

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.