General information
Category:
Databases,Tables, Views, Indexing and SQL syntax
Title:
NextID for SQL syntax
What is the correct way (record locking,etc.) to make a "NextID" function for sql? I am using the following, but can forsee problems if tons of users bang on this thing at the same time:
function NextID(pTable)
select lastval from dbsequences into array laNum where tablename = pTable
laNum[1] = laNum[1] + 1
update dbsequences set lastval = laNum[1] where tablename = pTable
RETURN(lnNum[1])
This type of thing works fine for few users. My question is what happens when two users hit this thing at about the same time? Both will get the same number and that will cause a big problem. Somehow the record should be locked,updated,unlocked to prevent this. I know how to do it using normal VFP commands: locate for..., lock(), replace ,unlock(). etc., etc. I just can't figure out how it's done in the SQL world.
- Gary
Next
Reply
View the map of this thread
View the map of this thread starting from this message only
View all messages of this thread
View all messages of this thread starting from this message only