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: 

Insert

Former Member
0 Kudos

Can we insert directly in a databse table in a report?

e.g. Insert into EKPO...

IS this Advisable...Instead we go for BAPI or BDC ?

regards

Message was edited by:

Madan Gopal Sharma

Message was edited by:

Madan Gopal Sharma

5 REPLIES 5

Former Member
0 Kudos

Yes you can. Press F1 on Insert command you will get the code.

Check this from SAP help.

Syntax Diagram

INSERT

Insert into a database table

- INSERT INTO dbtab VALUES wa.or

INSERT INTO (dbtabname) VALUES wa. or

INSERT dbtab FROM wa. or

INSERT (dbtabname) FROM wa.

- INSERT dbtab FROM TABLE itab. or

INSERT (dbtabname) FROM TABLE itab.

- INSERT dbtab. or

INSERT *dbtab.

Former Member
0 Kudos

Hi

Better to use Modify statement

Fetch the data into ITAB

then use Modify ekpo from table ITAB.

Reward points for useful Answers

Regards

Anji

Former Member
0 Kudos

hi

yes u can

insert into ztable ....

after the query dont forget to write

<b>MODIFY <DATABASE TABLE></b>

regards

ravish

reward if useful

Former Member
0 Kudos

Hi,

Yes U can

INSERT - Inserting Data in Database Tables

Variants:

1. INSERT INTO dbtab [CLIENT SPECIFIED] VALUES wa.

INSERT INTO (dbtabname) [CLIENT SPECIFIED] VALUES wa.

2. INSERT dbtab [CLIENT SPECIFIED] FROM TABLE itab. oder

INSERT (dbtabname) [CLIENT SPECIFIED] FROM TABLE itab.

3. INSERT dbtab [CLIENT SPECIFIED]. oder

INSERT *dbtab [CLIENT SPECIFIED]. oder

INSERT (dbtabname) [CLIENT SPECIFIED] ... .

Effect

Adds new records to a database table (see relational database). You can specify the name of the database table either directly in the program in the form dbtab or at runtime as the contents of the field dbtabname. In either case, the database table must be declared in the ABAP Dictionary. You can only insert data using a view if the view refers to a single table and has the maintenance status "No restriction" in the ABAP Dictionary.

By default, data is only inserted in the current client. However, if you use the CLIENT SPECIFIED addition, you can switch off the automatic client handling. This enables you to enter data for any client in a cross-client table, not just in the client in which you are logged on. In this case, the client field is treated like a normal field to which you can assign a value in the work area.

INSERT is an OPEN SQL statement.

Notes

You cannot insert a table line if the table already contains a record with the same primary key or an identical set of key values for a UNIQUEindex.

When you insert records using a views, all fields of the database table that are not contained in the view are filled with their initial value ( TABLES) if they were created with the NOT NULL option in the ABAP Dictionary. Otherwise, they are filled with NULL values.

The INSERT statement does not perform any authorization checks. You should instead implement your own at program level.

When you use the INSERT statement, the data is not written firmly to the database until a database commit occurs (see LUW). Before this, you can use a database rollback to undo any changes (see Programming Transactions).

It is not possible to synchronize simultaneous access to the same data by several users using the lock mechanism of the database system alone. You will therefore often need to use the SAP locking system.

Variant 1

INSERT INTO dbtab [CLIENT SPECIFIED] VALUES wa. oder

INSERT INTO (dbtabname) [CLIENT SPECIFIED] VALUES wa.

Effect

Inserts one line into a database table. The new line that you want to insert is taken from the work area wa. The data is read from wa from left to right accordinng to the structure of the table work area dbtab(TABLES). The structure of wa is not considered. Consequently, the work area wa must be at least as wide (see DATA) as the table work area dbtab and its alignment must correspond to that of the table work area, otherwise a runtime error will occur.

After the statement has been executed, the system field SY-DBCNT contains the number of lines that have been inserted (0 or 1).

The return code is set as follows:

SY-SUBRC = 0:

Line inserted.

SY-SUBRC = 4:

The line could not be inserted. A line with the same key already existed.

Example

Adding customer "Robinson" in the current client:

TABLES SCUSTOM.

