Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Update, Insert, Modifying SAP tables using PROGRAM

Former Member
0 Likes
12,620

Hi folks,

How can we UPDATE, INSERT & MODIFY the SAP tables using a program. Can you please give me EXAMPLE code for the each of those.

Thanks in advance.

Regards,

Ram

7 REPLIES 7
Read only

Former Member
0 Likes
2,135

You can use SAP help. ABAPHELP is the tcode

Read only

former_member188829
Active Contributor
0 Likes
2,135

By using (ABAP Transactions) Dialog programming We can update ,insert and modify the SAP TABLES.

in this program the program type is modulepool programming .

Read only

Former Member
0 Likes
2,135

hi,

by using maintainance view we can do yhis........

regards,

ram.

Read only

Former Member
0 Likes
2,135

Hi,

go to tcode "ABAPDOCU".double click on keyword help.enter the keywor and press enter.then u can get all the syntaxes.

regards,

bharat.

Read only

Former Member
0 Likes
2,135

Hi Ram,

Plz try it out.

ex:

INSERT INTO ZMara VALUES ITAB.

UPDATE ZMara SET mtart = ITAB-matrt.

sample code:

MODULE PROCESS INPUT.

IF SY-UCOMM = 'SAVE'.

LOOP AT ITAB1.

IF ITAB1-CHECK1 = 'I'.

READ TABLE ITAB WITH KEY NAME = ITAB1-NAME

FRIEND_NAME = ITAB1-FRIEND_NAME.

INSERT INTO ZFRIENDS_CONTACT VALUES ITAB.

ELSEIF ITAB1-CHECK1 = 'D'.

DELETE FROM ZFRIENDS_CONTACT WHERE NAME = ITAB1-NAME

AND FRIEND_NAME = ITAB1-FRIEND_NAME.

ELSEIF ITAB1-CHECK1 = 'M'.

READ TABLE ITAB WITH KEY NAME = ITAB1-NAME

FRIEND_NAME = ITAB1-FRIEND_NAME.

UPDATE ZFRIENDS_CONTACT SET NAME = ITAB-NAME

FRIEND_NAME = ITAB-FRIEND_NAME

DOB = ITAB-DOB

CELL_NO = ITAB-CELL_NO

WHERE NAME = ITAB-NAME AND

FRIEND_NAME = ITAB-FRIEND_NAME.

ENDIF.

ENDLOOP.

ENDIF.

ENDMODULE. " PROCESS INPUT

Regards,

Kiran B

Read only

Former Member
0 Likes
2,135

1.INSERT

DATA scarr_wa TYPE scarr.

scarr_wa-carrid = 'FF'.

scarr_wa-carrname = 'Funny Flyers'.

scarr_wa-currcode = 'EUR'.

scarr_wa-url = 'http://www.funnyfly.com'.

INSERT INTO scarr VALUES scarr_wa.

Alternative 2

... FROM TABLE itab ...

Effect

If you specify an internal table itab, several rows are created from its content for insertion in the database table. A row for insertion into the table is taken from each row of the internal table according to the same rules as for a single work area Einfügenwa. The line type of the internal table has to meet the prerequisites for use in Open-SQL statements.

If a row witht he same primary key or a same unique secondary index does not already exist in the database table for any of the rows to be inserted, all rows are inserted and sy-subrc is set to 0. If the internal table is empty, sy-subrc is also set to 0. The system field sy-dbcnt is always set to the number of rows that were actually inserted.

If a row with the same primary key or a same unique secondary index already exists in the database table for one or more of the rows to be inserted, these rows cannot be inserted. In this situation, there are three possibilities:

2.UPDATE

Example

Dynamic conversion of the content of an arbitrary column in an arbitrary database table of a previous currency in Euro.

PARAMETERS: table TYPE c LENGTH 30,

column TYPE c LENGTH 30,

old_curr TYPE sycurr.

DATA: set_expr TYPE string,

condition TYPE string.

CONCATENATE column ` = 'EUR'`

INTO set_expr.

CONCATENATE column ` = old_curr`

INTO condition.

TRY.

UPDATE (table)

SET (set_expr)

WHERE (condition).

CATCH cx_sy_dynamic_osql_error.

MESSAGE `Error in update!` TYPE 'I'.

ENDTRY.

Alternative 2

... FROM wa

Effect

If you specify a non-table-type work area wa, the system will search for a row in the database table which, in its primary key, has the same content as the respective beginning part of the work area. The content of the work area is interpreted in its non-converted form and in accordance with the structure of the database table or the view. The content of the work area is assigned to this row. The assignment takes place without conversion from the left to the right in accordance with the structure of the database table or the view. The work area must fulfill the prerequisites for use in Open SQL statements.

If there is no row with the same content for the primary key in the database or if the change would lead to a double entry in a unique secondary index, the line is not changed and sy-subrc is set to 4.

Notes

The work area wa should always be declared in relation to the database table or the view in the ABAP Dictionary.

If you have a static specification of the database table or the view, the specification of the work area using FROM wa can be omitted outside of classes - provided a table work area dbtab for the respective database table or the view is declared using the statement TABLES. The system expands the UPDATE statement implicitly to include the addition FROM dbtab.

Alternative 3

... FROM TABLE itab

Effect

When an internal table itab is specified, the system processes all the rows of the internal table in accordance with the rules for the work area wa. The row type of the internal table must meet the requirements for use in Open-SQL statements.

