Reputation: 139
I have an assignment that will insert records to a foxpro table. There is a column called “submitid”, but it is not a primary key. I will have to insert a number by increment of max(submitid) + 1. What if two user submit the same query at the same time. Would it has the same duplicated submitid?
Upvotes: 1
Views: 691
Reputation: 6557
You could use OLEDB CommandText to accomplish this by using VFP commands and functions to get the next number and insert the record.
Below is pseudo but you should get the idea. The "EXECS" is a VFP function that allows you to execute native VFP commands and functions.
oCommand.CommandText = "EXECS([USE MyTable AGAIN SHARED IN 0] + chr(13) + [FLOCK("MyTable")] + chr(13) + ["get number"] + chr(13) + [INSERT INTO MyTable (col1, col2) VALUES (nextid, othervalue) ] )"
Upvotes: 4
Reputation: 1351
With concurrent users you could end up duplicating the id. One of the ways I had seen this dealt with in FoxPro was a table to track the current id, much like a sequence in postgres, and work off of that instead of using Max. Would also involve locking the table before retrieving and incrementing the value if you wanted to be certain.
Upvotes: 2