SCUSTOM-ID = '12400177'.

SCUSTOM-NAME = 'Robinson'.

SCUSTOM-POSTCODE = '69542'.

SCUSTOM-CITY = 'Heidelberg'.

SCUSTOM-CUSTTYPE = 'P'.

SCUSTOM-DISCOUNT = '003'.

SCUSTOM-TELEPHONE = '01234/56789'.

INSERT INTO SCUSTOM VALUES SCUSTOM.

Example

Adding customer "Robinson" to client 002:

TABLES SCUSTOM.

SCUSTOM-MANDT = '002'.

SCUSTOM-ID = '12400177'.

SCUSTOM-NAME = 'Robinson'.

SCUSTOM-POSTCODE = '69542'.

SCUSTOM-CITY = 'Heidelberg'.

SCUSTOM-CUSTTYPE = 'P'.

SCUSTOM-DISCOUNT = '003'.

SCUSTOM-TELEPHONE = '01234/56789'.

INSERT INTO SCUSTOM CLIENT SPECIFIED VALUES SCUSTOM.

Variant 2

INSERT dbtab [CLIENT SPECIFIED]. oder INSERT *dbtab [CLIENT SPECIFIED]. oder

INSERT (dbtabname) [CLIENT SPECIFIED] ... .

Addition: ... FROM wa

In an ABAP Objects context, a more severe syntax check is performed that in other ABAP areas. See Short forms not allowed and * work areas not allowed.

Effect

These are SAP-specific short forms for the statements introduced under variant 1.

INSERT INTO dbtab VALUES dbtab. bzw.

INSERT INTO dbtab VALUES *dbtab. bzw.

INSERT INTO (dbtabname) VALUES wa.

When you specify the name of the database table directly, the program must contain a corresponding TABLES statement.

If you specify the database table at runtime, you must use the ... FROM wa addition. After the statement has been executed, the system field SY-DBCNT contains the number of records inserted (0 or 1).

The return code is set as follows:

SY-SUBRC = 0:

Line inserted.

SY-SUBRC = 4:

Line could not be inserted, since the table already contains a line with the same key.

Example

Adding a record to a database table:

TABLES SAIRPORT.

SAIRPORT-ID = 'NEW'.

SAIRPORT-NAME = 'NEWPORT APT'.

INSERT SAIRPORT.

Addition

... FROM wa

Effect

The values for the lines to be inserted are taken from the specified work area wa, not from the table work area dbtab. The work area wa must satisfy the conditions specified under variant 1. In this addition, you can specify the name of the database table either directly or indirectly, as specified in variant 1.

Note

If you do not specify an explicit work area, the values for the line to be inserted are still taken from the table work area dbtab if the statement occurs in a FORM or FUNCTION in which the table work area is obscured by a formal parameter or local variable with the same name.

Variant 3

INSERT dbtab [CLIENT SPECIFIED] FROM TABLE itab. or INSERT (dbtabname) [CLIENT SPECIFIED] FROM TABLE itab.

Addition: ... ACCEPTING DUPLICATE KEYS

Effect

Mass insert: Inserts all lines of table itab in a single operation. The lines of itab must satisfy the same condition as the work area wa in variant 1.

After the statement has been executed, the system field SY-DBCNT contains the number of lines inserted.

The return code is set as follows:

SY-SUBRC = 0:

All lines inserted successfully. Any other situation causes a runtime error.

Note

If the internal table itab is empty, SY-SUBRC and SY-DBCNT are both 0 after the call.

Addition

... ACCEPTING DUPLICATE KEYS

Effect

If the system cannot insert a line, the program does not terminate with a runtime error. Instead, SY-SUBRC is set to 4. All lines of the internal table after the one that could not be inserted are still inserted into the database table.

reagards,

Nandha

Reward if it helps

Former Member
0 Kudos

hi madan,

it is definitely possible to insert data into database table. but you need to take care of locking and unlocking the table before and after usage.

Create a lock object from "se11" and that would create "enqueue_<lockobjectname>" and "dequeue_<lockobjectname>" function modules.

use both the functionmodules before and after inserting values to your database tables.

<b>Reward points if this was helpful</b>

Kiran