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

INSERT into database Keyword

Former Member
0 Likes
1,934

Hi Guys,

If I use the INSERT keyword to add new entries to a database table, will it append to the existing entries, or will it overwrite the existing entries...

I need it only to append, so if it does overwrite, which keywork can i use?

tks

C

Points will be awarded

1 ACCEPTED SOLUTION
Read only

sachin_mathapati
Contributor
0 Likes
1,357

Insert :

Adds new record to database table..

This will over write if already existing.

Instead use Modify..

Modify :

Inserts new lines or updates existing lines in a database table

9 REPLIES 9
Read only

sachin_mathapati
Contributor
0 Likes
1,358

Insert :

Adds new record to database table..

This will over write if already existing.

Instead use Modify..

Modify :

Inserts new lines or updates existing lines in a database table

Read only

former_member787646
Contributor
0 Likes
1,357

Hi Christiaan,

The "INSERT" statement will always inserts a new record in

the Database.

"UPDATE" statement is used to overwrite the details

of the existing record.

(See MODIFY Statement also.)

Hope it helps you.

Murthy.

Read only

soumya_jose3
Active Contributor
0 Likes
1,357

Hi,

INSERT will not modify an existing entry, it will only append. To modify there is another keyword MODIFY.

See the below help also:

INSERT { {INTO target VALUES source }

| { target FROM source } }.

Effect

The INSERT statement inserts one or more rows specified in source in the database table specified in target. The two variants with INTO and VALUES or without INTO with FROM behave identically, with the exception that you cannot specify any internal tables in source after VALUES.

System Fields

The INSERT statement sets the values of the system fields sy-subrc and sy-dbcnt.

sy-subrc Meaning

0 At least one row was inserted.

4 At least one row could not be inserted, because the database table already contains a row with the same primary key or a unique secondary index.

Regards,

Soumya.

Read only

Former Member
0 Likes
1,357

Hi Christiaan,

No it won't.

From SAP Help:

The INSERT statement sets the values of the system fields sy-subrc and sy-dbcnt.

sy-subrc Meaning

0 At least one row was inserted.

4 At least one row could not be inserted, because the database table already contains a row with the same primary key or a unique secondary index.

The INSERT statement sets sy-dbcnt to the number of rows inserted.

Thanks,

Victor.

Read only

Former Member
0 Likes
1,357

Hi,

Try this sample coding,

TABLES:MARA.

DATA:ITAB LIKE MARA OCCURS 0 WITH HEADER LINE.

START-OF-SELECTION.

ITAB-MATNR = '123ABCDA'. .

ITAB-MBRSH = 'C'.

ITAB-MTART = 'FERT' .

ITAB-MEINS = 'KG' .

APPEND ITAB.

ITAB-MATNR = '123ABCDB'. .

ITAB-MBRSH = 'C'.

ITAB-MTART = 'FERT' .

ITAB-MEINS = 'KG' .

APPEND ITAB.

LOOP AT ITAB.

INSERT MARA FROM ITAB.

MODIFY MARA .

ENDLOOP.

Reward if helpfull

Thanks & Regards,

Y.R.Prem Kumar

Read only

Former Member
0 Likes
1,357

Hi,

INSERT is used to append..

MODIFY will change the existing enties...

Regards,

Sai

Read only

rainer_hbenthal
Active Contributor
0 Likes
1,357

Insert will only and only insert,

update will only update,

modify is trying to insert, if this will fail, it will update.

Read only

Former Member
0 Likes
1,357

Hi,

We can use modify statement so that even if in the case of for appending a new line or updating a existing line , the same command can work. So Modify best suites your requirement.

Answer to your question is that INSERT only appends a new line but donot update existing line.

Modify :

Inserts new lines or updates existing lines in a database table (s. relational database). If a line with the specified primary key already exists, an UPDATE is executed. Otherwise, an INSERT is performed.

The system field SY-DBCNT contains the number of lines processed after the call of the statement.

The Return Code is set as follows :

SY-SUBRC = 0 :

All lines were successfully inserted or updated.

SY-SUBRC = 4 :

One or more lines could not be inserted or updated.

Hope this clarifies your doubt.

plz reward if useful.

thanks,

dhanashri.

Read only

Former Member
0 Likes
1,357

hi,

INSERT command is used to APPEND the values .

for EXAMPLE :

Data: Begin of line,

col1 type I,

col2 type I,

End of line.

Data itab like line occurs 10.

Do 2 times.

Line-col1 = sy-index.

Line-col2 = sy-index ** 2.

Append line to itab.

Enddo

.

Line-col1 = 11.

Line-col2 = 22.

Insert line into itab index 2.

Insert initial line into itab index 1.

Loop at itab into line.

Write: / sy-tabix, Line-col1, Line-col2.

Endloop.

EXPLANATION :

Let us consider the example. Here the internal table ITAB is defined to have the structure of line and does not have a Header line.

In the DO LOOP, the columns of the structure are assigned the values of SY-INDEX and its square. SY-INDEX is another system variable which has the value of the loop iteration. At the end of the loop the table ITAB will have following contents:

1 1

2 4

Next we are trying to insert the line with values 11 and 22 at index 2.

1 1

11 22

2 4

The next line inserts an initial line at index 1. So, the table contents now become

0 0

1 1

11 22

2 4