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: 

how to update database table from Internal Table

Former Member

hi experts,

Can anyone please assist me in inserting records to a database table from an Internal Table

whose structures are identical.

Thanks in Advance,

Sudhaa............

21 REPLIES 21

Former Member
0 Kudos

Hi,

You can use MODIFY,INSERT statements. Goto tcode ABAPDOCU and enter the above keywords to get the exact syntax.

For example:

Modify <database table> from table <itab>.

Thanks & Regards,

Navneeth K.

Former Member
0 Kudos

Hi,

You can find the syntax to do so in F1 help provided by SAP, just type update in ABAP editer and press F1 you will get the required info in detail.

Former Member
0 Kudos

INSERT dbtab FROM TABLE itab.

UPDATE dbtab FROM TABLE itab.

MODIFY dbtab FROM TABLE itab.

Former Member
0 Kudos

modify <DBtable> from <int.tab>

if you use update

you need to provide the primary key also

former_member598013
Active Contributor
0 Kudos

Hi Sudha,

Below is the sample code.



DATA scarr_wa TYPE scarr. 

scarr_wa-carrid   = 'FF'. 
scarr_wa-carrname = 'ABC Ltd'. 
scarr_wa-currcode = 'EUR'. 
scarr_wa-url      = 'http://www.abcltd.com'.. 

INSERT  INTO scarr VALUES scarr_wa. 

Thanks,

Chidanand

Former Member
0 Kudos

Hi you write as

modify <DBtable> from table <INTernal.tab>.

Former Member

Hello,

Better you use MODIFY statement instead of going INSERT or UPDATE.

If u use Modify if the records exists it will modify the row.

MODIFY i_tab INDEX idx FROM i_wa

TRANSPORTING currcode.

MODIFY sflight_tab FROM sflight_wa

TRANSPORTING planetype WHERE planetype = p_plane1.

If u want to modify complete row dont use Transporting key word.

Former Member
0 Kudos

Hi sudha,

It is not recommended to update a sap database table

Even if u want u can use something like this----

select *

from dbtab

into table itab.

loop at itab.

itab-field = <new_value>.

modify itab index sy-tabix.

endloop.

UPDATE dbtab FROM TABLE itab.

use update command.

regards

Prashant.

former_member585060
Active Contributor
0 Kudos

Hi,

use this syntax

MODIFY <ztable> FROM TABLE <it_itab>.

Regards

Bala Krishna

Former Member
0 Kudos

HI ,

You can use insert and modify statements to update the database table .

Ex:

Modify <dbtbname> from <itab>.

insert into <dbtabname > values <wa_itab> (where wa_itab have the values which is to be inserted)

Ex:

data: wa_vbak type vbak.

wa_vbak-vkorg = '11'

Now you can do like this:

insert into vbak values wa_vbak '' here you have to take care about primay key ''

or you can use :

insert <dbtabname> from <tbname>

Hope this will help you .

Bye

Former Member
0 Kudos

Hi Sudha,

Here are some example of update and insert:

UPDATE SFLIGHT SET PLANETYPE = 'A310'

PRICE = PRICE - '100.00'

WHERE CARRID = 'LH' AND CONNID = '0402'.

This example overwrites the contents of the PLANETYPE column with A310 and decreases the value of the PRICE column by 100 for each entry in SFLIGHT where CARRID contains u2018LHu2019 and CONNID contains u2018402u2019.

TABLES SPFLI.

DATA WA TYPE SPFLI.

MOVE 'AA' TO WA-CARRID.

MOVE '0064' TO WA-CONNID.

MOVE 'WASHINGTON' TO WA-CITYFROM.

...

UPDATE SPFLI FROM WA.

MOVE 'LH' TO SPFLI-CARRID.

MOVE '0017' TO SPFLI-CONNID.

MOVE 'BERLIN' TO SPFLI-CITYFROM.

...

UPDATE SPFLI.

