Application Development 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: 

updating database

Former Member
0 Kudos

Dear experts,

I need to update the values from the internal table to database.

pls provide some sample code for the above requirement

Thnaks and regards

karthik

9 REPLIES 9

Former Member
0 Kudos

say ur table is ABC...

loop at itab.

ABC-field1 = itab-field1.

ABC-field2 = itab-field2.

insert ABC. (u can use modify/update also).

endloop.

Message was edited by:

Ramesh Babu Chirumamilla

Former Member
0 Kudos

Hi,

LOOP AT ITAB.

DATABASE-FIELD1 = ITAB-FIELD1.

DATABASE-FIELD2 = ITAB-FIELD2.

DATABASE-FIELD3 = ITAB-FIELD3.

INSERT DATABASE.

ENDLOOP.

In the Aboe the DATABASE is the database table and ITAB is Internal table

Regards

Sudheer

Former Member
0 Kudos

Hi Karthik,

Use MODIFY statement to update database entries from internal table itab.

<b>MODIFY dbtab FROM TABLE itab.</b>

It is not advisable to update the contents of database through tables. Use transactions / BDCs / BAPIso update database table entries.

Check this sample code

SELECT MATNR MAKTX

FROM MAKT

INTO TABLE IT_MAKT.

LOOP AT IT_MAKT.

CONCATENATE 'New' IT_MAKT-MAKTX TO IT_MAKT-MAKTX.

MODIFY IT_MAKT INDEX SY-TABIX.

ENDLOOP.

MODIFY MAKT FROM TABLE IT_MAKT.

Thanks,

Vinay

Former Member
0 Kudos

<b>Inserting or Changing Single Lines (database table)</b>

MODIFY <target> FROM <wa> .

here target is database table name and wa is the workarea.

above logic you can use in the loop.... endloop.

this logic is outside loop at a once if you want to update.

<b>Inserting or Changing Several Lines (database table)</b>

MODIFY <target> FROM TABLE <itab> .

Those lines of the internal table <itab> for which there is not already a line in the database table with the same primary key are inserted into the table. Those lines of the internal table <itab> for which there is already a line in the database table with the same primary key overwrite the existing line in the database table. The same rules apply to the line type of <itab> as to the work area <wa> described above.

regds,

kiran

Former Member
0 Kudos

Hi,

Updating a database table & for perform use the below statements.

For example table : mara.

Data : z_mara type mara.

Loop at itab.

Clear z_mara.

z_mara-mtart = itab-mtart. "For example i want to update material type

modify table mara from z_mara

transporting mtart

where matnr = itab-matnr.

If sy-subrc = 0.

commit work.

endif.

Endloop.

Above statements will workout as performance wisealso.

Reward points, if helpful.

Kannu.

Former Member
0 Kudos

HI,

Follow the below procedure...

<b>1.</b> Check whether the user is authorized to update the table or not.

To check the authorization of the user of an ABAP program, use the AUTHORITY-CHECK statement:

AUTHORITY-CHECK OBJECT '<object>'

ID '<name1>' FIELD <f1>

ID '<name2>' FIELD <f2>

.............

ID '<name10>' FIELD <f10>.

<object> is the name of the object that you want to check. You must list the names (<name1>, <name2> ...) of all authorization fields that occur in <object>. You can enter the values <f 1 >, <f 2 >.... for which the authorization is to be checked either as variables or as literals. The AUTHORITY-CHECK statement checks the user’s profile for the listed object, to see whether the user has authorization for all values of <f>. Then, and only then, is SY-SUBRC set to 0. You can avoid checking a field by replacing FIELD <f> with DUMMY. You can only evaluate the result of the authorization check by checking the contents of SY-SUBRC. For a list of the possible return values and further information, see the keyword documentation for the AUTHORITY-CHECK statement. For further general information about the SAP authorization concept, refer to Users and Authorizations.

http://help.sap.com/saphelp_47x200/helpdata/en/fc/eb3ba5358411d1829f0000e829fbfe/content.htm

<b>2.</b> If the User is authorized, then Lock the table that you want to update. Check the link below to know more about Locking...

http://help.sap.com/saphelp_47x200/helpdata/en/41/7af4c5a79e11d1950f0000e82de14a/content.htm

<b>3.</b> Use the MODIFY or INSERT COMMANDS to update the database table from an internal table.

<b>Inserting Several Lines</b>

To insert several lines into a database table, use the following:

INSERT <target> FROM TABLE <itab> [ACCEPTING DUPLICATE KEYS] .

This writes all lines of the internal table <itab> to the database table in one single operation. The same rules apply to the line type of <itab> as to the work area <wa> described above.

http://help.sap.com/saphelp_47x200/helpdata/en/fc/eb3a6d358411d1829f0000e829fbfe/content.htm

<b>MODIFY lines.</b>

To insert lines into a database table regardless of whether there is already a line in the table with the same primary key, use the following:

MODIFY <target> <lines>.

If the database table contains no line with the same primary key as the line to be inserted, MODIFY works like INSERT, that is, the line is added.

If the database already contains a line with the same primary key as the line to be inserted, MODIFY works like UPDATE, that is, the line is changed.

For performance reasons, you should use MODIFY only if you cannot distinguish between these two options in your ABAP program.

You can add or change one or more lines <lines> in a database table <target>. You can only insert or change lines in an ABAP Dictionary view if it only contains fields from one table, and its maintenance status is defined as Read and change. You may specify the database table <target> either statically or dynamically.

http://help.sap.com/saphelp_47x200/helpdata/en/fc/eb3ac8358411d1829f0000e829fbfe/content.htm

<b>4.</b> Once the database update is successful, then COMMIT the database. Use Open SQL contains the statements

COMMIT WORK.

and

ROLLBACK WORK.

for confirming or undoing database updates. COMMIT WORK always concludes a database LUW and starts a new one. ROLLBACK WORK always undoes all changes back to the start of the database LUW.

http://help.sap.com/saphelp_47x200/helpdata/en/fc/eb3b64358411d1829f0000e829fbfe/content.htm

<b>5.</b> Unlock the database table.

I hope this gives a clear picture of how to program a database updates.

Regards,

Vara

Former Member
0 Kudos

Hi Karthik,

You can use a modify statement.Make sure the internal table and the database table are of the same type.

modify basetable from table itab.

Here itab is the internal table and basetable is the database table.

Pls reward points if found useful.

Thanks

Shyam

Former Member
0 Kudos

Hi,

Check the following code:

IT_GE-MBLNR = IMBLNR.

IT_GE-MJAHR = IMJAHR.

IT_GE-UNAME = SY-UNAME.

IT_GE-NAME_TEXTC = VNAME.

IT_GE-BUDAT1 = SY-DATUM.

IT_GE-ETIME = SY-UZEIT.

IT_GE-MACHINE_ID = VTERMINAL.

IT_GE-WERKS = VWERKS.

APPEND IT_GE.

INSERT ZGEXIT FROM TABLE IT_GE ACCEPTING DUPLICATE KEYS.

Hope this helps.

Reward if helpful.

Regards,

Sipra

Former Member
0 Kudos

Hi,

try like this:

data : itab like table of mara.

data : wa type mara.

select * from mara into corresponding fields of table itab up to 300 rows.

loop at itab into wa.

if sy-tabix = 3.

insert mara from wa.

endif.

endloop.

Regards

Shiva