Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Newbie question on SCOPE_IDENTITY
Message
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Autre
Versions des environnements
SQL Server:
SQL Server 2005
Divers
Thread ID:
01196646
Message ID:
01196685
Vues:
10
This message has been marked as the solution to the initial question of the thread.
>>
>>From BOL:
>>
>>Returns the last identity value inserted into an identity column in the same scope. A scope is a module: a stored procedure, trigger, function, or batch. Therefore, two statements are in the same scope if they are in the same stored procedure, function, or batch.
>>
>>

>>
>>Every SQLEXEC() is different batch, so you can't use SCOPE_IDENTITY() in separate SQLEXEC(). If you want to use it you must embed it in the same SQLEXEC() with INSERT:
>>
>>TEXT TO lcInsert NOSHOW TEXTMERGE
>>     INSERT INTO ............
>>     SELECT SCOPE_IDENTITY()
>>ENDTEXT
>>SQLEXEC(..., lcInsert, [crsIDColumn])
>>SELECT crsIDColumn
>>BROWSE NORMAL
>>
>>But keep in mind that if you have more than one record inserted that query will return you the LAST IDENTITY value inserted.
>
>Borislav,
>
>Thank you very much for your help. But when I tried to use your code (almost without any changes on my part), the cursor crsIdColumn is not created. That is, I get error on line SELECT crsIdColumn.
>
>What do you think could be the reason for it?

Don't know, but that works for me:
lnSql = SQLSTRINGCONNECT([Driver=SQL Server;Server=Boris1;Trusted_Connection=yes;])
IF lnSql > 0 
   IF SQLEXEC(lnSql, [CREATE TABLE #Test (Id int IDENTITY(1,1), Fld2 varchar(200))]) < 0
      AERROR(aaa)
      MESSAGEBOX(aaa[2])
   ENDIF
   TEXT TO lcTest NOSHOW
        INSERT INTO #Test (Fld2) VALUES ('aaaaaaaaaaaaaaaaa')
        SELECT SCOPE_IDENTITY()
   ENDTEXT
   IF SQLEXEC(lnSql, lcTest, [crsTest]) < 0
      AERROR(aaa)
      MESSAGEBOX(aaa[2])
   ENDIF

   SELECT crsTest
   BROWSE NORMAL
   SQLDISCONNECT(0)
ENDIF
Against Stupidity the Gods themselves Contend in Vain - Johann Christoph Friedrich von Schiller
The only thing normal about database guys is their tables.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform