Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Primary Keys
Message
De
26/07/2001 10:28:08
Hilmar Zonneveld
Independent Consultant
Cochabamba, Bolivie
 
Information générale
Forum:
Visual FoxPro
Catégorie:
Base de données, Tables, Vues, Index et syntaxe SQL
Titre:
Divers
Thread ID:
00535657
Message ID:
00535685
Vues:
15
>In my dbc I have a procedure that gets the primary key. It all works great but I would like to make it more generic if possible. In the default value of the prikey field I have GetPriKey("tablename").
>
>Is there a way to replace "tablename" with something like THIS so that if a table gets renamed it will still work? It probably will never happen but I would like to have a better approach.
>
>I considered getting ALIAS() in the top of GetPriKey() and use that but I would not feel confident that ALIAS would always return the table I am adding records to.
>
>This is not a critical problem but something I have been wondering about.
>
>Looking forward to meeting more of you in San Diego.
>
>Thanks...
>Tommy

GetPriKey("MyTable") would pressumably SEEK("MyTable") in your list of sequences. Renaming the table would not cause any problem - the sequence would still be used. On the contrary, using a different sequence would restart numbering from 1 (if the sequence is created automatically) - and this would cause trouble.

I am including my version of a function to generate primary keys, in case there is some significant difference I have overlooked.

Note: You can also use a SINGLE sequence for all tables. Since the user never sees the primary key values, it doesn't really matter, as long as you don't go over the 2e9 limit for integers.

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