Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Automatic Primary Key Value
Message
De
07/08/2001 10:57:22
Hilmar Zonneveld
Independent Consultant
Cochabamba, Bolivie
 
 
À
07/08/2001 10:52:33
Information générale
Forum:
Visual FoxPro
Catégorie:
Base de données, Tables, Vues, Index et syntaxe SQL
Divers
Thread ID:
00540694
Message ID:
00540700
Vues:
27
>Just beginning to delve into SQL syntax in VFP and would like to create a primary (numeric) key in a table. I would also like this field to simply be an integer value which increments by 1 each time a new record is added.
>
>Some SQL languages have a definition which causes this to happen. I haven't seen any reference to something like this so far. Is there anything like this in VFP ?
>
>If not, is there a recommended method for handling this ? Separate FREE table to store next value in ? Table within the database ?

Let's say your table is called "client". In the default value of the field, type SerialNumber("client"). Place function SerialNumber in stored procedures. Create the appropriate table (sequence C(20), NextNum I).

HTH, Hilmar.
**********************************************************************
FUNCTION SERIALNUMBER(tnSequence)
	* Get serial number. Used mainly to generate primary keys.
	* The easiest way to achieve this is to call this function from a fields default value.
	tnSequence = lower(tnSequence)
	local lnSelect
	lnSelect = select()
	if used("serialnumber")
		select serialnumber
	else
		select 0
		use serialnumber
	endif
	set order to "sequence"
	seek padr(tnSequence, len(sequence))
	if not found()
		append blank
		replace sequence with tnSequence, nextnum with 1
	endif
	local lnReturnValue
	if lock()
		lnReturnValue = nextnum
		replace nextnum with nextnum + 1
	else
		lnReturnValue = -1
	endif
	unlock
	select (lnSelect)
	return lnReturnValue
ENDFUNC
Difference in opinions hath cost many millions of lives: for instance, whether flesh be bread, or bread be flesh; whether whistling be a vice or a virtue; whether it be better to kiss a post, or throw it into the fire... (from Gulliver's Travels)
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform