8 Replies Latest reply: Mar 10, 2007 8:54 AM by sunil kumar RSS

Inserting in sorted internal table

Aarthy Aiyer
Currently Being Moderated

Hi folks,

I have created a sorted internal table of type MARA and have populated the internal table with values from the MARA table. Now, I have to insert a record into the internal table. If I insert the record using INSERT statement, it is giving me a runtime error. I know I have to use the sort key somewhere in the syntax, but am not able to figure it out. Can anyone give me a snippet of code to do the insert functionality into a sorted table.

 

Thanks in advance.

Helpful answers will be rewarded.

  • Re: Inserting in sorted internal table
    Utsah Garg
    Currently Being Moderated

    Instead of using the INSERT Statement use APPEND and then sort the whole table again.

  • Re: Inserting in sorted internal table
    Raghavender Vadakattu
    Currently Being Moderated

    Hi Aarthy,

                       

     

    INSERT wa INTO TABLE table_name.

     

    wa -


    > Work Area

     

    Here, You must use the TABLE keyword, so that u can avoid runtime error.This will work your internal table i.e., Sorted table.

     

    You can not use INDEX keyword INSERT statement.OK.

    If you have any further queries, you are welcome.

     

    if problem solved, reward.

    Hope, the problem solved.

     

    Regards,

    V.Raghavender.

     

    Message was edited by:

            Raghavender Vadakattu

  • Re: Inserting in sorted internal table
    NimeshKumar Kapadia
    Currently Being Moderated

    Hi Arthy,

     

    Can you paste the existing code with syntax you used for creating sorted table ?

     

    Regards

    Kapadia

  • Re: Inserting in sorted internal table
    pavan pachimatla
    Currently Being Moderated

    for a sorted table you can append values using the statement.... APPEND..SORTED BY....

    here is the example.....

     

    Example

    Creating a ranking of the three flights of a connection showing the most free seats.

     

    PARAMETERS: p_carrid TYPE sflight-carrid,

                p_connid TYPE sflight-connid.

     

    DATA: BEGIN OF seats,

            fldate TYPE sflight-fldate,

            seatsocc TYPE sflight-seatsocc,

            seatsmax TYPE sflight-seatsmax,

            seatsfree TYPE sflight-seatsocc,

          END OF seats.

     

    DATA seats_tab LIKE STANDARD TABLE OF seats

                   INITIAL SIZE 3.

     

    SELECT fldate seatsocc seatsmax

           FROM sflight

           INTO seats

           WHERE carrid = p_carrid AND

                 connid = p_connid.

      seats-seatsfree = seats-seatsmax - seats-seatsocc.

      APPEND seats TO seats_tab SORTED BY seatsfree.

    ENDSELECT.

  • Re: Inserting in sorted internal table
    Neha Bansal
    Currently Being Moderated

    Hi Arthy!

     

    Inserting a record in sorted table can be done by using TABLE keyword in your INSERT statement, like,

     

    INSERT <record> INTO TABLE <table name>.

     

    It will not give any run-time error and the record will be inserted at an appropriate position , i.e. in sorted order.

     

    Regards,

    Neha Bansal.

  • Re: Inserting in sorted internal table
    VIVEKANAND MEGHMALA
    Currently Being Moderated

    Hii,

    Its really very simple ,you know sorted tables are itself sorted,you just need to append the record into the table it will automatically sort it ,no need to write any sort or binary search statement after it,this is the benifit of sorted tables, i think ur problem is solved.

     

     

    one more thing if you are appending records one or two times then its ok go ahead but if your requirement is to append records very frequently then better go for standard tables and use statement SORT ITAB BINARY SEARCH  after that,

     

    and further you cannot enter record into a sorted table on your specified index becouse it will sort it always according to the primary key(s) so it may not come at that position what you gave,

     

    sorted tables can be a performance issue if you append records again and again.

     

    please give details about your RUNTIME DUMP,

     

     

    thanks

    vikas

  • Re: Inserting in sorted internal table
    sunil kumar
    Currently Being Moderated

    The sorted tables are having the feature that the recorted to be inserted into sorting order.

       the case that if u manually enter the record in correct posotion it well and good.

      if not before inserting the record into sorted table the data will be sorted.which is happend in data base tables.

      see an example of this code.

     

    DATA FS_TABLE LIKE MARA.

    DATA: T_TABLE LIKE SORTED TABLE OF FS_TABLE WITH UNIQUE KEY MATNR.

    SELECT *

    FROM MARA

    INTO FS_TABLE

    UP TO 10 ROWS

      .

    INSERT FS_TABLE INTO TABLE T_TABLE.

    ENDSELECT.

     

     

    if it is advantages to u justify properly.

Actions