Saturday, April 22, 2017

Insert/Update data into Database Table

Start of Content Area

1. Inserting Lines into Tables 

The Open SQL statement for inserting data into a database table is:
INSERT INTO target lines.
It allows you to insert one or more lines into the database table target. You may specify the database table target either statically or dynamically.

Specifying a Database Table

To specify the database table statically, enter the following for target:
INSERT INTO dbtab [CLIENT SPECIFIED] lines.
where dbtab is the name of a database table defined in the ABAP Dictionary.
To specify the database table dynamically, enter the following for target:
INSERT INTO (name) [CLIENT SPECIFIED] lines.
where the field name contains the name of a database table defined in the ABAP Dictionary.
You can use the CLIENT SPECIFIED addition to disable automatic client handling.

Inserting a Single Line

To insert a single line into a database table, use the following:
INSERT INTO target VALUES wa.
You can also insert single lines using the following shortened form of the INSERT statement:
INSERT target FROM wa.
Using FROM instead of VALUE allows you to omit the INTO clause.

Inserting Several Lines

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 itabto the database table in one single operation. 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.
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

Example
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 INSERTstatement.
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
here. The name SPFLI is therefore not unique.
These variations of the INSERT addition only work with table work areas that have been declared using TABLESand should therefore no longer be used.
Example
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.


2. Updating Lines into Tables 

Change a specific field in an SAP database table 
Changing a values within a database table using the UPDATE command is very useful function to perform. Although if you want to update a number of fields in the table you may want to use the MODIFY command.


data: ld_ebeln type ekko-ebeln.

update EKKO set ERNAM = sy-uname
 where ebeln = ld_ebeln.

Before/After inserting/updating database table, you must lock/unlock database table using lock object (se11) or FM ENQUEUE_E_TABLE/DEQUEUE_E_TABLE


End of Content Area

No comments:

Post a Comment

SAP giới thiệu mã hỗ trợ AI trong ngôn ngữ ABAP riêng của mình

SAP đã ra mắt một loạt tính năng mã hỗ trợ AI trong môi trường phát triển ứng dụng dựa trên đám mây của mình, đồng thời tham gia vào danh sá...