Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
SQL Server @@Identity
Message
 
 
À
16/10/2001 17:23:20
Information générale
Forum:
Visual FoxPro
Catégorie:
Client/serveur
Divers
Thread ID:
00569312
Message ID:
00569334
Vues:
23
>Perhaps this is not the best place to post a SQL Server question, but here goes anyway...
>
>In VFP, I commonly insert parent and child rows into tables by first generating a primary key value for the parent table and then using that as the foreign key value for the child rows. This way, of course, the child rows are properly related to the parent.

An article at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnfoxtk00/html/ft00l1.asp shows how you can do the same on Sql Server.

>
>How is this done in SQL Server? That is, how do I generate the "Next ID" for the parent so that I can use that value when inserting the child records? I suppose the T-SQL @@Identity could be used, but isn't that scoped to the connection and therefore not guaranteed to be the unique when I actually insert the parent record?

It is unique but it's not guaranteed to be sequential. Also it always holds the last generated Identity value for a connection. It means, that if you do insert in the table A with identity column and it has insert trigger that inserts record in the table B that also has Identity column than the value of Identity column of table B will be stored into @@Identity variable. The other difference is that you have to insert record into parent table to "generate" new id. After tha you can retrieve @@Identity value and use it for chaild records.

In SQL 2000 there are two new functions SCOPE_IDENTITY, and IDENT_CURRENT that return the last value inserted into the IDENTITY column of a table.

See Sql Server Books Online for details.
--sb--
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform