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 command will work on DATABASE tables..

Former Member
0 Kudos
128

Hi experts ...

Can we insert some field values with reference to primary key of database table.

Will insert command work on database table.

Regds

Rajasekhar.

3 REPLIES 3

Former Member
0 Kudos
102

Hi,

YES --> we insert some field values with reference to primary key of database table

Yes --> Insert command work on database table

Regards

Sudheer

former_member235056
Active Contributor
0 Kudos
102

Hi,

<b>Inserting Lines into Tables</b>

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 targeteither 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.

<b>Inserting a Single Line</b>

To insert a single line into a database table, use the following:

INSERT INTO target VALUES wa.

The contents of the work area wa are written to the database table dbtab. It is a good idea to define the work area with reference to the structure of the database table.

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 INTOclause. Shorter still is:

INSERT dbtab.

In this case, the contents of the table work area dbtab are inserted into the database table with the same name. You must declare this table work area using the TABLES statement. In this case, it is not possible to specify the name of the database table dynamically. Table work areas with the same name as the database table (necessary before Release 4.0) should no longer be used for the sake of clarity.

<b>Inserting Several Lines</b>

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

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.

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 itaband inserts its contents into the database table SPFLI. The program examines the contents of sy-subrc to see if the operation was successful.

These are the possible uses of insert in abap programs.

Pls reward all helpful points.

regards,

Ameet

Former Member
0 Kudos
102

Yes insert Command will work ...values with reference to primary key of database table.

otherwise it will give you the error if the data is not there in the Primary key of the Check table .

<b>Example :</b>

Inserting a new airline company in the database table SCARR.


TABLE : SCARR .

DATA scarr_wa TYPE scarr. 

scarr_wa-carrid   = 'FF'. 
scarr_wa-carrname = 'Funny Flyers'. 
scarr_wa-currcode = 'EUR'. 
scarr_wa-url      = 'http://www.funnyfly.com'. 

INSERT INTO scarr VALUES scarr_wa.  "  See the new data from  work area  to  database table scarr  inserting 

reward points if it is usefull...

Girish