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

Read line

Former Member
0 Likes
1,183

How to get morethan one record without using loop at statement?

In my internal table

No <f1> <f2> <f3>

1 - - -

1 - - -

1 - - -

2 - - -

2 - - -

How to know how many records are there as per No?

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,115

HI

You need to Loop to retrieve all records one by one in header of table. There is no other statement.

If you want to count the total no. of records then you can use

DESCRIBE table <table-name> lines <variable name to store result>.

Regards

Aditya

10 REPLIES 10
Read only

Former Member
0 Likes
1,116

HI

You need to Loop to retrieve all records one by one in header of table. There is no other statement.

If you want to count the total no. of records then you can use

DESCRIBE table <table-name> lines <variable name to store result>.

Regards

Aditya

Read only

Former Member
0 Likes
1,115

Hi,

if u use

parameters: num type no.

select count(no) from dbtab into itab where no in num.

Regards

Read only

Former Member
0 Likes
1,115

Hi

Firstly ,

Describe table itab lines n.

Do n times.

Read table itab index sy-index

if sy-subrc = 0.

...

endif.

enddo.

Regards,

Bijal:)

Read only

Former Member
0 Likes
1,115

Hi,

If your data is in one internal table and have to move other than use below syntax:

INSERT LINES OF <itab1> [FROM <n1>] [TO <n 2>] INTO TABLE <itab2>.

EX:

DATA: BEGIN OF LINE,

COL1 TYPE I,

COL2 TYPE I,

END OF LINE.

DATA: ITAB LIKE STANDARD TABLE OF LINE,

JTAB LIKE SORTED TABLE OF LINE

WITH NON-UNIQUE KEY COL1 COL2.

DO 3 TIMES.

LINE-COL1 = SY-INDEX. LINE-COL2 = SY-INDEX ** 2.

APPEND LINE TO ITAB.

LINE-COL1 = SY-INDEX. LINE-COL2 = SY-INDEX ** 3.

APPEND LINE TO JTAB.

ENDDO.

INSERT LINES OF ITAB INTO TABLE JTAB.

LOOP AT JTAB INTO LINE.

WRITE: / SY-TABIX, LINE-COL1, LINE-COL2.

ENDLOOP.

2. DATA : v_cnt TYE I.

DESCRIBE TABLE ITAB LINES v_cnt.

WRITE : v_cnt.

Read only

Former Member
0 Likes
1,115

Hi

use

DESCRIBE TABLE itab LINES v_totalrec.

Read only

Former Member
0 Likes
1,115

Hai,

To read a single line of any table, use the statement:

READ TABLE <itab> <key> <result>.

For the statement to be valid for any kind of table, you must specify the entry using the key and not the index. You specify the key in the <key> part of the statement. The <result> part can specify a further processing option for the line that is retrieved.

If the system finds an entry, it sets SY-SUBRC to zero, if not, it takes the value 4, as long as it is not influenced by one of the possible additions. If the internal table is an index table, SY-TABIX is set to the index of the line retrieved. If the table has a non-unique key and there are duplicate entries, the first entry is read.

Specifying the Search Key

The search key may be either the table key or another key.

Using the Table Key

To use the table key of <itab> as a search key, enter <key> as follows:

READ TABLE <itab> FROM <wa> <result>.

or as follows

READ TABLE <itab> WITH TABLE KEY <k1> = <f 1> ... <k n> = <f n> <result>.

In the first case, <wa> must be a work area compatible with the line type of <itab>. The values of the key fields are taken from the corresponding components of the work area.

In the second case, you have to supply the values of each key field explicitly. If you do not know the name of one of the key fields until runtime, you can specify it as the content of a field <n i > using the form (<n i >) = <f i >. If the data types of <f i > are not compatible with the key fields, the system converts them.

The system searches for the relevant lines as follows:

Standard tables

Linear search, where the runtime is in linear relation to the number of table entries.

Sorted tables

Binary search, where the runtime is in logarithmic relation to the number of table entries.

Hashed tables

The entry is found using the hash algorithm of the internal table. The runtime is independent of the number of table entries.

Using a Different Search Key

To use a key other than the table key as a search key, enter <key> as follows:

READ TABLE <itab> WITH KEY = <f> <result>.

or as follows

READ TABLE <itab> WITH KEY <k1> = <f1> ... <k n> = <f n> <result>.