If, in the database, there is no row with the same content of the primary key for a row in the internal table, or if the change would lead to a double entry in a unique secondary key, the respective row is not changed and sy-subrc is set to 4. If the internal table is empty, sy-subrc is set to 0. The system field sy-dbcnt is always set to the number of rows actually inserted.

Example

Reduction of the flight cost for all of today's flights of an airline carrier in the database table SFLIGHT by the percentage percent. The calculation of the new price is always done in an internal table sflight_tab and the database table is changed accordingly.

PARAMETERS: p_carrid TYPE sflight-carrid,

percent TYPE p LENGTH 1 DECIMALS 0.

DATA sflight_tab TYPE TABLE OF sflight.

FIELD-SYMBOLS -price * ( 1 - percent / 100 ).

ENDLOOP.

ENDIF.

UPDATE sflight FROM TABLE sflight_tab.

3.MODIFY

Alternative 1

... FROM wa

Effect

When a wa work area that is not table-type is specified, which meets the requirements for use in Open SQL statements, a line is searched for in the database table that has the same content in the primary key as the corresponding beginning part of the work area.

If such a line is not found, a new line is inserted according to the same rules as for the INSERT statement.

If such a line is found, this line is overwritten according to the same rules as for the UPDATE statement.

If the change would lead to a double entry in a unique secondary index, then it is not executed and sy-subrc is set to 4.

Notes

The wa work area should always be declared with reference to the database table or the view in the ABAP Dictionary.

If the the database table or view is specified statically, then you the specification of the work area using FROM wa can be ommitted outside of classes if a dbtab table work area is declared for the corresponding database table or for the view using the TABLES statement. The system enhances the MODIFY statement implicitly with the FROM dbtab addition.

Example

Create or change a message in database table T100. If there is no message with the number 100 in the MYMSGCLASS message class in English, it will be created. Otherwise only the text is changed.

DATA message_wa TYPE t100.

message_wa-sprsl = 'EN'.

message_wa-arbgb = 'MYMSGCLASS'.

message_wa-msgnr = '100'.

message_wa-text = 'Some new message ...'.

MODIFY t100 FROM message_wa.

Alternative 2

... FROM TABLE itab

Effect

If an itab internal table is specified, the system processes all lines in the internal table according to the rules for the wa work area. The line type of the internal table has to meet the requirements for use in Open SQL statements.

If the change to a line in the internal table would lead to a double entry in a unique secondary index, the corresponding line is not inserted and sy-subrc is set to 4. If the internal table is empty, sy-subrc is set to 0. The sy-dbcnt system field is always set to the number of lines that were actually processed.

4.DELETE

PARAMETERS p_carrid TYPE sflight-carrid.

DELETE FROM sflight

WHERE carrid = p_carrid AND

fldate = sy-datum AND

seatsocc = 0.

~~~~and~~~

Syntax

... FROM wa|{TABLE itab}.

Alternatives:

1. ... FROM wa

2. ... FROM TABLE itab

Effect

After FROM, you can specifiy a data object wa that is not table-type or an internal table itab. The content of the data objects determines the row(s) to be deleted.

Alternative 1

... FROM wa

Effect

If you specify a work area wa that is not table-type, a row is searched for in the database table whose primary key content is the same as that of the corresponding initial part of the work area. The content of the work area is not converted and is interpreted according to the structure of the database table or view. This row is deleted. The work area must meet the prerequisites for use in Open SQL statements.

If there is no row in the database with the same content as the primary key, no row is deleted and sy-subrc is set to 4.

Notes

The work area wa should be declared wit reference to the database table or view in the ABAP Dictionary using the length of the primary key.

If the database table of view are specified statically, you do not have to specify the work area with FROM wa outside of classes if a table work area dbtab is declared for the relevant database table or view using the TABLES statement. The system then implicitly adds the FROM dbtab addition to the DELETE statement.

Alternative 2

... FROM TABLE itab

Effect

If an internal table itab is specified, the system processed all rows of the internal table according to the rules for the work area wa. The row type of the internal table must meet the prerequisites for use in Open SQLstatements.

If, for a row of the internal table, there is no row in the database with the same content as the primary key, the corresponding row is ignored and sy-subrc is set to 4. If the internal table is empty, sy-subrc is set to 0. The system field sy-dbcnt is always set to the number of rows actually deleted.

Example

In the following example, all today's flights of an airline in which no seats are occupied are deleted from the database table SFLIGHT. The client field must be in the row structure of the internal table sflight_key_tab. Otherwise it does not cover the primary key of the database table and incorrect key values will be accepted as a result. This example has the same function as that for dtab-cond, but it requires two database accesses. The deleted rows are recorded in the internal table.

PARAMETERS p_carrid TYPE sflight-carrid.

TYPES: BEGIN OF sflight_key,

mandt TYPE sflight-mandt,

carrid TYPE sflight-carrid,

connid TYPE sflight-connid,

fldate TYPE sflight-fldate,

END OF sflight_key.

DATA sflight_key_tab TYPE TABLE OF sflight_key.

SELECT carrid connid fldate

FROM sflight

INTO CORRESPONDING FIELDS OF TABLE sflight_key_tab

WHERE carrid = p_carrid AND

fldate = sy-datum AND

seatsocc = 0.

DELETE sflight FROM TABLE sflight_key_tab.

Regards,

Ramesh.