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 Records in Database table

Former Member
0 Likes
3,564

Hi,

I am trying to insert values in table

loop at it_header.

INSERT YTAB from table it_header accepting duplicate keys.

endloop.

But, the records are not getting inserted.

Pl guide.

Regards,

Netra

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,351

Additionally, you're code will try to insert the table multiple times

Instead of this


loop at it_header.
  INSERT YTAB from table it_header accepting duplicate keys.
endloop.

you only need this


  INSERT YTAB from table it_header accepting duplicate keys.

6 REPLIES 6
Read only

Former Member
0 Likes
1,351

hi,

If, for one or more of the rows to be inserted, a row with the same primary key or the same unique secondary key already exists and the ACCEPTING DUPLICATE KEYS addition is specified, the rows are not inserted.

regards,

madhu

Read only

Former Member
0 Likes
1,351

hi check this...

When you specify an internal table itab, several rows are created from its contents for insertion into the database table. A row for insertion into the database table is take from each row of the internal table, using the same rules as for an individual work area wa. The row type of the internal table must meet the prerequisites for use in Open SQL statements.

If a row with the same primary key or the same unique secondary key 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 actually inserted.

If, for one or more of the rows to be inserted, a row with the same primary key or the same unique secondary key already exists and the ACCEPTING DUPLICATE KEYS addition is specified, the rows are not inserted and sy-subrc is set to 4. If, in this case, the addition ACCEPTING DUPLICATE KEYS is not specified, no rows are inserted. Prior to Release 6.10, this raises an exception that cannot be handled; as of Release 6.10, this raises the exception that can be handled CX_SY_OPEN_SQL_DB.

regards,

venkat

Read only

Former Member
0 Likes
1,352

Additionally, you're code will try to insert the table multiple times

Instead of this


loop at it_header.
  INSERT YTAB from table it_header accepting duplicate keys.
endloop.

you only need this


  INSERT YTAB from table it_header accepting duplicate keys.

Read only

Former Member
0 Likes
1,351

If you are inserting the it_header (internal table) records to your YTAB (DB table)....then follow the code...

INSERT YTAB from table it_header accepting duplicate keys.

No need for Loop...endloop.

-


If you are inserting the it_header (internal table) records to your YTAB (internal table)....then follow the code...

INSERT lines of it_header INTO TABLE ytab.

Please refer this link:

http://help.sap.com/saphelp_nw70/helpdata/EN/fc/eb3a6d358411d1829f0000e829fbfe/frameset.htm

Read only

Former Member
0 Likes
1,351

Hi,

Insert YTAB from table it_header ACCEPTING DUPLICATE KEYS.

Reward if helpful.

Read only

Former Member
0 Likes
1,351

Hi

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.

If the system is able to insert all of the lines from the internal table, SY-SUBRC is set to 0. If one

or more lines cannot be inserted because the database already contains a line with the same

primary key, a runtime error occurs. You can prevent the runtime error occurring by using the

addition ACCEPTING DUPLICATE KEYS. In this case, the lines that would otherwise cause

runtime errors are discarded, and SY-SUBRC is set to 4.

The system field SY-DBCNT contains the number of lines inserted into the database table,

regardless of the value in SY-SUBRC.

Whenever you want to insert more than one line into a database table, it is more efficient to work

with an internal table than to insert the lines one by one.

Examples

Adding single lines

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.

This program inserts a single line into the database table SPFLI using each of the

three possible variants of the INSERT statement.

Instead of

INSERT SPFLI

in the last line, you could also use the longer forms

INSERT SPFLI FROM SPFLI

or

INSERT INTO SPFLI VALUES SPFLI

The name SPFLI is therefore not unique.

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.

...

INSERT SPFLI FROM TABLE ITAB ACCEPTING DUPLICATE KEYS.

IF SY-SUBRC = 0.

...

ELSEIF SY-SUBRC = 4.

...

ENDIF.

This example fills a hashed table ITAB and inserts its contents into the database

table SPFLI. The program examines the contents of SY-SUBRC to see if the

operation was successful.