General information
Category:
Databases,Tables, Views, Indexing and SQL syntax
Hi Craig,
Heres my stored procedure which allocates primary keys and is giving occasional duplicates in a multi-user environment.
*-- Generate a Unique ID
FUNCTION NewID(tcAlias, tnDefaultValue, tcPrefix, tlLen)
LOCAL lcAlias, ;
lnID, ;
lcOldReprocess, ;
lnOldArea
lnOldArea = SELECT()
IF EMPTY(tcAlias)
lcAlias = UPPER(ALIAS())
ELSE
lcAlias = UPPER(tcAlias)
ENDIF
lcOldReprocess = SET('REPROCESS')
IF !USED("at_setup")
USE at_setup IN 0
ENDIF
SELECT at_setup
IF SEEK(lcAlias, "at_setup", "key_name")
*-- Lock until user presses Esc
SET REPROCESS TO AUTOMATIC
*-- We MUST obtain a lock here so we
*-- can be certain we have a reference
*-- to return.
DO WHILE !RLOCK()
ENDDO
REPLACE at_setup.value WITH at_setup.value + 1
lnID = at_setup.value
UNLOCK
SET REPROCESS TO lcOldReprocess
ELSE
*-- If seek failed, add new table to our setup table
*-- and set the default value being used.
APPEND BLANK
REPLACE at_setup.key_name WITH lcAlias, ;
at_setup.value WITH tnDefaultValue
lnID = tnDefaultValue
ENDIF
USE
SELECT (lnOldArea)
IF EMPTY(tcPrefix)
RETURN lnID
ELSE
RETURN tcPrefix + PADL(lnID, tlLen-LEN(tcPrefix), "0")
ENDIF
ENDFUNC
Any ideas greatly appreciated.
Paul.
Previous
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