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

abt workarea

Former Member
0 Likes
1,161

Hi all

Why do we need to declare a workarea to do any database related operations.can we not get records directly from database instead of creating a workarea or internaltable...what makes difference.

Thanks and Regards

1 ACCEPTED SOLUTION
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
1,126

You can read from the database in a few ways, the first way is with a SELECT...ENDSELECT, this is not the best way as it is a round trip for each record, here you must define a work area to read the data into, even if you use a TABLES statement for the table name, it is still a work area.

tables: mara.

select * from mara.

endselect.

Or you can use a work area defined by a DATA statement.

Data: xmara type mara.

select * from mara into xmara.

endselect.

Or a single select.

data: xmara type mara.

Select Single * into xmara from mara
           where matnr = p_matnr.

The most efficient way for multplie records is an arry fetch here, you can specify an internal table .

data: imara type table of mara with header line.

select * into table imara from mara
                   up to 100 rows.

But in the OO context, header lines(which are like implicit work areas) are not allowed, so you need a work area to read the data from the internal table.

data: imara type table of mara .
data: xmara like line of imara.

select * into table imara from mara
                   up to 100 rows.


loop at imara into xmara.
   write:/ xmara-matnr, xmara-mtart.
endloop.

Regards,

Rich Heilman

11 REPLIES 11
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
1,127

You can read from the database in a few ways, the first way is with a SELECT...ENDSELECT, this is not the best way as it is a round trip for each record, here you must define a work area to read the data into, even if you use a TABLES statement for the table name, it is still a work area.

tables: mara.

select * from mara.

endselect.

Or you can use a work area defined by a DATA statement.

Data: xmara type mara.

select * from mara into xmara.

endselect.

Or a single select.

data: xmara type mara.

Select Single * into xmara from mara
           where matnr = p_matnr.

The most efficient way for multplie records is an arry fetch here, you can specify an internal table .

data: imara type table of mara with header line.

select * into table imara from mara
                   up to 100 rows.

But in the OO context, header lines(which are like implicit work areas) are not allowed, so you need a work area to read the data from the internal table.

data: imara type table of mara .
data: xmara like line of imara.

select * into table imara from mara
                   up to 100 rows.


loop at imara into xmara.
   write:/ xmara-matnr, xmara-mtart.
endloop.

Regards,

Rich Heilman

Read only

Former Member
0 Likes
1,126

Hi Popi,

We can also do database related operations without creating work area too. But it is a good practise to have work areas.

Generally we will get data into internal tables from database. For LDBs(Logical databases ) you doesnt require internal tables or work areas.

Thanks,

Vinay

Read only

Former Member
0 Likes
1,126

Hi,

The reason you need to use work area or internal table if you want to manipulate/ modify the data otherwise you can read directly from database.

Regards,

Ferry Lianto

Read only

0 Likes
1,126

Hi

Can we not modify directly in the database?

Thanks and Regards

popi

Read only

0 Likes
1,126

Hi,

We can directly modify database content using MODIFY statement but updating database tables directly is not a good practise.

Check this link for MODIFY statement

http://www.sts.tu-harburg.de/teaching/sap_r3/ABAP4/modify_d.htm

Better to Update them through transactions or BDCs.

Thanks,

Vinay

Read only

0 Likes
1,126

Yes, you can modify the database directly, but again, using the TABLES statement is like declaring a workarea.

tables: ZTEST.

Select * from ztest.
    ztest-fld1 = 'X'.
    modif ztest.
endselect.

All other methods of update the DB, use a work area or internal table of some kind.

Regards,

Rich Heilman

Read only

0 Likes
1,126

Hi vinay

can u please be more specific that why is it using a workarea r internal table is a best practice...what ever modifications we r doing we r doing to the database only either it is thru workarea r direct.sorry for bothering u all..this question is bothering me soo much...not able to move any forward

Thanks and Regards

popi

Read only

0 Likes
1,126

I dopn't think you need either a work area or a tables statement. For example:


REPORT ztest.

UPDATE ztest
  SET   f1 = space
  WHERE f2 = 'X'.

I'm not recommending doing it this way, just saying that syntactically, it's correct and will work.

Rob

Message was edited by:

Rob Burbank

Read only

0 Likes
1,126

Rob, yes you are right about the UPDATE statement, I personally have never used this statement in a productive program.(Which is why I might have misspoke)

Regards,

Rich Heilman

Read only

0 Likes
1,126

I recall seeing this in an SAP note some time ago. It was just a quick database ZAP, but I was a bit surprised that they would use it.

(It was note 301795. We had to implement it after upgrading to 4.6C)

Rob

Message was edited by:

Rob Burbank

Read only

Former Member
0 Likes
1,126

Hi,

Using a Work Area or Internal Tables is a best practice. Using TABLES Statements & then not mentioning anythin in the INTO is an obsolete way. Also, TABLES reserves some space in the memory before the data is fetched.

Reward points if the answer is helpful.

Regards,

Mukul