Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Autoincrementing according to sample
Message
From
02/10/2001 10:19:10
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
 
To
02/10/2001 09:58:07
Victor Verheij
International Film Festival Rotterdam
Rotterdam, Netherlands
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00562909
Message ID:
00562995
Views:
15
>hello again
>I did as you suggested. The nextid in ids.dbf for table3 are now nicely replaced. So this part seems to work now. However, I still get the datatype mismatch error. So table3 doesn't receive the nextid. the question marks are comming out of my ears, so to speek. Maybe your newid() is the solutions, i so very much hope so.

Victor,
I sent my version below. In yours mystery is hanging (since you said structures were same I assume already Id field was integer). You could make a copy of DBC and tables, drop all but ids and last table you created. Then execute :
do home()+'gendbc\gendbc'
Paste generated code here so we could see what's happening.



My version uses a slightly different ids table. You can either modify existing to match or can create it with this command :
open database < DatabaseNameHere >
create table ids (TableName c(30), CurId i)
index on upper(TableName) tag TableName
And add this to stored procedures (possibly replace current NewID if it's there).
*DBC Stored proc
FUNCTION NewID
	LPARAMETERS tcTableName
	LOCAL lcTableName, lnOldArea, lcOldReprocess
	lnOldArea = SELECT()
	lnOldReprocess = SET('REPROCESS')
	* Uppercase Alias name
	lcTableName = upper(iif(parameters() = 0, alias(), tcTableName))
	* Lock reprocess - try once
	SET REPROCESS TO 1
	IF !USED("IDS")
		USE ids IN 0
	ENDIF
	* If no entry yet create and return 1
	IF !SEEK(lcTableName, "Ids", "tablename")
		INSERT into ids (TableName, CurId) values (lcTableName,1)
		SELECT (lnOldArea)
		SET REPROCESS TO lnOldReprocess
		RETURN ids.CurId
	ENDIF
	* Lock, increment id, unlock, return CurId value
	DO while !RLOCK('ids')
		* Delay before next lock trial
		lnStart = seconds()
		DO while seconds()-lnStart < 0.01
		ENDDO
	ENDDO
	* Increment
	REPLACE ids.CurId WITH ids.CurId + 1 in 'ids'
	UNLOCK in 'ids'
	SELECT (lnOldArea)
	SET REPROCESS TO lnOldReprocess
	RETURN ids.CurId
ENDFUNC
For any table you create add a PKId field (you might name it as TableName_Id but be sure to check it's indexed and for ease of use name index tag PKId). For Id field's default enter expression :
NewId('< tablenamehere_NOT_alias >')

Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Reply
Map
View

Click here to load this message in the networking platform