SAP YARD-athon “Cultivate, Learn & Grow in SAP”

SAP Yard-athon
Share on Facebook45Share on LinkedIn95Tweet about this on TwitterShare on Google+0
Please Share!!

SAPYard presents Yard-athon for all SAP Technical Enthusiasts. This is an opportunity to collaborate, learn and hone our skills.

No knowledge is ever wasted. And we believe, everyone would gain something or other from this Yard-athon. Believe us, no knowledge is small or little.

“Cultivate, Learn & Grow in SAP” is our war cry for this Yard-athon. Heil SAP!!

Objective:

The requirement is to modify the below code snippet using all modern 7.4 ABAP syntax and make it efficient. The idea is to get the Plant and Plant Name into an internal table and print the number of entries in the table as the output.

Hint: Make use of ABAP 7.4 syntaxes and minimize the number of lines of codes.

Prizes:

Best 10 submissions, whom our experienced SAP ABAP Panel choose, would get assured Gifts. We will feature the Winners on our Home Page.

How to participate?

We would suggest you register and write your answer in the comment section but it is not mandatory. You can directly write your Answer in the Comment section below (without registering). Please make sure, your code is activated and we can copy it to our system and execute it as it is.

Number of submissions?

You can submit multiple answers. We would consider the best one from your list.

Below code snippet has to be enhanced to win the Prize.

Please Note:

lt_plant[] internal table needs to have all t001w data and internal table lt_plant_name[] has to have only plant and plant name fields. We need data in both the internal tables.

We want to see the best way to populate both lt_plant[] and lt_plnat_name[]. Assume that, we need entries in both the internal tables for some other business logic down the line in the whole big program.

Sample Output.

SAP TecEd

Let the innovative, alternative answers and code keep flowing in. What are you waiting for? Type in your snippets and win your Prize.

Participate now!

Encourage your friends, colleagues and team members to participate as well!!

Share on Facebook45Share on LinkedIn95Tweet about this on TwitterShare on Google+0
Please Share!!

About the Author

SAP Yard
SAP Yard

SAPYard is one stop page for all Technical Folks in SAP. You would find un-conventional explanations, tutorials, and tricks.

Please like our Facebook Page and also join our LinkedIn Group.

