Recently I had to create database records outside of Dynamics AX. I used SQL to move some records from ISV tables to new standard tables within AX. There was a ton of data, so it was much faster to insert these records using SQL. Initially, I overlooked one important factor. When I created these records outside of the AX environment, the SystemSequences table was not updated.
The SystemSequences table stores the next RecID by table in the AX environment: https://msdn.microsoft.com/en-us/library/systemsequences.aspx
If this table is not updated properly, the users will see duplicate record errors that may not make sense to you. The users had sent me this error below. When I looked at the data, the Load ID did not exist. It didn’t throw an error about Rec IDs, so it’s a bit misleading.
The issue was that I had created records in the WHSLoadTable outside of AX. The SystemSequences table wasn’t updated, so the next value of the Rec ID was incorrect and throwing an error. In this situation, you must update the SystemSequences table. You’ll need to find the max value for the table and update to the max value + 1.
Here is a portion of the SQL used to fix this issue:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
DECLARE @MaxRecID BIGINT DECLARE @NextVal BIGINT SELECT @MaxRecID = MAX(RECID) FROM WHSLoadTable SELECT @NextVal = NEXTVAL FROM SYSTEMSEQUENCES INNER JOIN SQLDICTIONARY ON SQLDICTIONARY.FIELDID = 0 AND SQLDICTIONARY.name = 'WHSLoadTable' AND SQLDICTIONARY.TABLEID = SYSTEMSEQUENCES.TABID IF (@NextVal > @MaxRecID) BEGIN PRINT 'WHSLoadTable did not need to be updated.' END ELSE BEGIN PRINT 'Updated WHSLoadTable from ' + CONVERT(VARCHAR(MAX), @NextVal) + '' to '' + CONVERT(VARCHAR(MAX), @MaxRecID + 1) UPDATE SYSTEMSEQUENCES SET NEXTVAL = @MaxRecID + 1 FROM SYSTEMSEQUENCES INNER JOIN SQLDICTIONARY ON SQLDICTIONARY.FIELDID = 0 AND SQLDICTIONARY.name = 'WHSLoadTable' AND SQLDICTIONARY.TABLEID = SYSTEMSEQUENCES.TABID END |
I hope this helps you out!
Presume that 400 records need to be inserted to a table. The process would HAVE TO be inside of a cursor because I would have to update both myTable and SYSTEMSEQUENCES tables one record at a time.
I dislike cursors. LOL! Is this assessment correct?
How would you ensure concurrency problems did not occur when inserting records?
Hello Peter,
Actually for your first question, you could insert your 400 records and then after you’ve inserted the records, update the one systemsequences table record that exists for the table you just inserted the records for. You will need to update the nextval to be the appropriate number, but only just the one time.
In regards to your second question, you are going to want to make sure you are inserting unique records (paying attention to the primary key of the table). On the insert method, Nicole had a recID that ensured all of her records were unique. SQL should be able to handle all of that for you, but it’s safest to ensure you are using the primary index.
Hope this helps, thank you.
Brandon