Getting IDENTITY value BEFORE an insert(?!)

Getting IDENTITY value BEFORE an insert(?!)

Post by Tracy Norr » Thu, 14 Dec 1995 04:00:00



I am using Borland's Delphi (Client/Server) and Sql Server 6.0. Does anyone
have a way to get the "soon-to-be" value of an identity column before an
insert? How about a stored procedure to gen unique IDs instead of using the
IDENTITY column type?

WARNING: Delphi & SQL Server 6.0 will fight like cats and dogs if you
attempt to use an IDENTITY (unique index) column as a Primary key. The
Borland Database Engine (BDE) get's totally lost if Sql Server gens a value
for the primary key. The BDE is expecting the key value to be written to the
database along with the record. Watch out for any stored procedures, etc
that modify a just-inserted record's key values. I have not had this problem
with Gupta's SQLWindows 5.01 ;^

Tracy

 
 
 

Getting IDENTITY value BEFORE an insert(?!)

Post by Michael Moor » Fri, 15 Dec 1995 04:00:00



> I am using Borland's Delphi (Client/Server) and Sql Server 6.0. Does anyone
> have a way to get the "soon-to-be" value of an identity column before an
> insert? How about a stored procedure to gen unique IDs instead of using the
> IDENTITY column type?

> WARNING: Delphi & SQL Server 6.0 will fight like cats and dogs if you
> attempt to use an IDENTITY (unique index) column as a Primary key. The
> Borland Database Engine (BDE) get's totally lost if Sql Server gens a value
> for the primary key. The BDE is expecting the key value to be written to the
> database along with the record. Watch out for any stored procedures, etc
> that modify a just-inserted record's key values. I have not had this problem
> with Gupta's SQLWindows 5.01 ;^

> Tracy

Sound's like this would violate one of the concurrency rules.  
What if two processes tried to do this simultainiously etc.?

I always used to simulate this using a query such as:

  insert MyTable
    select isnull(max(KeyID),0)+1, "Fred", "Is", "Dead"
    from MyTable

I THINK this is un-interruptable, and hence safe, but I could be
wrong.  I don't know how to write it so that the unique ID is
returned without creating a transaction.

Regards,
        Michael D. Moore

 
 
 

Getting IDENTITY value BEFORE an insert(?!)

Post by Jose Luis Malaquia » Wed, 20 Dec 1995 04:00:00


I ran into a similar problem, even though I only needed the IDENTITY
value AFTER the insert. I needed that value to insert as a reference
in another table. I solved it by running to the last record after the
insert, and reading the value. But I'm not sure whether that won't
get me in trouble soon (suppose for some reason sql server doesn't
insert at the end of the table or someone else inserts one after me)

Does anyone know of a solution?
Jose Luis Malaquias

 
 
 

Getting IDENTITY value BEFORE an insert(?!)

Post by Brian Ellio » Wed, 20 Dec 1995 04:00:00



   --- Brian



Quote:>I am using Borland's Delphi (Client/Server) and Sql Server 6.0. Does anyone
>have a way to get the "soon-to-be" value of an identity column before an
>insert? How about a stored procedure to gen unique IDs instead of using the
>IDENTITY column type?

 
 
 

Getting IDENTITY value BEFORE an insert(?!)

Post by Eric J. Holtm » Wed, 20 Dec 1995 04:00:00




>>I ran into a similar problem, even though I only needed the IDENTITY
>>value AFTER the insert. I needed that value to insert as a reference
>>in another table. I solved it by running to the last record after the
>>insert, and reading the value. But I'm not sure whether that won't
>>get me in trouble soon (suppose for some reason sql server doesn't
>>insert at the end of the table or someone else inserts one after me)

>>Does anyone know of a solution?
>>Jose Luis Malaquias



Although I'd spell it correctly in the real stored proc.  :)

--
------
Eric J. Holtman               | Managing programmers is like herding cats.
#include <stdjunk.h>          |      
http://www.jaeger.com/~erich/ | There's no government like no government

 
 
 

Getting IDENTITY value BEFORE an insert(?!)

Post by Eric J. Holtm » Wed, 20 Dec 1995 04:00:00



Quote:

>I ran into a similar problem, even though I only needed the IDENTITY
>value AFTER the insert. I needed that value to insert as a reference
>in another table. I solved it by running to the last record after the
>insert, and reading the value. But I'm not sure whether that won't
>get me in trouble soon (suppose for some reason sql server doesn't
>insert at the end of the table or someone else inserts one after me)

>Does anyone know of a solution?
>Jose Luis Malaquias


--
------
Eric J. Holtman               | Managing programmers is like herding cats.
#include <stdjunk.h>          |      
http://www.jaeger.com/~erich/ | There's no government like no government

 
 
 

1. Getting IDENTITY value of newly inserted row

Hi,

I have a quick question.  How can I get the identity value of a newly
inserted row using the Recordset object.  Here is what I am trying right
now:

        Set toRS = New Recordset
        toRS.Open "PMSUsers", oDb.CN, adOpenForwardOnly, adLockOptimistic
        toRS.AddNew
        toRS("UsrLoginID") = strEncUsername
        toRS("UsrPassword") = strEncPassword
        toRS("CustomerID") = CustomerID
        toRS.Update
        AddUser = toRS("UsrID") 'Return New UserID to calling function

The UsrID column is defined as an IDENTITY column in SQL.  However, it's
always returning 0 for some reason, and according to SQL Server, new ids are
being correctly generated.  Any help would be appreciated.  Thanks.

Xin Li

2. VNI-2015 : Authentication error

3. Getting the value of an autoincrement or identity field during an sql insert

4. Inserting Into Primary + Join Tables

5. getting identity value after insert

6. Transfer FMP data into Webmail CGI/Perl inteface

7. Getting the value of an IDENTITY column after an INSERT query

8. Help! Thank you!

9. Getting auto-incremented identity value on insert...

10. Getting Back Identity column value on Insert using RDS

11. Getting IDENTITY value of a row that was just INSERTED

12. Getting the new identity value from an Insert query

13. Getting the value of an autoincrement or identity field during an sql insert