What would be the best way to insert records with ABAP to a custom table

842 Views Asked by At

I have a table in SAP called ZDT_COUNTRY with the following fields:

  • MANDT (type: NUMC)
  • ID_PAIS (type: NUMC)
  • NAME_PAIS (type: CHAR)
  • CAPITAL_PAIS (type: CHAR)
  • POBLACION_PAIS (type: NUMC)
  • SURFACE_PAIS (type: NUMC)
  • CURRENCY_COUNTRY (type: CHAR)
  • LANGUAGE_COUNTRY (type: CHAR)

I want to create a method in a transaction SE38 that allows me to insert at least 8 records into this table. Both MANDT and ID_PAIS are the key fields.

I've written the following code, but when I press F8 to execute it, I get the error: DBSQL_DUPLICATE_KEY_ERROR:

REPORT Z_INSERT_COUNTRY.

DATA: lt_countries TYPE TABLE OF ZDT_COUNTRY,
      ls_country   TYPE ZDT_COUNTRY.

ls_country-MANDT = '001'.
ls_country-ID_PAIS = '001'.
ls_country-NAME_PAIS = 'United States'.
ls_country-CAPITAL_PAIS = 'Washington D.C.'.
ls_country-POBLACION_PAIS = '339996567'.
ls_country-SURFACE_PAIS = '9629091'.
ls_country-CURRENCY_COUNTRY = 'US Dollar'.
ls_country-LANGUAGE_COUNTRY = 'English'.
APPEND ls_country TO lt_countries.

CLEAR ls_country.
ls_country-MANDT = '002'.
ls_country-ID_PAIS = '002'.
ls_country-NAME_PAIS = 'Canada'.
ls_country-CAPITAL_PAIS = 'Ottawa'.
ls_country-POBLACION_PAIS = '39566248'.
ls_country-SURFACE_PAIS = '9976140'.
ls_country-CURRENCY_COUNTRY = 'Canadian Dollar'.
ls_country-LANGUAGE_COUNTRY = 'English, French'.
APPEND ls_country TO lt_countries.

CLEAR ls_country.
ls_country-MANDT = '003'.
ls_country-ID_PAIS = '003'.
ls_country-NAME_PAIS = 'Australia'.
ls_country-CAPITAL_PAIS = 'Canberra'.
ls_country-POBLACION_PAIS = '25767000'.
ls_country-SURFACE_PAIS = '7692024'.
ls_country-CURRENCY_COUNTRY = 'Australian Dollar'.
ls_country-LANGUAGE_COUNTRY = 'English'.
APPEND ls_country TO lt_countries.

CLEAR ls_country.
ls_country-MANDT = '004'.
ls_country-ID_PAIS = '004'.
ls_country-NAME_PAIS = 'Brazil'.
ls_country-CAPITAL_PAIS = 'Brasilia'.
ls_country-POBLACION_PAIS = '218596943'.
ls_country-SURFACE_PAIS = '8515767'.
ls_country-CURRENCY_COUNTRY = 'Brazilian Real'.
ls_country-LANGUAGE_COUNTRY = 'Portuguese'.
APPEND ls_country TO lt_countries.

CLEAR ls_country.
ls_country-MANDT = '005'.
ls_country-ID_PAIS = '005'.
ls_country-NAME_PAIS = 'India'.
ls_country-CAPITAL_PAIS = 'New Delhi'.
ls_country-POBLACION_PAIS = '1425775850'.
ls_country-SURFACE_PAIS = '3287263'.
ls_country-CURRENCY_COUNTRY = 'Indian Rupee'.
ls_country-LANGUAGE_COUNTRY = 'Hindi, English'.
APPEND ls_country TO lt_countries.

CLEAR ls_country.
ls_country-MANDT = '006'.
ls_country-ID_PAIS = '006'.
ls_country-NAME_PAIS = 'Germany'.
ls_country-CAPITAL_PAIS = 'Berlin'.
ls_country-POBLACION_PAIS = '83019200'.
ls_country-S

2

There are 2 best solutions below

0
AlexSchell On

Actually you can use any syntax you want - this is one time operation only with just a few records so performance / efficiency is not a concern in this case.

The error says that you try to insert records which have the same values for key fields of the table. Please check your the data once more, check what are the actual table key fields and make sure that the values of key fields for all records are unique (the combination of values of the key fields should be unique).

You can also try to insert records one a time instead of adding them all into an internal table before and you will see on which record exactly the code crashes. And you can use debugging possibilities of course.

Do not fill the MANDT field - it would be automatically handled by SAP (OpenSQL engine) for you with the current client value of the system you executing this code on and it will be the same for all your records.

0
Frontmaniaac On

DBSQL_DUPLICATE_KEY_ERROR indicates that you are inserting entries with the same key. This probably means that you have already records with these key values MANDT and ID_PAIS in your table.

I assume that you have run your code, and had run it once more, which tries to insert the same records again, which in this case is not allowed, to have entires with the same key values for a Standard Table.

You can view all the records by using transaction SE16N or SE16N and selecting your table ZDT_COUNTRY.

Or do SELECT * FROM ZDT_COUNTRY INTO TABLE @DATA(lt_country). , create a break-point to see what data is inside if you should not have permission to use SE16n.