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: 

Fill database table from internal table

Former Member
0 Kudos

I made one table ZDISP_CHDOC_CC and want to fill that table from internal table.and i got runtime error. that duplicate entry

and two entry are like 10 200000 likhp 10

10 200000 likp 10

DESCRIBE TABLE IT_CHDOC .

LOOP AT IT_CHDOC.

INSERT ZDISP_CHDOC_CC FROM IT_CHDOC.

endloop.

IF SY-SUBRC = 0.

COMMIT WORK.

ELSE.

and when i used following then only one entry is insreted.

DESCRIBE TABLE IT_CHDOC .

LOOP AT IT_CHDOC.

INSERT ZDISP_CHDOC_CC FROM IT_CHDOC.

IF SY-SUBRC = 0.

COMMIT WORK.

endloop.

ELSE.

1 ACCEPTED SOLUTION

faisal_altaf2
Active Contributor
0 Kudos

Hi, Kjain

Please Check for you table Key Fields i think you are trying to INSERT the Duplicate records mean with the same Key Fields Values for both Records,

So it is giving message, Check and Reply if any Else Issue,

Best Regards,

Faisal

10 REPLIES 10

faisal_altaf2
Active Contributor
0 Kudos

Hi, Kjain

Please Check for you table Key Fields i think you are trying to INSERT the Duplicate records mean with the same Key Fields Values for both Records,

So it is giving message, Check and Reply if any Else Issue,

Best Regards,

Faisal

0 Kudos

thanks for ur helpfull reply

Ya u r right..My field is cost center is key field..in my ztable but i want to insert both record.because two and more filed are different in each row except cost center.so please give me solution

Edited by: priyank kjain on Aug 22, 2009 6:20 PM

0 Kudos

>

> thanks for ur helpfull reply

>

> Ya u r right..My field is cost center is key field..

You mean to say that you have only one Key Field Which is Cost Center if yes than you must Add some more Key Field according to your Requirements.

Remember that the combination of all Key Fields Must be Unique

Regards,

Faisal

0 Kudos

hi,

in a database table there cant be tow entries with same primary key(s).

so first in addition to profit center add one more field as primary key,say line number or any field which you know that could make combination for primary key.

0 Kudos

Hi thanks for ur soluction. i have one more problem. supose I run my program then it will inserted all data into custom table with respect to select condition . again i run with same condition then it willl go in dump. i want it will check in custom table and after give the message that 'Record is Already inserted'.

0 Kudos

Hi,

for that situation you can do it this way....

DESCRIBE TABLE IT_CHDOC .
LOOP AT IT_CHDOC.
  SELECT * FROM ZDISP_CHDOC_CC INTO IT_CHDOC
         WHERE KEY_FIELD1 = IT_CHDOC-KEYFIELD1 
             AND KEY_FIELD2 = IT_CHDOC-KEYFIELD2 
             "AND SO ON... IF THERE ARE MORE THAN ONE KEYFIELDS IN THE CUSTOM TABLE
.
IF SY-SUBRC = 0.
  WRITE : / IT_CHDOC-KEYFIELD, ' Already Exists'.
ELSE.
  INSERT ZDISP_CHDOC_CC FROM IT_CHDOC.
ENDIF.
ENDLOOP.
COMMIT WORK.

REGARDS,

Siddarth

former_member201227
Active Participant
0 Kudos

Hi,

Please check the key fields of your table. I think the fields representing 10 and 200000 are your key fields.

It is not possible to use the same set of key fields in a table. Since you are providing redundant data you end up in error.

The first case gives you a run time error, because you are checking the value of sy-subrc at the end of all inserts and committing your job. This checks only for the last row of the internal table and inserts all the rows and was not able to

do so, as there was a duplicate entry.

Whereas the second case checks each and every INSERT inside the loop. Sy-subrc will not be 0 when the duplicate entry is encountered and hence that was not inserted to the table.

Best Regards,

Sharmila

Former Member
0 Kudos

This message was moderated.

Former Member
0 Kudos

Hi,

Replace your current code

DESCRIBE TABLE IT_CHDOC .
LOOP AT IT_CHDOC.
INSERT ZDISP_CHDOC_CC FROM IT_CHDOC.
endloop.
IF SY-SUBRC = 0.
COMMIT WORK.
ELSE.

WITH THE ONE GIVEN BELOW

DESCRIBE TABLE IT_CHDOC .
INSERT ZDISP_CHDOC_CC FROM TABLE IT_CHDOC ACCEPTING DUPLICATE KEYS.
IF SY-SUBRC = 0.
COMMIT WORK.
ELSE.

Regards,

Siddarth

Former Member
0 Kudos

hi priyank,

If the data already exists in the db table then u go for update n u need to give the primary key wher u want it to update the kna1 table..

ellse u need to use INsert...

what error r u getting exactly...

Use the abap code:

LOOP AT ITAB INTO WA.
MODIFY ZTABLE FROM WA.
IF SY-SUBRC NE 0.
Move wa to wa1.
append itab1 from wa1.
endif.
clear wa.
endloop.

-Thanks & Regards

Saurabh Goel