Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Deleting
Message
 
À
26/11/1999 13:29:52
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Titre:
Divers
Thread ID:
00295796
Message ID:
00295828
Vues:
29
Tyler,

Autogenerating Primary keys from the same table is a bad idea. It will give you many headaches over time. Here's a simple approach to generating unique values for the PK for a table.

Creat a table named SysKeys.dbf with the following structure;
Field            Type         Width
TableName          C            40
LastID             I
Now use this code to create a new value;
PROCEDURE GetNewKey( pcTable )
LOCAL lcAlias, liNewID
* Same current work area
lcAlias = ALIAS()
IF NOT USED( "Syskeys" )
   * If syskeys is not open
   USE SysKeys ALIAS Syskeys AGAIN IN 0
ENDIF
* Select the syskeys table and set the index order
SELECT Syskeys
SET ORDER TO TableName

* Find the record for the table requested
SEEK pcTableName
IF NOT FOUND()
   * If there is no record for the table, make one
   APPEND BLANK
ENDIF
* Lock the record so no one else can get an id while you are in process
DO WHILE NOT RLOCK()
ENDDO
* Increment the lastKey field to the variable
liNewID = LastID + 1
* Update the record with the newly assigned ID
REPLACE TableName WITH pcTable, ;
        LastID    WITH liNewID
* Unlock the record to let others in
UNLOCK
* Restore the work area
IF NOT EMPTY( lcAlias )
   SELECT ( lcAlias )
ELSE
   SELECT 0
ENDIF
* Return the new ID
RETURN liNewID
You can call this function from the Default Value property of the Primary key field in the database as; GetNewKey( "TheTablesName" ).
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform