Application Development 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: 

Sort internal table in Subroutine

Former Member
0 Kudos

Hi,everyone.

I want to sort internal table in subroutine , but there is an error.My code is below.

TYPES: BEGIN OF TYP_SAKNR,

SAKNR LIKE ZTEFI0710-SAKNR,

END OF TYP_SAKNR.

DATA: TAB_SAKNR TYPE STANDARD TABLE OF TYP_SAKNR,

WA_SAKNR TYPE TYP_SAKNR.

START-OF-SELECTION.

PERFORM GET_SAKNR USING TAB_SAKNR.

LOOP AT TAB_SAKNR INTO WA_SAKNR.

WRITE: / WA_SAKNR-SAKNR.

ENDLOOP.

FORM GET_SAKNR USING TAB_SAKNR TYPE STANDARD TABLE.

SELECT SAKNR

FROM ZTEFI0710

INTO TABLE TAB_SAKNR

WHERE ZZEURENKETU = '2'.

<b> SORT TAB_SAKNR BY SAKNR.</b>

ENDFORM.

If I just write SORT TAB_SAKNR, it's ok.

Can you tell me why?And how to solve this problem?

This is an example,if in the internal table there are many fields,and I want to sort this internal table by a few fields in subroutine,what should I do?

Regards,

feng.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

You need to pass the table using "Tales" key word. something like

data: wa_saknr type typ_saknr.

PERFORM GET_SAKNR tables TAB_SAKNR.

FORM GET_SAKNR tables TAB_SAKNR structure wa_saknr.

endform.

Reward if useful.

6 REPLIES 6

former_member223537
Active Contributor
0 Kudos

TYPES: BEGIN OF TYP_SAKNR,
SAKNR LIKE ZTEFI0710-SAKNR, 
END OF TYP_SAKNR.
DATA: TAB_SAKNR TYPE STANDARD TABLE OF TYP_SAKNR,
WA_SAKNR TYPE TYP_SAKNR.

START-OF-SELECTION.
PERFORM GET_SAKNR USING TAB_SAKNR.
LOOP AT TAB_SAKNR INTO WA_SAKNR.
WRITE: / WA_SAKNR-SAKNR.
ENDLOOP.

FORM GET_SAKNR USING TAB_SAKNR TYPE STANDARD TABLE OF TYP_SAKNR. " Specify the type of Standard table as TYP_SAKNR
SELECT SAKNR
FROM ZTEFI0710
INTO TABLE TAB_SAKNR
WHERE ZZEURENKETU = '2'.

SORT TAB_SAKNR BY SAKNR.
ENDFORM.


0 Kudos

Thanks,Prashant Patil .

I tried,but it didn't work.

Regards,

feng.

Former Member
0 Kudos

You need to pass the table using "Tales" key word. something like

data: wa_saknr type typ_saknr.

PERFORM GET_SAKNR tables TAB_SAKNR.

FORM GET_SAKNR tables TAB_SAKNR structure wa_saknr.

endform.

Reward if useful.

Former Member
0 Kudos

Table category of an internal table that is managed using a table index and is <b>always sorted according to its table key</b>. The corresponding generic data type is sorted table.

<b>I want to sort this internal table by a few fields in subroutine,what should I do?</b>

It is not possible to sort the table with any other sort criteria except table key. Otherwise it will give a error. What you can do, inside the subroutine define a standard internal table with same structure and copy the sorted table to that standard table and use it.

IT_sorted_table[] = IT_standard_table[].

and use IT_standard_table[] instead of IT_sorted_table[].

Former Member
0 Kudos

Hi Feng,

Use table type in subroutine.

Refer this code :

TYPES: BEGIN OF TYP_SAKNR,

SAKNR LIKE ZTEFI0710-SAKNR,

END OF TYP_SAKNR.

<b>TYPES : ttyp_saknr type standard table of TYP_SAKNR.</b>

data : TAB_SAKNR type ttyp_saknr .

FORM GET_SAKNR USING TAB_SAKNR TYPE <b>ttyp_saknr</b>

SELECT SAKNR

FROM ZTEFI0710

INTO TABLE TAB_SAKNR

WHERE ZZEURENKETU = '2'.

SORT TAB_SAKNR BY SAKNR.

ENDFORM.

Now it would work.

Regards,

Hemant

Former Member
0 Kudos

Hi

If u want to sort the internal table by few fields just give

sort it_matnr by f1 f2 ... fn

where f1 , f2 ... fn are the fields on which u want to sort the internal table.