In the first case, the whole line of the internal table is used as the search key. The contents of the entire table line are compared with the contents of field <f>. If <f> is not compatible with the line type of the table, the value is converted into the line type. The search key allows you to find entries in internal tables that do not have a structured line type, that is, where the line is a single field or an internal table type.

In the second case, the search key can consist of any of the table fields <k 1 >...<k n >. If you do not know the name of one of the components until runtime, you can specify it as the content of a field <n i > using the form (<n i >) = <f i >. If <n i > is empty when the statement is executed, the search field is ignored. If the data types of <f i > are not compatible with the components in the internal table, the system converts them. You can restrict the search to partial fields by specifying offset and length.

The search is linear for all table types. The runtime is in linear relation to the number of table lines.

Specifying the Extra Processing Option

You can specify an option that specifies what the system does with the table entry that it finds.

Using a Work Area

You can write the table entry read from the table into a work area by specifying <result> as follows:

READ TABLE <itab> <key> INTO <wa> [COMPARING <f1> <f 2> ...

|ALL FIELDS]

[TRANSPORTING <f1> <f 2> ...

|ALL FIELDS

|NO FIELDS].

If you do not use the additions COMPARING or TRANSPORTING, the contents of the table line must be convertible into the data type of the work area <wa>. If you specify COMPARING or TRANSPORTING, the line type and work area must be compatible. You should always use a work area that is compatible with the line type of the relevant internal table.

If you use the COMPARING addition, the specified table fields <f i > of the structured line type are compared with the corresponding fields of the work area before being transported. If you use the ALL FIELDS option, the system compares all components. If the system finds an entry with the specified key <key> and if the contents of the compared fields are the same, SY-SUBRC is set to 0. If the contents of the compared fields are not the same, it returns the value 2. If the system cannot find an entry, SY-SUBRC is set to 4. If the system finds an entry, it copies it into the target work area regardless of the result of the comparison.

If you use the TRANSPORTING addition, you can specify the table fields of the structured line type that you want to transport into the work area. If you specify ALL FIELDS without TRANSPORTING, the contents of all of the fields are transported. If you specify NO FIELDS, no fields are transported. In the latter case, the READ statement only fills the system fields SY-SUBRC and SY-TABIX. Specifying the work area <wa> with TRANSPORTING NO FIELDS is unnecessary, and should be omitted.

In both additions, you can specify a field <f i > dynamically as the contents of a field <n i > in the form (<n i >). If <n i > is empty when the statement is executed, it is ignored. You can restrict the search to partial fields by specifying offset and length.

Using a Field Symbol

You can assign the table entry read from the table to a field symbol by specifying <result> as follows:

READ TABLE <itab> <key> ASSIGNING <FS>.

After the READ statement, the field symbol points to the table line. If the line type is structured, you should specify the same type for the field symbol when you declare it. This allows you to address the components of the field symbol. If you cannot specify the type statically, you must use further field symbols and the technique of assigning components of structures to address the components of the structure.

For further information about assigning table lines to field symbols, refer to Access Using Field Symbols.

Examples

DATA: BEGIN OF LINE,

COL1 TYPE I,

COL2 TYPE I,

END OF LINE.

DATA ITAB LIKE HASHED TABLE OF LINE WITH UNIQUE KEY COL1.

DO 4 TIMES.

LINE-COL1 = SY-INDEX.

LINE-COL2 = SY-INDEX ** 2.

INSERT LINE INTO TABLE ITAB.

ENDDO.

LINE-COL1 = 2. LINE-COL2 = 3.

READ TABLE ITAB FROM LINE INTO LINE COMPARING COL2.

WRITE: 'SY-SUBRC =', SY-SUBRC.

SKIP.

WRITE: / LINE-COL1, LINE-COL2.

The output is:

SY-SUBRC = 2

2 4

The program fills a hashed table with a list of square numbers. The work area LINE, which is compatible with the line type, is filled with the numbers 2 and 3. The READ statement reads the line of the table in which the key field COL1 has the same value as in the work area and copies it into the work area. SY-SUBRC is set to 2, because the contents of field COL2 were different.

DATA: BEGIN OF LINE,

COL1 TYPE I,

COL2 TYPE I,

END OF LINE.

DATA ITAB LIKE SORTED TABLE OF LINE WITH UNIQUE KEY COL1.

DO 4 TIMES.

LINE-COL1 = SY-INDEX.

LINE-COL2 = SY-INDEX ** 2.

INSERT LINE INTO TABLE ITAB.

ENDDO.

CLEAR LINE.

READ TABLE ITAB WITH TABLE KEY COL1 = 3

