need ID from newly inserted record (altered by insert trigger on SQL server) 
Author Message
 need ID from newly inserted record (altered by insert trigger on SQL server)

I'm using an insert trigger (SQL Server) to reproduce the counter
functionality in Access.  When a new record is inserted without an ID (ID =
Null or 0), then it is assigned an ID (by the trigger) that is equal to the
highest ID in the table so far plus one.  So far so good.

Well, how do I find out what that ID is?  In a multiuser environment, it is
not possible to simply select the highest ID, because when two users who
insert a new record at the same time, it is impossible to distinguish the two
newly inserted records.  Both users will believe that the highest record is
theirs, when in fact only one user actually owns the last record, and the
other users owns the next-to-last record.

It seems like I need something like a cursor, but I'm not sure how to
implement such a thing.  Keep in mind that this is a problem in almost ALL of
my talbes, since most use a trigger of this sort, so I need a generic
solution, not a one-time fix for just this table.

Thanks in advance...

Joel



Tue, 10 Feb 1998 03:00:00 GMT  
 need ID from newly inserted record (altered by insert trigger on SQL server)

A common way to do this is to have a separate table to hold the 'next
id'. Your 'insert ' trigger updates the ID value to current value +
increment value (depending on your SQL server it can be efficient in
I/O terms to use an increment of greater than 1).

You'll find good discussions on these types of issue in
comp.lang.databases.theory plus the sybase/sql server newsgroups.

There are several good books on physical database design around but
I'd reccomend you pick one that is specific to the version of SQL
Server you are using.

Quote:
>I'm using an insert trigger (SQL Server) to reproduce the counter
>functionality in Access.  When a new record is inserted without an ID (ID =
>Null or 0), then it is assigned an ID (by the trigger) that is equal to the
>highest ID in the table so far plus one.  So far so good.
>Well, how do I find out what that ID is?  In a multiuser environment, it is
>not possible to simply select the highest ID, because when two users who
>insert a new record at the same time, it is impossible to distinguish the two
>newly inserted records.  Both users will believe that the highest record is
>theirs, when in fact only one user actually owns the last record, and the
>other users owns the next-to-last record.
>It seems like I need something like a cursor, but I'm not sure how to
>implement such a thing.  Keep in mind that this is a problem in almost ALL of
>my talbes, since most use a trigger of this sort, so I need a generic
>solution, not a one-time fix for just this table.
>Thanks in advance...
>Joel



Sat, 14 Feb 1998 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. sql triggers vb on record insert

2. Geting the new ID for a record after a SQL insert operation

3. Insert Data Into Access Database From SQL Server ADO Insert

4. Extracting information from a newly inserted record

5. How to retrieve info from newly inserted record

6. Obtaining the value of the Identity column of a newly inserted record

7. How do I get the AutoNumber (or Identity) for a newly inserted record (from able-consulting)

8. Returning the primary key for a newly inserted record

9. Get the ID vaule from SQL 7 for the newly added record

10. Insert Records from SQL Server Passthrough Query

11. Problems inserting records into SQL Server

12. Problem With Inserting Over 1200o records into SQL server 6.5

 

 
Powered by phpBB® Forum Software