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 DATASET

Former Member
0 Likes
2,957

Hi Friends,

I'm getting problem in reading only 100 records from the application server using the READ DATASET statement. The file on the application server may have 8000 records but i wanted to get only 100 records and process them. Then i wanted to get next 100 records from the file stored on app.server and process and so on.

After I get the 100 records, I wanted to do further operations such as putting them into a custom table.

Will the below code work?

DATA l_item(100) type c.

*Open the .DAT file for reading
  OPEN DATASET p_file1 FOR INPUT IN TEXT MODE ENCODING UTF-8 MESSAGE
  x_error.

  IF sy-subrc NE 0.
*An error occured during opening the dataset !
    MESSAGE x_error TYPE 'E'.
    CLEAR x_error.
  ENDIF.

  DO.
*Read the .DAT file into an internal table
    READ DATASET p_file1 INTO l_item.

    IF sy-subrc EQ 0.
*split the records into data fields based on delimiter ','
*then append the current record to an internal table
      SPLIT l_item AT ',' INTO
                               x_item-inv_item_id
                               x_item-intl_aa_nbr
                               x_item-intl_pba_nbr
                               x_item-intl_ta_nbr
                               x_item-intl_product_code
                               x_item-descr254
                               x_item-intl_stck_id
                               x_item-awr_disallowed_flg
                               x_item-returnable_flg
                               x_item-fulfillment_flg
                               x_item-refund_only_flg
                               x_item-prod_returnable_flg
                               x_item-stock_cat_id.

      APPEND x_item TO i_item.
      CLEAR x_item.
      
    ELSE.
*Data was read and the end of the file was reached
      MESSAGE i006.
      EXIT.
    ENDIF.
  ENDDO.

1 ACCEPTED SOLUTION
Read only

venkat_o
Active Contributor
0 Likes
1,998

Hi Shamim, Try this way.


data: count type p.                                             "New changes      
*Open the .DAT file for reading
  OPEN DATASET p_file1 FOR INPUT IN TEXT MODE ENCODING UTF-8 MESSAGE x_error.
  IF sy-subrc NE 0.
*   An error occured during opening the dataset !
    MESSAGE x_error TYPE 'E'.
    CLEAR x_error.
  ENDIF.
  DO.
*   Read the .DAT file into an internal table
    count = sy-index div 100.                                   "New changes
    READ DATASET p_file1 INTO l_item.
    IF sy-subrc EQ 0.
*     split the records into data fields based on delimiter ','
*     then append the current record to an internal table
      CASE count.                                               "New changes
       WHEN 1.                                                  "New changes
        SPLIT l_item AT ',' INTO
                               x_item-inv_item_id
                               x_item-intl_aa_nbr
                               x_item-intl_pba_nbr
                               x_item-intl_ta_nbr
                               x_item-intl_product_code
                               x_item-descr254
                               x_item-intl_stck_id
                               x_item-awr_disallowed_flg
                               x_item-returnable_flg
                               x_item-fulfillment_flg
                               x_item-refund_only_flg
                               x_item-prod_returnable_flg
                               x_item-stock_cat_id.
        APPEND x_item TO i_item.
        CLEAR x_item.
       WHEN 2.                                                 "New changes
       WHEN 3.                                                 "New changes
       WHEN 4.                                                 "New changes
       WHEN 5.                                                 "New changes
       WHEN 6.                                                 "New changes
       WHEN others.                                            "New changes
      ENDCASE.                                                 "New changes
    ELSE.
*     Data was read and the end of the file was reached
      MESSAGE i006.
      EXIT.
    ENDIF.
  ENDDO.
Thanks, Venkat.O

8 REPLIES 8
Read only

Former Member
0 Likes
1,998

OPEN DATASET p_file1 FOR INPUT IN TEXT MODE ENCODING UTF-8 MESSAGE

x_error.

IF sy-subrc NE 0.

*An error occured during opening the dataset !

MESSAGE x_error TYPE 'E'.

CLEAR x_error.

ENDIF.

DO. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<

DO 100 times.<<<<<<<<<<<<<<<<<<<<<<<<<<<

*Read the .DAT file into an internal table

READ DATASET p_file1 INTO l_item.

IF sy-subrc EQ 0.

*split the records into data fields based on delimiter ','

*then append the current record to an internal table

SPLIT l_item AT ',' INTO

x_item-inv_item_id

x_item-intl_aa_nbr

x_item-intl_pba_nbr

x_item-intl_ta_nbr

x_item-intl_product_code

x_item-descr254

x_item-intl_stck_id

x_item-awr_disallowed_flg

x_item-returnable_flg

x_item-fulfillment_flg

x_item-refund_only_flg

x_item-prod_returnable_flg

x_item-stock_cat_id.

APPEND x_item TO i_item.

CLEAR x_item.

ELSE.

*Data was read and the end of the file was reached

MESSAGE i006.

EXIT.

ENDIF.

ENDDO.

ENDDO.

Read only

Former Member
0 Likes
1,998

Hi,

Use DO 100 times. before using READ DATASET p_file1 INTO l_item. such that reading will stop after 100 lines are reached and continue with updation of the custom table.

Regards,

Vik

Read only

Former Member
0 Likes
1,998

HI SHAMIM,

The code that you have give will work fine, but for all the records. It does not handle the condition where in after 100 records you'll process them. You may either refer to the code in the previous post or set a counter to check the record number after reaching the count of 100 process the records accordingly and reset the counter back to 1 for the next set of records.

Hope this helps.

Regards,

Sachin

Read only

MarcinPciak
Active Contributor
0 Likes
1,998

