Application Development and Automation 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: 
Read only

Inserting columns from internal table to a database table!

Former Member
0 Likes
3,515

Hi people,

How do I go about to insert columns from an internal table -intab- into an data base table -dbtab-. These tables contain some similar columns, not all.

A code example would be much appriciated!

/Armin

1 ACCEPTED SOLUTION
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
1,899

Loop at your internal table move corresponding fields from your itab wa to the wa of the db table, then modify the db table from the wa.

data: xtable type ztable.


Loop at itab.

move-corresponding itab to xtable.
modify ztable from xtable.

endloop.

Regards,

Rich Heilman

10 REPLIES 10
Read only

Manohar2u
Active Contributor
0 Likes
1,899

Inserting a column in internal table is nothing by defining a field in your internal table defination.

And before that you might inserted the field in database table.

Am I understood your query ??

Regds

Manohar

Read only

Former Member
0 Likes
1,899

Hi Armin,

Use the following statement.

<b>INSERT INTO dbtab VALUES intab.</b>

If you need to update few rows of internal table based on certain condition do as follows::

<b>DATA wa TYPE intab.

LOOP AT intab INTO wa WHERE<condition>.

INSERT INTO dbtab VALUES wa.

CLEAR wa.

ENDLOOP.</b>

Regards,

Vinay

Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
1,900

Loop at your internal table move corresponding fields from your itab wa to the wa of the db table, then modify the db table from the wa.

data: xtable type ztable.


Loop at itab.

move-corresponding itab to xtable.
modify ztable from xtable.

endloop.

Regards,

Rich Heilman

Read only

Former Member
0 Likes
1,899

hi armin,

INSERT dbtab FROM TABLE itab. oder

INSERT (dbtabname) FROM TABLE itab.

Extras:

1. ... CLIENT SPECIFIED

2. ... ACCEPTING DUPLICATE KEYS

3. ... CONNECTION con

Effect

Mass insert: All lines of the internal table itab are inserted in one single operation. The lines of itab must fulfill the same conditions as the work area wa in variant 1.

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

The Return Code is set as follows:

SY-SUBRC = 0:

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

Note

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

Addition 1

... CLIENT SPECIFIED

Effect

As with variant 1.

Addition 2

... ACCEPTING DUPLICATE KEYS

Effect

If a line cannot be inserted, the system does not

terminate with a runtime error but only sets the return value SY-SUBRC to 4. All other lines are inserted after the command is executed.

hope this helps,

do reward if it helps,

priya.

Read only

Former Member
0 Likes
1,899

Hi Armin,

Do you want to

1)insert new record in all the cases or

2) you want to insert only when its a new record in internal table but if there is a record with same keys already present in the table, then the existing record should be updated with the internal table records.

If first case is true then code will be :

INSERT dbtab FROM TABLE intab.

else if the second case is true then code will be :

MODIFY dbtab FROM TABLE intab.

Above statements will do mass updates in 1 go but if you want to do it in a loop taking 1 record from intab and inserting it then statement will be :

1)

data : wa_intab type intab.
  loop at intab into wa_intab.
   INSERT INTO dbtab VALUES wa_intab.
  endloop.

2) )

data : wa_intab type intab.
  loop at intab into wa_intab.
   MODIFY dbtab FROM wa_intab.
  endloop.

Cheers,

Vikram

Pls reward for helpful replies!!

Read only

Former Member
0 Likes
1,899

Thanks a lot for your suggestions, but I have allready tryed all the suggested approaches and non of them worked:

1) When trying this approach:

INSERT dbtab FROM TABLE intab.

or:

MODIFY dbtab FROM TABLE intab.

or:

data : wa_intab type intab.

loop at intab into wa_intab.

INSERT INTO dbtab VALUES wa_intab.

endloop.

etc...

I get the error: "The type of the database table and work area (or internal table) are not Unicode convertible.."

2) When trying this approach as suggested by mr. Heilman:

DATA db_wa TYPE dbtab.

LOOP AT intab

INTO wa

WHERE checkboxx = 'X'.

db_wa~stono = wa-stono.

.

.

INSERT INTO dbtab VALUES db_wa.

ENDLOOP.

I get the error: "db_wa unexpected.."

The problem is that the tables have some columns that are the same and others that are not. They are not of the same type!

Has anyone had this prob before and solved it?

Thanks!

Read only

0 Likes
1,899

Check your program with UCCHECK tcode, this might give you some log.

Regds

Manohar

Read only

Former Member
0 Likes
1,899

Never mind guys, Rich's sollution worked. Had some other problems that made me initially think it doesn't work.

So 10 goes to mr. Heilman and rest of you get a strong 2 for the effort!

Thanks guys!

Read only

Former Member
0 Likes
1,899

Dear Armin,

Are you able to do that?? Thats surprizing !! could you please explain me how it adds a new column to a DATABASE TABLE from internal table??

regards,

Amiya Shrivastava

Read only

Former Member
0 Likes
1,899

I am sorry ..I read the confabulation only superficially, now I am clear with what you had asked!!

Inserting columns values and not adding new columns!!