INTO LINE TRANSPORTING COL2.

WRITE: 'SY-SUBRC =', SY-SUBRC,

/ 'SY-TABIX =', SY-TABIX.

SKIP.

WRITE: / LINE-COL1, LINE-COL2.

The output is:

SY-SUBRC = 0

SY-TABIX = 3

0 9

The program fills a sorted table with a list of square numbers. The READ statement reads the line of the table in which the key field COL1 has the same value as in the work area and copies it into the work area. Only the contents of COL2 are copied into the work area LINE. SY-SUBRC is zero, and SY-TABIX is 3, because ITAB is an index table.

DATA: BEGIN OF LINE,

COL1 TYPE I,

COL2 TYPE I,

END OF LINE.

DATA ITAB LIKE SORTED TABLE OF LINE WITH UNIQUE KEY COL1.

DO 4 TIMES.

LINE-COL1 = SY-INDEX.

LINE-COL2 = SY-INDEX ** 2.

INSERT LINE INTO TABLE ITAB.

ENDDO.

READ TABLE ITAB WITH KEY COL2 = 16 TRANSPORTING NO FIELDS.

WRITE: 'SY-SUBRC =', SY-SUBRC,

/ 'SY-TABIX =', SY-TABIX.

The output is:

SY-SUBRC = 0

SY-TABIX = 4

The program fills a sorted table with a list of square numbers. The READ statement reads the line of the table in which the key field COL1 has the value 16. It does not use the table key. No fields are copied to a work area or assigned to a field symbol. Instead, only the system fields are set. SY-SUBRC is zero, since a line was found, and SY-TABIX is four.

DATA: BEGIN OF LINE,

COL1 TYPE I,

COL2 TYPE I,

END OF LINE.

DATA ITAB LIKE HASHED TABLE OF LINE WITH UNIQUE KEY COL1.

FIELD-SYMBOLS <FS> LIKE LINE OF ITAB.

DO 4 TIMES.

LINE-COL1 = SY-INDEX.

LINE-COL2 = SY-INDEX ** 2.

INSERT LINE INTO TABLE ITAB.

ENDDO.

READ TABLE ITAB WITH TABLE KEY COL1 = 2 ASSIGNING <FS>.

<FS>-COL2 = 100.

LOOP AT ITAB INTO LINE.

WRITE: / LINE-COL1, LINE-COL2.

ENDLOOP.

The output is:

1 1

2 100

3 9

4 16

The program fills a hashed table with a list of square numbers. The READ statement reads the line of the table in which the key field COL1 has the value 2 and assigns it to the field symbol <FS>. The program then assigns the value 100 to component COL2 of <FS>. This also changes the corresponding table field.

Read only

Former Member
0 Likes
1,115

Hi,

By using System variable Sy-Dbcnt u can get the the total no. of records in the table.

If u dont want to use loop ... endloop to retive the data in the internal table then u can use Do.. Enddo. by limiting the do loop using the count in sy-dbcnt.

Regards.

kavitha.

Read only

Former Member
0 Likes
1,115

Hi Krishna,

If you want to transfer data from one internal table to other without having to use the LOOP statement try the following,

Move ITAB2 to ITAB1 or Move-Corresponding ITAB2 to ITAB1.

here to find out the total number of records in the internal table try the statement,

Describe table ITAB1 lines w_lines. (w_lines should be type I).

For transferring the data from a Database table to Internal Table try the following,

Select

field1

field2

field3

from DB_TABLE

into corresponding fields of

table ITAB1.

here you can use the SY-DBCNT to check the total records in the table.

Reward if helpful.

Read only

Former Member
0 Likes
1,115

>

> How to get morethan one record without using loop at statement?

> In my internal table

> No <f1> <f2> <f3>

> 1 - - -

> 1 - - -

> 1 - - -

> 2 - - -

> 2 - - -

>

> How to know how many records are there as per No?

Hi,

x = 1.

Do sy-tabix times.

read table it_itab into wa_itab index x.

if sy-subrc = 0.

write :wa-field.

x = x + 1.

else.

exit. " to avoid infinite loop.

enddo.

regards,

swami.

Read only

Former Member
0 Likes
1,115

Dear Krishna

You can code like this.

if ur internal table name is itab1.

itab2 = itab1.

sort itab2 by no.

delete adjacent duplicates from itab2 comparing no.

decribe itab2 lines <variable>.

or if u r using loop at you can use AT NEW ENDAT.

Hope this helps.

Regards

Sathar