CARRID and CONNID are the primary key fields of table SPFLI. All fields of those lines where the primary key fields are "AA" and "0064", or "LH" and "0017", are replaced by the values in the corresponding fields of the work area WA or the table work area SPFLI.

DATA: ITAB TYPE HASHED TABLE OF SPFLI

WITH UNIQUE KEY CARRID CONNID,

WA LIKE LINE OF ITAB.

WA-CARRID = 'UA'. WA-CONNID = '0011'. WA-CITYFROM = ...

INSERT WA INTO TABLE ITAB.

WA-CARRID = 'LH'. WA-CONNID = '1245'. WA-CITYFROM = ...

INSERT WA INTO TABLE ITAB.

WA-CARRID = 'AA'. WA-CONNID = '4574'. WA-CITYFROM = ...

INSERT WA INTO TABLE ITAB.

...

UPDATE SPFLI FROM TABLE ITAB.

This example fills a hashed table ITAB and then overwrites the lines in SPFLI that have the same primary key (CARRID and CONNID) as a line in the internal table.

Insert statement :

TABLES SPFLI.

DATA WA TYPE SPFLI.

WA-CARRID = 'LH'.

WA-CITYFROM = 'WASHINGTON'.

...

INSERT INTO SPFLI VALUES WA.

WA-CARRID = 'UA'.

WA-CITYFROM = 'LONDON'.

...

INSERT SPFLI FROM WA.

SPFLI-CARRID = 'LH'.

SPFLI-CITYFROM = 'BERLIN'.

...

INSERT SPFLI.

If the database table does not already contain a line with the same primary key as specified in the work area, the operation is completed successfully and SY-SUBRC is set to 0. Otherwise, the line is not inserted, and SY-SUBRC is set to 4.

Regards,

Premraj kaushik

0 Kudos

thank you all for your quick responses

0 Kudos

Hi Prem,

For such type of scenario,I have a doubt regarding modifying a database table.

I would like to add the line items from an internal table to a data base table with the content of the primary key as same.

For example:

If I specify a correspondence number in table ZU1CD_CORRPOS,I would like to have 3 or multi line items for that correspondence number.

In such a case,I should insert 3or many rows from internal table with the value of the primary key as the same but I should insert multi line items.

I have tried issuing Modiy Insert zu1cd_corrpos from table it_corr accepting duplicate keys..

But now I could see the first entry of the internal table and the rest of the entries are not inserted into DB.

Please help me.

Thanks,

Johny

ritesh_inamdar
Explorer
0 Kudos

This message was moderated.

ssai3973
Explorer
0 Kudos

Always Use Lock Objects While Updating the records of a table.
That will Consider as best Practice.

Regards,

Satish.

Jelena
Active Contributor
0 Kudos

This question is from 2008. Please take note of the date before chiming in with an answer and consider if an answer adds any value at this point.

0 Kudos

Hi Jelena,

Yes i saw the date, as I am a fresher and searching for the same doubt before 2 weeks ,and got the solution as mentioned above.

Thinking it may useful for freshers like me searching for the same question I answered 😊.

Regards,

Satish.

former_member633317
Discoverer
0 Kudos

Update with below code.

MODIFY <DB_tab> FROM TABLE <lt>.

This question is from 2008! And it's not even the right answer, the question is about INSERT.

Such questions can be easily answered by reading ABAP Help, which has been improved tremendously since 2008.

Jelena
Active Contributor

Not sure why people keep posting answers to this RTFM question every 5 years. Please stop doing that. If anyone still has a similar question - go to ABAPDOCU transaction and read article about INSERT command. No access to the system? Google "ABAP keyword documentation". The answer is literally in the command syntax.

Still have a question? Post your own.

Yearning to share knowledge? Post a blog.

Please no more necromancing.

Sandra_Rossi
Active Contributor
0 Kudos

Jelena Perfiljeva I don't understand why the SCN team has "opened the archive". That was nice a few months ago when "archive.sap.com" was closed to answers and comments, and "answers.sap.com" used to contain only new questions.