55 Comments on "SAP YARD-athon “Cultivate, Learn & Grow in SAP”"

  1. REPORT zsapyard_Prg_27.
    *– Local Type Declaration
    TYPES: BEGIN OF lty_werks_name,
    plant TYPE werks_d,
    name TYPE name1,
    END OF lty_werks_name.

    types: tt_plant_name TYPE STANDARD TABLE OF lty_werks_name WITH KEY plant.

    *– Local Data Declaration
    data: lv_regio type land1 value ‘TX’.

    *– Select Plant
    SELECT * FROM t001w
    INTO TABLE @data(lt_plant).

    *– ABAP 740 FOR loop is used to loop the plant info table
    data(lt_plant_name) = value tt_plant_name( for in lt_plant where ( regio = lv_regio )
    ( plant = -werks
    name = -name1 ) ).
    *– Get the Number of Plants in the Region
    data(lv_count) = lines( lt_plant_name[] ).
    *– ABAP 740 string operations is used to bind o/p string
    data(lv_string) = |Region: { lv_regio }| && | has { lv_count }| && | Plants|.
    * Write a string output
    WRITE: lv_string.

  2. DATA(lv_regio) = ‘TX’.

    *Select Plant Info
    SELECT * FROM t001w
    INTO TABLE @DATA(lt_plant).

    *Fetch plant and plant name from T001W
    SELECT werks AS plant,
    name1 AS name
    FROM t001w
    WHERE regio EQ @lv_regio
    INTO TABLE @DATA(lt_plant_name).

    *Get the Number of Plants in the Region
    DATA(lv_lines) = REDUCE i( INIT x = 0 FOR ls_plant IN lt_plant_name NEXT x = x + 1 ).

    *Write a plain output
    WRITE: ‘Region’ , lv_regio, ‘has’, lv_lines, ‘Plant(s)’.

  3. * Local Type Declaration
    TYPES: BEGIN OF lty_werks_name,
    plant TYPE werks_d,
    name TYPE name1,
    END OF lty_werks_name.

    TYPES: tt_plant_name TYPE STANDARD TABLE OF lty_werks_name WITH EMPTY KEY.

    * Local Data Declaration
    DATA: lv_count TYPE i,
    lv_regio TYPE land1 VALUE ‘TX’.

    * Select Plant Info
    SELECT * FROM t001w
    INTO TABLE @DATA(lt_plant).

    * Assign internal table with plant and plant name
    DATA(lt_plant_name) = VALUE tt_plant_name(
    FOR ls_plant IN lt_plant
    WHERE ( regio EQ lv_regio )
    ( plant = ls_plant-werks ) ( name = ls_plant-name1 ) ).

    * Get the Number of Plants in the Region
    DESCRIBE TABLE lt_plant_name LINES lv_count.

    * Write a plain output
    WRITE: ‘Region’ , lv_regio, ‘has’, lv_count, ‘Plant(s)’.

  4. data : lv_regio TYPE land1 VALUE ‘TX’.

    SELECT werks,
    name1,
    regio
    FROM t001w
    INTO TABLE @DATA(lt_t001w).
    * ORDER BY werks.

    lv_count = REDUCE i( INIT x = 0 FOR ls_t001 IN lt_t001w
    WHERE ( regio = lv_regio ) NEXT x = x + 1 ).

    * Write a plain output
    WRITE: ‘Region’ , lv_regio, ‘has’, lv_count, ‘Plant(s)’.

  5. * Local Type Declaration
    TYPES: BEGIN OF lty_werks_name,
    werks TYPE werks_d,
    name1 TYPE name1,
    END OF lty_werks_name,
    lty_werks_name_t TYPE STANDARD TABLE OF lty_werks_name WITH EMPTY KEY.
    DATA lv_regio TYPE land1 VALUE ‘TX’.
    SELECT * FROM t001w
    INTO TABLE @DATA(lt_plant).
    * Loop through and save Plant and Name only
    DATA(lt_plant_name) = VALUE lty_werks_name_t( FOR ls_plant IN lt_plant WHERE ( regio = lv_regio ) ( werks = ls_plant-werks name1 = ls_plant-name1 ) ).
    * Get the Number of Plants in the Region
    DATA(lv_count) = lines( lt_plant_name ). WRITE: ‘Region’ , lv_regio, ‘has’, lv_count, ‘Plant(s)’,/.
    LOOP AT lt_plant_name INTO DATA(lwa_plant_name).
    WRITE: ‘Plant-‘ , lwa_plant_name-werks, ‘Plant Name -‘, lwa_plant_name-name1 , /.
    ENDLOOP.

    Anils

  6. * Local Type Declaration
    TYPES: BEGIN OF lty_werks_name,
    plant TYPE werks_d,
    name TYPE name1,
    END OF lty_werks_name.

    * Local Data Declaration
    DATA: lt_plant TYPE STANDARD TABLE OF t001w,
    ls_plant TYPE t001w,
    lv_regio TYPE land1 VALUE ‘TX’,
    lt_plant_name TYPE STANDARD TABLE OF lty_werks_name,
    ls_plant_name TYPE lty_werks_name,
    lv_count TYPE i.

    * Select Plant Info
    *SELECT * FROM t001w
    *INTO TABLE lt_plant.
    *** Sy-subrc check not required
    **
    *** Loop through and save Plant and Name only
    *LOOP AT lt_plant INTO ls_plant WHERE regio EQ lv_regio.
    * ls_plant_name-plant = ls_plant-werks.
    * ls_plant_name-name = ls_plant-name1.
    * APPEND ls_plant_name TO lt_plant_name.
    *ENDLOOP.

    SELECT * FROM t001w
    INTO ls_plant.
    IF ls_plant-regio EQ lv_regio.
    ls_plant_name-plant = ls_plant-werks.
    ls_plant_name-name = ls_plant-name1.
    APPEND ls_plant_name TO lt_plant_name.
    ENDIF.
    APPEND ls_plant TO lt_plant.
    ENDSELECT.

    SELECT COUNT( * ) FROM t001w
    INTO lv_count
    WHERE regio EQ lv_regio.

    * Get the Number of Plants in the Region
    *DESCRIBE TABLE lt_plant_name LINES lv_count.

    * Write a plain output
    WRITE: ‘Region’ , lv_regio, ‘has’, lv_count, ‘Plant(s)’.

    Thanks and Rgrds,
    György

  7. * Local Types declaration
    TYPES: BEGIN OF ty_plant_name,
    werks TYPE werks_d,
    name1 TYPE name1,
    END OF ty_plant_name.
    * Local Data declaration
    DATA: lt_plant_name TYPE STANDARD TABLE OF ty_plant_name,
    lv_regio TYPE land1 VALUE ‘TX’.
    * To prepare internal table with Plant details for Region ‘TX’
    SELECT *
    FROM t001w
    INTO TABLE @DATA(lt_plant)
    WHERE regio = @lv_regio.
    IF sy-subrc = 0.
    * To move corresponding rows to plant name internal table
    MOVE-CORRESPONDING lt_plant[] TO lt_plant_name[].
    * Get number of lines of Internal table
    DATA(lv_count) = lines( lt_plant_name ).
    WRITE: ‘Region’ , lv_regio, ‘has’, lv_count, ‘Plant(s)’.
    ELSE.
    MESSAGE ‘No Data Found’ TYPE ‘E’ DISPLAY LIKE ‘A’.
    ENDIF.

    Thanks,
    Velu G.P

  8. Erik von der Osten | August 12, 2017 at 7:53 am | Reply

    ” Select all t001w data
    SELECT * FROM t001w INTO TABLE @DATA(lt_plant).

    ” Get the Plant and Plant Name into an internal table
    SELECT werks AS plant, name1 AS name1 FROM t001w
    WHERE regio = ‘TX’
    INTO TABLE @DATA(lt_plant_name).

    ” Print the number of entries in the table
    WRITE: ‘Region’ , ‘TX’, ‘has’, lines( lt_plant_name ), ‘Plant(s)’.

  9. REPORT ZTEST_558.
    **Local Type Declaration
    TYPES: BEGIN OF lty_werks_name,
    plant TYPE werks_d, “plant
    name TYPE name1, “plant name
    END OF lty_werks_name,
    **Table type declaration to get plant & its name.
    ltt_werks_name TYPE STANDARD TABLE OF lty_werks_name WITH DEFAULT KEY.
    **variable for region .
    **as in our table i am not having any entry for regio ‘TX’ so i took
    ** regio as ‘CA’
    DATA(lv_regio) = ‘CA’.
    **Start-of-selection
    START-OF-SELECTION.
    ** Select Plant Info
    SELECT * FROM t001w
    INTO TABLE @DATA(lt_plant) .
    * Loop through and save Plant and Name only to get value into lt_plant_name table
    * to get regio for CA value
    DATA(lt_plant_name) = VALUE ltt_werks_name( for ls_plant IN lt_plant
    WHERE ( regio = lv_regio )
    ( plant = ls_plant-werks
    name = ls_plant-name1 ) ).
    * Get the Number of Plants in the Region
    DESCRIBE TABLE lt_plant_name LINES DATA(lv_count).
    * Write a plain output
    WRITE: ‘Region’ , lv_regio, ‘has’, lv_count, ‘Plant(s)’.

  10. REPORT ZTESTAS.

    PARAMETERS: p_regio TYPE land1 DEFAULT ‘TX’.
    DATA: v_cnt TYPE i.
    SELECT * FROM t001w INTO @DATA(ITAB_ALL).
    SELECT werks AS werks ,name1 AS name1 FROM t001w INTO @DATA(ITAB_FINAL) WHERE regio = p_regio.
    DESCRIBE TABLE itab_final LINES v_cnt.
    WRITE: ‘Region’ , p_regio, ‘has’, v_cnt , ‘plant(s)’.

  11. Naveen Mahadasa | August 10, 2017 at 4:44 am | Reply

    TYPES: BEGIN OF ty_werks_name,
    plant TYPE werks_d,
    name TYPE name1,
    END OF ty_werks_name.

    TYPES : tt_werks_name TYPE STANDARD TABLE OF ty_werks_name WITH EMPTY KEY.

    DATA lv_regio TYPE land1 VALUE ‘TX’.

    SELECT * FROM t001w INTO TABLE @DATA(lt_plant).
    IF sy-subrc = 0.
    DATA(lt_plant_name) = VALUE tt_werks_name( FOR ls_plant IN lt_plant WHERE ( regio EQ lv_regio )
    ( plant = ls_plant-werks name = ls_plant-name1 ) ).
    DATA(lv_count) = lines( lt_plant_name ).

    WRITE: ‘Region’ , lv_regio, ‘has’, lv_count, ‘Plant(s)’.
    ENDIF.

  12. * Local Type Declaration
    TYPES: BEGIN OF lty_werks_name,
    plant TYPE werks_d,
    name TYPE name1,
    END OF lty_werks_name.

    * Local Data Declaration
    DATA: lv_regio TYPE land1 VALUE ‘PR’,
    lt_plant_name TYPE STANDARD TABLE OF lty_werks_name.

    * Select Plant Info
    SELECT * FROM t001w
    INTO TABLE @DATA(lt_plant).
    * Sy-subrc check not required

    * Loop through and save Plant and Name only
    lt_plant_name = VALUE #( FOR ls_plant IN lt_plant WHERE ( regio EQ lv_regio ) ( plant = ls_plant-werks name = ls_plant-name1 ) ).

    * Write a plain output
    WRITE: |Region { lv_regio } has { lines( lt_plant_name ) } Plant(s)|.

  13. Anil Salekar | August 9, 2017 at 1:02 pm | Reply

    There are 2 options to improve the performance as below:

    Original Program:
    Total Execution Time: 33.628 ms
    ABAP: 12.656 ms (37.6%)
    Database: 19.768 ms (58.8%)
    System: 1.204 ms (3.6%)

    1. Add WHERE clause in the query itself and remove WHERE clause from LOOP..ENDLOOP.

    Total Execution Time: 17.654 ms
    ABAP: 16.422 ms (93.0%)
    Database: 213 ms (1.2%)
    System: 1.019 ms (5.8%)

    2. Add WHERE clause in the query itself with secondary index created on REGIO field in T001W table and remove WHERE clause from LOOP..ENDLOOP.

    Total Execution Time: 12.641 ms
    ABAP: 11.053 ms (87.4%)
    Database: 525 ms (4.2%)
    System: 1.063 ms (8.4%)

    Based on the table size we an go for option 1 or 2. Option 2 should be best option ideally.

  14. REPORT zsapyardthon.

    * Local Type Declaration
    TYPES: BEGIN OF lty_werks_name,
    plant TYPE werks_d,
    name TYPE name1,
    END OF lty_werks_name.

    TYPES: tt_werks_name TYPE STANDARD TABLE OF lty_werks_name WITH DEFAULT KEY.
    * Local Data Declaration
    DATA:lv_regio TYPE land1 VALUE ‘TX’.

    * Select Plant Info
    SELECT * FROM t001w
    INTO TABLE @DATA(lt_plant).

    *Save Plant and Name only
    DATA(lt_plant_name) = VALUE tt_werks_name(
    FOR ls_plant IN lt_plant WHERE ( regio = lv_regio )
    ( plant = ls_plant-werks
    name = ls_plant-name1 )
    ).

    * Get the Number of Plants in the Region
    DESCRIBE TABLE lt_plant_name LINES DATA(lv_count).

    * Write a plain output
    WRITE: ‘Region’ , lv_regio, ‘has’, lv_count, ‘Plant(s)’.

  15. Meghomallar Das | August 9, 2017 at 8:53 am | Reply

    * Local Type Declaration
    TYPES: BEGIN OF lty_werks_name,
    plant TYPE werks_d,
    name TYPE name1,
    regio TYPE regio,
    END OF lty_werks_name.

    * Local Data Declaration
    DATA: lt_plant TYPE TABLE OF lty_werks_name,
    lv_count TYPE i.
    CONSTANTS : lc_regio TYPE regio VALUE ‘TX’.

    START-OF-SELECTION.
    * Select Plant Info’
    refresh lt_plant.
    SELECT plant name regio FROM t001w
    INTO TABLE lt_plant
    WHERE regio = lc_regio.
    if sy-subrc eq 0.
    SORT lt_plant.
    ENDIF.

    * Get the Number of Plants in the Region
    CLEAR lv_count.
    DESCRIBE TABLE lt_plant LINES lv_count.

    * Write a plain output
    WRITE: ‘Region’ , lv_regio, ‘has’, lv_count, ‘Plant(s)’.

  16. report zsample_opti.

    * Local Data Declaration
    data: lv_regio type land1 value ’34’. ” May dynamic values so that this line will be there

    select werks ,
    name1 ,
    case regio
    when @lv_regio then ‘X’
    when ‘ ‘ then ‘ ‘
    end as regio
    from t001w
    into table @data(lt_plant).

    *Both data will be available at runtime
    data(lt_plant_name) = lt_plant .

    *Sort and delete which plant not required
    sort lt_plant_name by regio.
    delete lt_plant_name where regio eq ‘ ‘.

    * Get the Number of Plants in the Region
    describe table lt_plant_name lines data(lv_count).

    * Write a plain output
    write: ‘Region’ , lv_regio, ‘has’, lv_count, ‘Plant(s)’.

  17. Pratham Kapoor | August 9, 2017 at 3:49 am | Reply

    TYPES: BEGIN OF lty_werks_name,
    werks TYPE werks_d,
    name TYPE name1,
    regio TYPE regio,
    END OF lty_werks_name.

    * Local Data Declaration
    DATA: lt_plant TYPE STANDARD TABLE OF lty_werks_name,
    lv_regio TYPE land1 value ‘TX’,
    lv_count TYPE i.

    * Select Plant Info
    SELECT werks name1 regio FROM t001w
    INTO TABLE lt_plant WHERE regio EQ lv_regio.
    * Sy-subrc check not required

    * Get the Number of Plants in the Region
    DESCRIBE TABLE lt_plant LINES lv_count.

    * Write a plain output
    WRITE: ‘Region’ , lv_regio, ‘has’, lv_count, ‘Plant(s)’.

    Improvement that made:
    1) No use of Loop statement .
    2) with one internal table we can calculate the count.
    3) where condition should be put it in selection statement as well.
    4) No need to get the t001w structure we can use the existing dynamic structure and add the regio field in it

  18. TYPES: BEGIN OF lty_werks_name,
    werks TYPE werks_d,
    name1 TYPE name1,
    regio TYPE regio,
    END OF lty_werks_name.

    DATA(lv_regio) = ‘TX’.
    TYPES: ty_werks TYPE TABLE OF lty_werks_name.
    DATA: lt_plant TYPE ty_werks.
    TYPES: ty_citys TYPE STANDARD TABLE OF werks_d WITH EMPTY KEY.

    SELECT werks name1 regio
    FROM t001w
    INTO TABLE lt_plant.

    DATA(lt_plant_name) = VALUE ty_citys( FOR ls_plant IN lt_plant WHERE ( regio = lv_regio )
    ( ls_plant-werks ) ).
    DATA(lv_count) = lines( lt_plant_name ).
    WRITE: ‘Region’ , lv_regio, ‘has’, lv_count, ‘Plant(s)’.

  19. * Local Type Declaration
    TYPES:
    BEGIN OF ts_werks_name,
    plant TYPE werks_d,
    name TYPE name1,
    END OF ts_werks_name.

    * Standard table type used with corresponding operator
    TYPES tt_werks_name TYPE STANDARD TABLE OF ts_werks_name WITH DEFAULT KEY.

    * Sorted table type used with filter and conv operators
    TYPES tt_plant_sorted TYPE SORTED TABLE OF t001w WITH NON-UNIQUE KEY regio.

    * Local Data Declaration
    DATA(lv_regio) = CONV land1( ‘TX’ ).

    * Select Plant Info
    SELECT * FROM t001w INTO TABLE @DATA(lt_plant).
    * Sy-subrc check not required

    * Loop through and save Plant and Name only
    DATA(lt_plant_name) = CORRESPONDING tt_werks_name(
    FILTER #( CONV tt_plant_sorted( lt_plant ) WHERE regio = lv_regio )
    MAPPING plant = werks name = name1 ).

    * Get the Number of Plants in the Region
    DATA(lv_count) = lines( lt_plant_name ).

    * Write a plain output
    WRITE |Region { lv_regio } has { lv_count } Plant(s)|.

  20. REPORT yhackaton.

    ” Local Data Declaration
    DATA: l_regio TYPE land1 VALUE ‘TX’.
    ” Select all info for all plants
    SELECT * FROM t001w INTO TABLE @DATA(it_plants).
    ” Select plant names for 1 region
    SELECT werks AS plant, name1 AS name FROM t001w INTO TABLE @DATA(it_plant_names) WHERE regio EQ @l_regio.
    * Write a plain output
    WRITE: |Region { l_regio } has { lines( it_plant_names ) } Plant(s)|.

    I realize this requires 1 too many db access, but on a small table I think the performance hit is negligible.

  21. * Local Type Declaration
    TYPES: BEGIN OF lty_werks_name,
    plant TYPE werks_d,
    name TYPE name1,
    END OF lty_werks_name.

    * Local Data Declaration
    DATA: lt_plant TYPE STANDARD TABLE OF t001w,
    lt_plant1 TYPE STANDARD TABLE OF t001w,
    ls_plant TYPE t001w,
    lv_regio TYPE land1 VALUE ‘TX’,
    lt_plant_name TYPE STANDARD TABLE OF lty_werks_name,
    ls_plant_name TYPE lty_werks_name,
    lv_count TYPE i.

    * Select Plant Info
    SELECT * FROM t001w
    INTO TABLE lt_plant.
    * Sy-subrc check not required

    lt_plant1 = lt_plant.
    DELETE lt_plant1 WHERE regio NE lv_regio.
    * Loop through and save Plant and Name only
    LOOP AT lt_plant1 INTO ls_plant.
    MOVE-CORRESPONDING ls_plant TO ls_plant_name.
    APPEND ls_plant_name TO lt_plant_name.
    ENDLOOP.

    * Get the Number of Plants in the Region
    DESCRIBE TABLE lt_plant_name LINES lv_count.

    * Write a plain output
    WRITE: ‘Region’ , lv_regio, ‘has’, lv_count, ‘Plant(s)’.

  22. *&———————————————————————*
    *& Report Z11192_DEMO1
    *&
    *&———————————————————————*
    *&
    *&
    *&———————————————————————*

    REPORT Z11192_DEMO1.
    * Local Type Declaration
    TYPES: BEGIN OF ty_werks_name,
    plant TYPE werks_d,
    name TYPE name1,
    END OF ty_werks_name.

    * Local Data Declaration
    DATA: lt_plant TYPE STANDARD TABLE OF t001w,
    ls_plant TYPE t001w,
    lv_regio TYPE land1 VALUE ‘TX’,
    lt_plant_name TYPE STANDARD TABLE OF ty_werks_name,
    ls_plant_name TYPE ty_werks_name,
    lv_count TYPE i.

    * Select Plant Info
    SELECT * FROM t001w
    INTO TABLE lt_plant.
    * Sy-subrc check not required

    ” first ,we need to sort table LT_PLANT by REGIO
    “secode,recored the first REGIO
    “third,append the data uitil the REGIO ne lv_regio
    DATA:L_TABIX_STAR TYPE SY-TABIX.
    SORT LT_PLANT BY REGIO.
    READ TABLE LT_PLANT INTO LS_PLANT WITH KEY REGIO = LV_REGIO.
    IF SY-SUBRC EQ 0 .
    L_TABIX_STAR = SY-TABIX.
    ENDIF.
    CLEAR:LS_PLANT.
    * Loop through and save Plant and Name only
    LOOP AT lt_plant INTO ls_plant FROM L_TABIX_STAR.
    IF ls_plant-regio NE lv_regio.
    exit.
    ELSE.
    ls_plant_name-plant = ls_plant-werks.
    ls_plant_name-name = ls_plant-name1.
    APPEND ls_plant_name TO lt_plant_name.
    ENDIF.
    ENDLOOP.
    * Get the Number of Plants in the Region
    DESCRIBE TABLE lt_plant_name LINES lv_count.

    * Write a plain output
    WRITE: ‘Region’ , lv_regio, ‘has’, lv_count, ‘Plant(s)’.

  23. Our proposal with two alternatives. One is shorter in terms of lines of code, while the second alternative is more efficient:

    REPORT z_test_minimze_this.
    * Local Data Declaration
    DATA: lv_regio TYPE land1 VALUE ‘TX’.

    SELECT * FROM t001w INTO TABLE @DATA(lt_plant).”lt_plant is still needed due to challenge requirements

    *————————————————————————————————————–
    * Select Plant Info – Alternative 1 – by another select
    *SELECT werks AS plant, name1 AS name FROM t001w WHERE regio EQ @lv_regio INTO TABLE @DATA(lt_plant_name).
    *————————————————————————————————————–

    *————————————————————————————————————–
    * Select Plant Info – Alternative 2 – based on initial data selection – more lines to write, but we still choose this alternative to ensure no second SELECT is fired…
    TYPES: BEGIN OF ty_s_plant, plant TYPE t001w-werks, name TYPE t001w-name1, END OF ty_s_plant, ty_t_plant TYPE STANDARD TABLE OF ty_s_plant WITH EMPTY KEY.
    DATA(lt_plant_name) = VALUE ty_t_plant( FOR ls_plant IN lt_plant WHERE ( regio = lv_regio ) ( plant = ls_plant-werks name = ls_plant-name1 ) ).
    *————————————————————————————————————–

    * Write a plain output
    WRITE: ‘Region’ , lv_regio, ‘has’, lines( lt_plant_name ), ‘Plant(s)’.

  24. Updated Answer( To preserve values from both tables )

    SELECT * FROM t001w INTO TABLE @DATA(lt_plant).

    DATA(lt_plant_name) =
    VALUE lt_plant_name( FOR ls_plant IN lt_plant where (regio eq @lv_regio)
    (plant = ls_plant-werks
    name = ls_plant-name1) ).

  25. SELECT plant, name FROM t001w INTO TABLE @DATA(lt_plant) where regio = @lv_regio.

    DATA(lt_plant_name) =
    VALUE lt_plant_name( FOR ls_plant IN lt_plant ).

  26. Hi,

    I don’t understand why need internal tables lt_plant and lt_plant_names if they are not used further on. If the program is specified onlyfor the number of plants of related region, the following code is enough:

    * Local Data Declaration
    data: lv_regio TYPE land1 VALUE ‘TX’,
    lv_count type.

    * Select Plant Info
    select count( * ) from t001w
    into lv_count
    where regio eq lv_regio.

    write: ‘Sales org.’ , lv_regio, ‘has’, lv_count, ‘Plant(s)’.

  27. * Local Type Declaration
    TYPES: BEGIN OF lty_werks_name,
    plant TYPE werks_d,
    name TYPE name1,
    END OF lty_werks_name.

    TYPES: lty_werks_names TYPE SORTED TABLE OF lty_werks_name WITH UNIQUE KEY plant.

    * Local Data Declaration
    DATA: lt_plant TYPE STANDARD TABLE OF t001w,
    lv_regio TYPE land1 VALUE ‘TX’,
    ls_plant_name TYPE lty_werks_name.

    * Select Plant Info
    SELECT * FROM t001w
    INTO TABLE lt_plant.
    * Sy-subrc check not required

    DATA(lt_plant_name) = VALUE lty_werks_names( FOR ls_plant IN lt_plant WHERE ( regio = lv_regio )
    ( plant = ls_plant-werks name = ls_plant-name1 ) ).

    * Get the Number of Plants in the Region
    DATA(lv_count) = REDUCE i( INIT x = 0 FOR ls_plant_name1 IN lt_plant_name NEXT x = x + 1 ).

    * Write a plain output
    WRITE: ‘Region’ , lv_regio, ‘has’, lv_count, ‘Plant(s)’.

  28. * Local Data Declaration
    CONSTANTS: lc_regio TYPE regio VALUE ’01’.

    TYPES: BEGIN OF lty_plant_name,
    plant TYPE werks_d,
    name TYPE name1,
    END OF lty_plant_name,

    ltt_plant_name TYPE STANDARD TABLE OF lty_plant_name WITH EMPTY KEY.

    * Select Plant Info
    SELECT * FROM t001w INTO TABLE @DATA(lt_plant).

    * Sy-subrc check not required
    * Loop through and save Plant and Name only
    DATA(lt_plant_name) = VALUE ltt_plant_name( FOR ls_plant IN lt_plant WHERE ( regio = ’01’ )
    ( plant = ls_plant-werks name = ls_plant-name1 ) ).

    * Get the Number of Plants in the Region
    DESCRIBE TABLE lt_plant_name LINES DATA(lv_count).

    * Write a plain output with the number of Plants in the Region

    WRITE: |Region “{ lc_regio }” has, { lv_count } Plant(s)|.

  29. TYPES: BEGIN OF lty_werks_name,
    plant TYPE werks_d,
    name TYPE name1,
    END OF lty_werks_name.

    TYPES: ltt_werks_name TYPE STANDARD TABLE OF lty_werks_name WITH EMPTY KEY.

    * Select Plant Info
    SELECT * FROM t001w
    INTO TABLE @DATA(lt_plant).
    * Sy-subrc check not required
    DATA(lv_regio) = CONV land1(‘TX’).
    * Loop through and save Plant and Name only

    DATA(lt_plant_name) = VALUE ltt_werks_name(
    FOR IN lt_plant
    WHERE ( regio = lv_regio )
    ( plant = -werks
    name = -name1 ) ).
    * Get the Number of Plants in the Region
    DESCRIBE TABLE lt_plant_name LINES DATA(lv_count).

    * Write a plain output
    WRITE: ‘Region’ , lv_regio, ‘has’, lv_count, ‘Plant(s)’.

  30. types:
    begin of lty_werks_name,
    plant type werks_d,
    name type name1,
    end of lty_werks_name.

    data:
    lt_plant type standard table of t001w,
    lv_regio type land1 value ‘TX’,
    lt_plant_name type standard table of lty_werks_name,
    lv_count type i.

    select * from t001w into table lt_plant.

    lt_plant_name = corresponding #( lt_plant mapping plant = werks name = name1 ).

    describe table lt_plant_name lines lv_count.

    write : ‘Region’ , lv_regio , ‘has’ , lv_count, ‘Plant(s)’.

  31. * declaration
    types: begin of lty_werks_name, werks type werks_d, name1 type name1, end of lty_werks_name.
    types lty_t_werks_name type standard table of lty_werks_name with empty key.
    constants: lc_regio type land1 value ‘TX’.

    * Select Plant Info
    select * from t001w into table @data(lt_plant).
    data(lt_plant_name) = value lty_t_werks_name( for ls_plant in lt_plant where ( regio = lc_regio ) ( werks = ls_plant-werks name1 = ls_plant-name1 ) ).
    *
    ** Write a plain output
    write: ‘Region’ , lc_regio, ‘has’, lines( lt_plant_name ), ‘Plant(s)’.

  32. christianguenter | August 4, 2017 at 4:02 am | Reply

    * Local Type Declaration
    TYPES: BEGIN OF lty_werks_name,
    plant TYPE werks_d,
    name TYPE name1,
    END OF lty_werks_name.

    * Local Data Declaration
    DATA: lt_plant TYPE STANDARD TABLE OF t001w,
    lv_regio TYPE land1 VALUE ‘TX’,
    lt_plant_name TYPE STANDARD TABLE OF lty_werks_name.

    * Select Plant Info
    SELECT * FROM t001w
    INTO TABLE lt_plant.
    * Sy-subrc check not required

    lt_plant_name = VALUE #( FOR IN lt_plant
    WHERE ( regio = lv_regio )
    ( plant = -werks
    name = -name1 ) ).

    * Write a plain output
    WRITE: ‘Region’ , lv_regio, ‘has’, lines( lt_plant_name ), ‘Plant(s)’.

  33. SELECT count(*) FROM t001w INTO @DATA(lv_lines) WHERE land1 EQ ‘DE’.
    WRITE: ‘Region DE has ‘ , lines( lv_lines ) , ‘ Plant(s)’.

  34. Peter Kübert | August 4, 2017 at 12:57 am | Reply

    SELECT werks, name1 FROM t001w INTO TABLE @DATA(lt_plant) WHERE land1 EQ ‘DE’.
    WRITE: ‘Region’ , ‘DE’, ‘has’ , lines( lt_plant ) , ‘Plant(s)’.

  35. TYPES: BEGIN OF lty_werks_name,
    werks TYPE werks_d,
    name1 TYPE name1,END OF lty_werks_name,
    tt_werks_name TYPE STANDARD TABLE OF lty_werks_name WITH DEFAULT KEY.

    DATA(lv_regio) = ‘LA’. SELECT * FROM t001w INTO TABLE @DATA(lt_plant).
    DATA(lt_plant_name) = CORRESPONDING tt_werks_name( lt_plant ).
    LOOP AT lt_plant into DATA(ls_plant) WHERE regio = lv_regio GROUP BY ( regio = ls_plant-regio size = GROUP
    SIZE ) ASCENDING ASSIGNING FIELD-SYMBOL() .
    WRITE: ‘Region’ , lv_regio , ‘has’, -size , ‘Plant(s)’.
    ENDLOOP.

  36. * Local Data Declaration
    CONSTANTS: lc_regio TYPE regio VALUE ‘TX’.

    * Select Plant Info
    SELECT werks, name1 FROM t001w
    INTO TABLE @DATA(lt_plant)
    WHERE regio EQ @lc_regio.

    * Sy-subrc check not required
    * Write a plain output with the number of Plants in the Region

    WRITE: |Region { lc_regio } has { sy-dbcnt }, Plant(s)|.

  37. Ananda_Babu_U | August 3, 2017 at 11:56 pm | Reply

    field symbols are not copying properly.

  38. Ananda_Babu_U | August 3, 2017 at 11:49 pm | Reply

    TYPES: BEGIN OF lty_werks_name,
    werks TYPE werks_d,
    name1 TYPE name1,
    END OF lty_werks_name,
    tt_werks_name TYPE STANDARD TABLE OF lty_werks_name WITH DEFAULT KEY.
    DATA(lv_regio) = ‘TX’.
    SELECT * FROM t001w INTO TABLE @DATA(lt_plant). “all t001w data
    DATA(lt_plant_name) = CORRESPONDING tt_werks_name( lt_plant ). “Plant and Plant Name data
    LOOP AT lt_plant into DATA(ls_plant) WHERE regio = lv_regio GROUP BY ( regio = ls_plant-regio size = GROUP SIZE )
    ASCENDING ASSIGNING FIELD-SYMBOL() .
    WRITE: ‘Region’ , lv_regio , ‘has’, -size , ‘Plant(s)’.
    ENDLOOP.

    • Ananda_Babu_U | August 3, 2017 at 11:54 pm | Reply

      TYPES: BEGIN OF lty_werks_name,
      werks TYPE werks_d,
      name1 TYPE name1,
      END OF lty_werks_name,
      tt_werks_name TYPE STANDARD TABLE OF lty_werks_name WITH DEFAULT KEY.
      DATA(lv_regio) = ‘TX’.
      SELECT * FROM t001w INTO TABLE @DATA(lt_plant).
      DATA(lt_plant_name) = CORRESPONDING tt_werks_name( lt_plant ).
      LOOP AT lt_plant into DATA(ls_plant) WHERE regio = lv_regio GROUP BY ( regio = ls_plant-regio size = GROUP SIZE )
      ASCENDING ASSIGNING FIELD-SYMBOL() .
      WRITE: ‘Region’ , lv_regio , ‘has’, -size , ‘Plant(s)’.
      ENDLOOP.

  39. DATA: lv_count TYPE I.

    PARAMETERS: p_regio TYPE regio obligatory DEFAULT ‘TX’.

    * Select Plant Info
    SELECT COUNT(*) FROM t001w
    WHERE regio EQ p_regio.
    WRITE: ‘Region’ , p_regio, ‘has’, sy-dbcnt, ‘Plant(s)’.

  40. Rameshwar Thokane | August 3, 2017 at 11:58 am | Reply

    TYPES: BEGIN OF lty_werks_name,
    plant TYPE werks_d,
    name TYPE name1,
    END OF lty_werks_name.

    * Local Data Declaration
    DATA: lv_regio TYPE land1 VALUE ‘TX’,
    lt_plant_name TYPE STANDARD TABLE OF lty_werks_name,
    ls_plant_name TYPE lty_werks_name.

    * Select Plant Info
    SELECT * FROM t001w
    INTO TABLE @DATA(lt_plant).
    * Sy-subrc check not required

    * Loop through and save Plant and Name only
    LOOP AT lt_plant INTO DATA(ls_plant) WHERE regio EQ lv_regio.
    MOVE CORRESPONDING ls_plant to ls_plant_name.
    APPEND ls_plant_name TO lt_plant_name.
    ENDLOOP.

    * Get the Number of Plants in the Region
    DESCRIBE TABLE lt_plant_name LINES DATA(lv_count).

    * Write a plain output
    WRITE: ‘Region’ , lv_regio, ‘has’, lv_count, ‘Plant(s)’.

  41. * Local Type Declaration
    TYPES: BEGIN OF lty_werks_name,
    plant TYPE t001w-werks,
    name TYPE t001w-name1,
    END OF lty_werks_name,
    ltty_werks_name TYPE STANDARD TABLE OF lty_werks_name WITH EMPTY KEY.
    * Select Plant Info
    SELECT * FROM t001w INTO TABLE @DATA(lt_plant).
    * Sy-subrc check not required
    DATA(lv_regio) = ‘TX’.
    * Loop through and save Plant and Name only
    DATA(lt_plant_name) = VALUE ltty_werks_name( FOR ls_plant IN lt_plant WHERE ( regio = lv_regio ) ( plant = ls_plant-werks name = ls_plant-name1 ) ).
    * Get the Number of Plants in the Region
    DATA(lv_count) = lines( lt_plant_name ).
    * Write a plain output
    WRITE: ‘Region’ , lv_regio, ‘has’, lv_count, ‘Plant(s)’.

  42. Venkateswara Rao | August 3, 2017 at 9:54 am | Reply

    TYPES:
    BEGIN OF lty_werks_name,
    plant TYPE werks_d,
    name TYPE name1,
    END OF lty_werks_name.

    * Local Data Declaration
    DATA:
    lt_plant_name TYPE STANDARD TABLE OF lty_werks_name.

    * Select Plant Info
    SELECT werks, name1
    INTO TABLE @DATA(lt_t001w)
    FROM t001w.

    * Sy-subrc check not required
    LOOP AT lt_t001w INTO DATA(ls_data) .
    APPEND VALUE #( plant = ls_data-werks name = ls_data-name1 ) TO lt_plant_name.
    * WRITE:/ ls_data-werks, ls_data-name1.
    ENDLOOP.

    * Write a plain output
    WRITE: ‘Total ‘, sy-dbcnt, ‘Plant(s)’, /.

  43. * Local Type Declaration
    TYPES: BEGIN OF lty_werks_name,
    plant TYPE t001w-werks,
    name TYPE t001w-name1,
    END OF lty_werks_name,
    ltty_werks_name TYPE STANDARD TABLE OF lty_werks_name WITH EMPTY KEY.
    * Select Plant Info
    SELECT * FROM t001w INTO TABLE @DATA(lt_plant).
    IF sy-subrc = 0.
    DATA(lv_regio) = ‘TX’.
    DATA(lt_plant_name) = VALUE ltty_werks_name( FOR ls_plant IN lt_plant WHERE ( regio = lv_regio ) ( plant = ls_plant-werks name = ls_plant-name1 ) ).
    * Get the Number of Plants in the Region
    DATA(lv_count) = lines( lt_plant_name ).
    * Write a plain output
    WRITE: ‘Region’ , lv_regio, ‘has’, lv_count, ‘Plant(s)’.
    ENDIF.

  44. TYPES: BEGIN OF lty_werks_name,
    plant TYPE werks_d,
    name TYPE name1,
    END OF lty_werks_name,
    ltt_werks_name TYPE STANDARD TABLE OF lty_werks_name WITH DEFAULT KEY.

    DATA: lv_regio TYPE land1 VALUE ‘TX’.

    SELECT * FROM t001w INTO TABLE @DATA(lt_plant).

    DATA(lt_plant_name) = VALUE ltt_werks_name(
    FOR ls_plant IN lt_plant WHERE ( regio = lv_regio )
    ( plant = ls_plant-werks NAME = ls_plant-name1 )
    ).
    WRITE:/ ‘Region’ , lv_regio, ‘has’, lines( lt_plant_name ), ‘Plant(s)’.

  45. * Select Plant Info
    SELECT * FROM t001w
    INTO TABLE @DATA(lt_plant).

    DATA(lv_regio) = ‘TX’.

    SELECT werks AS plant,
    name1 AS name
    FROM t001w
    INTO TABLE @DATA(lt_plant_name)
    WHERE regio = @lv_regio.

    * Get the Number of Plants in the Region
    DATA(lv_count) = LINES( lt_plant_name ).

    * Write a plain output
    WRITE: ‘Region’ , lv_regio, ‘has’, lv_count, ‘Plant(s)’.

  46. Hello SAPYard,

    Here is my version!

    **~ Local Type Declaration
    TYPES: BEGIN OF lty_werks_name,
    plant TYPE werks_d,
    name TYPE name1,
    END OF lty_werks_name.

    TYPES : ls_plant_name TYPE STANDARD TABLE OF lty_werks_name WITH DEFAULT KEY.

    **~ Local Data Declaration
    DATA(lv_regio) = ’08’.

    **~ Select Plant Info
    SELECT * FROM t001w INTO TABLE @DATA(lt_plant).
    **~ Sy-subrc check not required

    **~ Move the data to another table
    DATA(lt_plant_name) = VALUE ls_plant_name( FOR ls_plant IN lt_plant
    WHERE ( regio = lv_regio )
    ( plant = ls_plant-werks
    name = ls_plant-name1 ) ).

    **~ Get the Number of Plants in the Region
    DESCRIBE TABLE lt_plant_name LINES DATA(lv_count).

    **~ Output
    WRITE: ‘Region’ , lv_regio, ‘has’, lv_count, ‘plant(s)’.

  47. B Kesava mani Kumar | August 3, 2017 at 5:31 am | Reply

    My code as follows…..

    *******************************************************************************************************

    * Local Type Declaration
    TYPES: BEGIN OF lty_werks_name,
    plant TYPE werks_d,
    name TYPE name1,
    END OF lty_werks_name,

    ls_plant_name TYPE STANDARD TABLE OF lty_werks_name WITH EMPTY KEY.

    * Local Data Declaration
    DATA(lv_regio) = ‘TX’.

    DATA lv_count TYPE i.

    * Select Plant Info
    SELECT * FROM t001w INTO TABLE @DATA(lt_plant).
    * Sy-subrc check not required

    *Segregate the data with Plant & Plant name only
    DATA(lt_plant_name) = VALUE ls_plant_name( FOR ls_plant IN lt_plant
    WHERE ( regio = lv_regio )
    ( plant = ls_plant-werks name = ls_plant-name1 ) ).

    * Get the Number of Plants in the Region
    DESCRIBE TABLE lt_plant_name LINES lv_count.

    * Write a plain output
    WRITE: ‘Region’ , lv_regio, ‘has’, lv_count, ‘Plant(s)’.

    ***********************************************************************************************************

  48. * Local Data Declaration
    CONSTANTS: lc_regio TYPE regio VALUE ‘TX’.

    * Select Plant Info
    SELECT werks, name1 FROM t001w
    INTO TABLE @DATA(lt_plant)
    WHERE regio EQ @lc_regio.

    * Sy-subrc check not required
    * Write a plain output with the number of Plants in the Region

    WRITE: |Region { lc_regio } has { sy-dbcnt }, Plant(s)|.

  49. B Kesava mani Kumar | August 3, 2017 at 3:25 am | Reply

    Sorry, I didn’t consider the note….let me try again

  50. Hello SAPYard,

    Here is my version!

    **~ Declarations
    DATA(lv_regio) = ’08’.
    **~ Select the Plant information
    SELECT COUNT(*) INTO @DATA(lv_count) FROM t001w
    WHERE regio = @lv_regio.

    **~ Write the output
    WRITE: ‘Region’ , lv_regio, ‘has’, lv_count, ‘Plant(s)’.

  51. B Kesava Mani Kumar | August 3, 2017 at 12:55 am | Reply

    * Local Data Declaration
    DATA: lv_regio TYPE land1 VALUE ‘TX’.

    * Select Plant Info
    SELECT count( distinct regio ) as cnt FROM t001w
    INTO @data(l_count) where regio eq @lv_regio.
    * Sy-subrc check not required

    * Write a plain output
    WRITE: ‘Region’ , lv_regio, ‘has’, l_count, ‘Plant(s)’.

  52. Venkateswara Rao | August 2, 2017 at 10:19 pm | Reply

    If you want to print the internal table data as well, use below code

    SELECT werks, name1
    INTO TABLE @DATA(lt_plant_name)
    FROM t001w.

    * Sy-subrc check not required

    * Write a plain output
    WRITE: ‘Total ‘, sy-dbcnt, ‘Plant(s)’, /.

    LOOP AT lt_plant_name INTO DATA(ls_data).
    WRITE:/ ls_data-werks, ls_data-name1.
    ENDLOOP.

  53. Venkateswara Rao | August 2, 2017 at 10:14 pm | Reply

    * Select Plant Info
    SELECT werks, name1
    INTO TABLE @DATA(lt_plant_name)
    FROM t001w.

    * Sy-subrc check not required

    * Write a plain output
    WRITE: ‘Total ‘, sy-dbcnt, ‘Plant(s)’.

  54. Hi Raju,
    Though my current ABAP version below 7.4, still trying my best to send this code snippet.

    */———————————————————————
    REPORT ZFGB_TEST.

    * Local Type Declaration
    TYPES: BEGIN OF lty_werks_name,
    plant TYPE werks_d,
    name TYPE name1,
    END OF lty_werks_name.

    * Local Data Declaration
    DATA: lt_plant TYPE STANDARD TABLE OF t001w,
    lv_regio TYPE land1 VALUE ‘NCR’.

    * Select Plant Info
    SELECT werks name1
    FROM t001w
    INTO CORRESPONDING FIELDS OF TABLE lt_plant
    WHERE regio EQ lv_regio.

    * Write a plain output
    WRITE: ‘Region’ , lv_regio, ‘has’, sy-dbcnt, ‘Plant(s)’.

    */=======================================================================

    Regards.

Leave a Reply to Jefferson Cancel reply

Your email address will not be published.


*