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 Ztable

Former Member
0 Kudos

Hi All,

Currently i have a z-table with records.

How do i perform a sort with one of the fields before i do a Loop through all the records?

If i were to do a SORT <ZTABLE> BY <FIELD>, en error will occur indicating that this is not an internal table - the "OCCURS n" specification is missing.

Can some experts advise on how to solve this?

Thanks.

Regards,

JL

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi, do u know what is the problem to sort an ztable created in SE11??

Actually it is a database table and we cannot sort a database table directly ,but we can bring all data in the ztable in an interbnal table and then we can perform the sort technique on any of the field.

you declare an internal table like the structure of ztable and select all data from database table to the internal table.

Data : i_table type standard table of ztable.

start-of-selection.

*select * from ztable into table i_table.*

Now apply sorting on the internal table.

Then modify the ztable from the internal table.

Please have a look in the answer. hope this will help.

regards

jayati.

11 REPLIES 11

peter_ruiz2
Active Contributor
0 Kudos

hi,

have declared it as an internal table in your program?

declare it like this.

DATA: ztable TYPE STANDARD TABLE OF ztable WITH HEADER LINE.

regards,

Peter

peter_ruiz2
Active Contributor
0 Kudos

hi,

have declared it as an internal table in your program?

declare it like this.

DATA: ztable TYPE STANDARD TABLE OF ztable WITH HEADER LINE.

regards,

Peter

Former Member
0 Kudos

Hi,

First bring all the data of z table into an internal table of type 'STANDARD TABLE OF <Z table>'.

Sort the internal table and then modify the z table from that internal table.

Former Member
0 Kudos

hi,

check out this link it will help you:

Hope it will help you

Thanks and Regards

Rahul Sharma

Former Member
0 Kudos

Hi,

u can use this code

DATA: BEGIN OF line,

t1(4) TYPE c,

t2 TYPE i,

END OF line.

DATA : tot TYPE I,

GRTOT TYPE I.

*DATA itab LIKE HASHED TABLE OF line WITH UNIQUE KEY t1.

DATA : ITAB LIKE TABLE OF LINE .

line-t1 = 'aaaa'.

line-t2 = 1.

INSERT line INTO TABLE itab.

line-t1 = 'aaaa'.

line-t2 = 1.

INSERT line INTO TABLE itab.

line-t1 = 'aaaa'.

line-t2 = 1.

INSERT line INTO TABLE itab.

line-t1 = 'bbbb'.

line-t2 = 2.

INSERT line INTO TABLE itab.

line-t1 = 'bbbb'.

line-t2 = 2.

INSERT line INTO TABLE itab.

line-t1 = 'bbbb'.

line-t2 = 2.

INSERT line INTO TABLE itab.

line-t1 = 'cccc'.

line-t2 = 3.

INSERT line INTO TABLE itab.

line-t1 = 'cccc'.

line-t2 = 3.

INSERT line INTO TABLE itab.

line-t1 = 'cccc'.

line-t2 = 3.

INSERT line INTO TABLE itab.

line-t1 = 'aaaa'.

line-t2 = 1.

INSERT line INTO TABLE itab.

SORT itab.

PERFORM loop_at_itab.

FORM loop_at_itab.

SORT itab by t1.

LOOP AT itab INTO line.

On change of line-t1.

if sy-tabix = 1.

else.

ULINE.

WRITE: /'Sum:', 10 tot.

GRTOT = GRTOT + TOT.

clear tot.

ULINE.

endif.

endon.

WRITE 😕 line-t1,line-t2.

tot = tot + line-t2.

ENDLOOP.

ULINE.

WRITE:/ 'Sum:', 10 tot.

ULINE.

ULINE.

GRTOT = GRTOT + TOT.

WRITE:/ 'GR.TOT:', 10 GRtot.

ULINE.

SKIP.

ENDFORM.

Hope this helps

Regards,

Manish

Former Member
0 Kudos

Hi All,

Sorry for any misunderstanding.

I created my z-table using SE11. So there is no need to declare again in my program right?

so, can i still sort all the records when i loop through this table?

0 Kudos

Hi

u cannot sort a Ztable created in Se11 directly from ur program.

first declare an internal table type ztable and then sort the internal table.

Regards,

naveen

0 Kudos

Hi Chong,

You need to define <Internal Table> TYPE <Z-TABLE> in program:

1. Define <Internal Table> TYPE <Z-TABLE> in program

2. Move <z-table> data to <internal table>

3. Sort <internal table>

Regards,

Moqeeth.

Former Member
0 Kudos

Hi, do u know what is the problem to sort an ztable created in SE11??

Actually it is a database table and we cannot sort a database table directly ,but we can bring all data in the ztable in an interbnal table and then we can perform the sort technique on any of the field.

you declare an internal table like the structure of ztable and select all data from database table to the internal table.

Data : i_table type standard table of ztable.

start-of-selection.

*select * from ztable into table i_table.*

Now apply sorting on the internal table.

Then modify the ztable from the internal table.

Please have a look in the answer. hope this will help.

regards

jayati.

Former Member
0 Kudos

hello,

you cant directly sort a DB table .... for this u have to declare a internal table in your program of type that ZTABLE with occurs 0 with header line specification....

then through a select query get the data in the ZTABLE into INT_ZTABLE i.e. the internal table of type ZTABLE and then sort the int_ZTABLE by field u want and do ur computation on the internal table and then update the DB table with internal table

Former Member
0 Kudos

Hi,

Please make use of occurs 0 with header line in declaration of the internal table.

Thanks,

Sriram Ponna.