Search the table Descending order

vios15vios15 Member Posts: 43
Dear All,

I am working on a coding to read a table but I need it to be in a descending order.

Reason being, I want the latest record to be read first.

Has anyone know how I should put in the coding in order to read descending?

Thanks

Answers

  • zzhengzzheng Member Posts: 30
    [IsAscending] := Record.ASCENDING([SetAscending])

    IF Record.FIND('+') THEN


    IF Record.FINDLAST THEN
  • kinekine Member Posts: 12,562
    zzheng wrote:
    IF Record.FINDLAST THEN

    This is not good for looping. Never use FINDFIRST and FIRSTLAST when looping through table.

    And going through table backwards is not good for performance on MS SQL at all...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • vios15vios15 Member Posts: 43
    Hi,

    Could you provide a sample coding on the ascending on how to use

    [IsAscending] := Record.Ascending ([SetAscending]}

    Thanks
  • garakgarak Member Posts: 3,263
    Do you has read the help in C/AL Symbol Menu?
    YourRecVariable.setcurentkey(YourKeyField(s));
    YourRecVariable.ascending(false);
    if YourRecVariable.findset(false,false) then begin
      repeat
      until YourRecVariable.next = 0;
    end;
    

    But as kine said, it's not good for performance (if you have many recs in your table.

    If you need a descending key often and you are under SQL you can also define in SQL Managm. Studio a descending index on your table.

    Regards
    Do you make it right, it works too!
  • vios15vios15 Member Posts: 43
    Hi,

    Thanks for the advice.

    I only need to read once only and then out of the loop.

    Regards
  • WaldoWaldo Member Posts: 3,412
    So you only need the last record, or the two last records?

    Eric Wauters
    MVP - Microsoft Dynamics NAV
    My blog
  • vios15vios15 Member Posts: 43
    Hi,

    E.g in the table I have the following data;
    Part-number1 EntryNo1
    Part-number2 EntryNo2
    Part-number3 EntryNo3
    Part-number4 EntryNo4
    Part-number1 EntryNo5 <- I need to pick-up this record instead of the one at the top as this is the latest of Part-number1
    Part-number5 EntryNo6

    As you can see by reading descending order, I can pick up the latest of Part-number1 transaction.

    Hope it clarify my intention.

    Regards
  • kinekine Member Posts: 12,562
    You mean something like
    MyRecVar.SETCURRENTKEY("Part-Number","ENtry No");
    MyRecVar.SETRANGE("Part-number","Part-number1");
    if MyRecVar.FINDLAST then...
    
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • vios15vios15 Member Posts: 43
    Hi,

    Unfortunately, my version do not have "FINDLAST".

    Regards
  • idiotidiot Member Posts: 651
    use FIND ('+')
    NAV - Norton Anti Virus

    ERP Consultant (not just Navision) & Navision challenger
  • garakgarak Member Posts: 3,263
    Then use find('+').
    Do you need to modify the last rec or not? If yes set a locktable before, so the server knows, that you will modify the rec.

    Regards
    Do you make it right, it works too!
  • vios15vios15 Member Posts: 43
    Hi,

    No modification is required, just reading only.
  • garakgarak Member Posts: 3,263
    then find('+') and before your needed key (if the needed key != primary key)
    Do you make it right, it works too!
  • vios15vios15 Member Posts: 43
    Dear All.

    Thanks for the great help.

    My problem has being resolved.

    Regards
Sign In or Register to comment.