Resetting a Numeric Field with Property AutoIncrement=YES

ta5ta5 Member Posts: 1,164
Hi
We have some fields with the Property AutoIncrement=YES. For example used as PK for a log entry. After a while customer can delete the log entries, because they are not used anymore. It would be nice if there is an easy possiblity to reset the Counter to Zero.
One example here
mohana-dynamicsnav.blogspot.ch/2014/03/reset-autoincrement-field-in-nav.html

Afaik this is possible with SQL Command only. Right? Any suggestions or work arounds?
Thx in advance, greatly appreciated.
Thomas

Best Answer

Answers

  • Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    edited 2018-04-30
    SQL: DBCC CHECKIDENT table_name RESEED new_reseed_value
    NAV:
    1. Edit table, take off Autoincrement=Yes
    2. Save table
    3. Edit table, add new field with Autoincrement=Yes, and make it PK
    4. Save table
    5. Clear all values to 0 in the original field
    6. Edit table, Autoincrement=No on artificial feld, save table
    7. set Autoincrement=Yes and PK on the original field, remove the artificial field.
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • ta5ta5 Member Posts: 1,164
    Thanks Slawek
    This means, not possible from within NAV, right?
  • NavNabNavNab Member Posts: 181
    Hello @ta5

    You can do it in NAV atone condition "The user who runs the code should be dbowner on SQL". The code should be like this:
    MyTable.SETFILTER(MyAutoIncField, '<>%1', 0); // If needed
    IF MyTable.FINDSET THEN
      REPEAT
        CLEAR(MyTable.MyAutoIncField); // Or whatever number you want to assign to your field...
        MyTable.MODIFY(TRUE);
      UNTIL MyTable.NEXT = 0;
    

    https://docs.microsoft.com/en-us/dynamics-nav/autoincrement-property
    imw5g99nq8bp.png

  • ta5ta5 Member Posts: 1,164
    Hi Slawek and NavNab
    Thanks for your postings. Ok for the moment, I'll take that into consideration for further proceedings.
    Kind regards and thanks again.
    Thomas
Sign In or Register to comment.