Hi,

I think problem lies in definition data l_item(100) type c. .

This way you will read 100 char per each loop, and you want to read only 100 records. In case line is wider you will read it in couple steps (not one loop iteration). Give type char255 .

Also check is this read string contains CL_ABAP_CHAR_UTILITIES=>CR_LF, so you know where the line ends and then append it to your table.

Regards

Marcin

Read only

Former Member
0 Likes
1,998

Hi,


DATA l_item(100) type c.
data: lv_times type i.
*Open the .DAT file for reading
  OPEN DATASET p_file1 FOR INPUT IN TEXT MODE ENCODING UTF-8 MESSAGE
  x_error.
  IF sy-subrc NE 0.
*An error occured during opening the dataset !
    MESSAGE x_error TYPE 'E'.
    CLEAR x_error.
  ENDIF.
  DO.
*Read the .DAT file into an internal table
    READ DATASET p_file1 INTO l_item.
    IF sy-subrc EQ 0.
*split the records into data fields based on delimiter ','
*then append the current record to an internal table
      SPLIT l_item AT ',' INTO
                               x_item-inv_item_id
                               x_item-intl_aa_nbr
                               x_item-intl_pba_nbr
                               x_item-intl_ta_nbr
                               x_item-intl_product_code
                               x_item-descr254
                               x_item-intl_stck_id
                               x_item-awr_disallowed_flg
                               x_item-returnable_flg
                               x_item-fulfillment_flg
                               x_item-refund_only_flg
                               x_item-prod_returnable_flg
                               x_item-stock_cat_id.
      APPEND x_item TO i_item.
      CLEAR x_item.
      lv_times = lv_times + 1.
     if lv_times = 100.
       Process records here.
     clear x_items.
   refresh x_items.
    endif.
    ELSE.
*Data was read and the end of the file was reached
      MESSAGE i006.
      EXIT.
    ENDIF.
  ENDDO.

Read only

Former Member
0 Likes
1,998

Hi,

If you want to process every 100 records once, you can in ways.

1.

DO.

*Read the .DAT file into an internal table

READ DATASET p_file1 INTO l_item.

IF sy-subrc EQ 0.

*split the records into data fields based on delimiter ','

*then append the current record to an internal table

SPLIT l_item AT ',' INTO

x_item-inv_item_id

x_item-intl_aa_nbr

x_item-intl_pba_nbr

x_item-intl_ta_nbr

x_item-intl_product_code

x_item-descr254

x_item-intl_stck_id

x_item-awr_disallowed_flg

x_item-returnable_flg

x_item-fulfillment_flg

x_item-refund_only_flg

x_item-prod_returnable_flg

x_item-stock_cat_id.

APPEND x_item TO i_item.

CLEAR x_item.

lv_count = lv_count + 1.

if lv_count = 100.

perform process 100 records.

clear: lv_count, x_items.

refresh x_items.

endif.

ELSE.

*Data was read and the end of the file was reached

MESSAGE i006.

EXIT.

ENDIF.

ENDDO.

2. Download all records into an internal table once.

then loop internaltable.

data: lv_count type i,

lv_count1 type i,

lv_previous type i.

lv_count = lv_count + 1.

lv_count1 = lv_count1 + 1.

if lv_count1 < = 100.

copy 100 records into another internal table2.

else.

lv_previous = lv_count - lv_previous.

if lv_previous = 100.

process records.

clear: lv_count1, internal table2.

refresh internal table2

endif.

endif.

endloop.

regards,

Ganesh

Read only

venkat_o
Active Contributor
0 Likes
1,999

Hi Shamim, Try this way.


data: count type p.                                             "New changes      
*Open the .DAT file for reading
  OPEN DATASET p_file1 FOR INPUT IN TEXT MODE ENCODING UTF-8 MESSAGE x_error.
  IF sy-subrc NE 0.
*   An error occured during opening the dataset !
    MESSAGE x_error TYPE 'E'.
    CLEAR x_error.
  ENDIF.
  DO.
*   Read the .DAT file into an internal table
    count = sy-index div 100.                                   "New changes
    READ DATASET p_file1 INTO l_item.
    IF sy-subrc EQ 0.
*     split the records into data fields based on delimiter ','
*     then append the current record to an internal table
      CASE count.                                               "New changes
       WHEN 1.                                                  "New changes
        SPLIT l_item AT ',' INTO
                               x_item-inv_item_id
                               x_item-intl_aa_nbr
                               x_item-intl_pba_nbr
                               x_item-intl_ta_nbr
                               x_item-intl_product_code
                               x_item-descr254
                               x_item-intl_stck_id
                               x_item-awr_disallowed_flg
                               x_item-returnable_flg
                               x_item-fulfillment_flg
                               x_item-refund_only_flg
                               x_item-prod_returnable_flg
                               x_item-stock_cat_id.
        APPEND x_item TO i_item.
        CLEAR x_item.
       WHEN 2.                                                 "New changes
       WHEN 3.                                                 "New changes
       WHEN 4.                                                 "New changes
       WHEN 5.                                                 "New changes
       WHEN 6.                                                 "New changes
       WHEN others.                                            "New changes
      ENDCASE.                                                 "New changes
    ELSE.
*     Data was read and the end of the file was reached
      MESSAGE i006.
      EXIT.
    ENDIF.
  ENDDO.
Thanks, Venkat.O

Read only

Former Member
0 Likes
1,998

thanks everybody,

i've used all your suggestions especially the counter method...

i'm increating counter every time inside the loop and when it reaches 100, taking all records into an internal table.

regards,

shamim