SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


123»»»

select multiple identity values after insert? Expand / Collapse
Author
Message
Posted Thursday, January 08, 2009 1:18 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, March 10, 2010 6:47 PM
Points: 26, Visits: 82
for example, if i insert a batch of say five rows in table IdentTest (UserID int identity(1,1), UserName varchar(10)), i'd like to collect all the UserIDs just created. I know if can collect the last one by selecting the value of Scope_Identity(). But the rest i just can't seem to get to!

does anyone know a way to get these values cleanly? Anyone know of a trick to get them?

Thanks very much for anyone who'd like to help. Below is a script to help you and to illustrate further my problem:




/* Create the tables */
Create table UsersTest (UserName varchar(10))
create table IdentTest (UserID int identity(1,1), UserName varchar(10))

/* Data insert 1 */
insert into UsersTest (UserName) values ('User1')
insert into UsersTest (UserName) values ('User2')
insert into UsersTest (UserName) values ('User3')
insert into UsersTest (UserName) values ('User4')
insert into UsersTest (UserName) values ('User5')

/* data insert 2 */
insert into IdentTest (UserName)
select UserName from UsersTest

/* here is where i want all the identities but can only get one */
select Scope_Identity()


/*
drop table UsersTest
drop table IdentTest
*/

Post #632792
Posted Thursday, January 08, 2009 1:46 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, February 24, 2010 3:44 PM
Points: 3, Visits: 29
If you are inserting the data as a series of discrete steps why don't you just read the scope_identity() after each insert?
Post #632808
Posted Thursday, January 08, 2009 1:50 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, March 10, 2010 6:47 PM
Points: 26, Visits: 82
the discrete steps were just to load the data for the example. The actual insert is a bulk insert in the form of

Insert into TableA(ID)
select ID from TableB

so, lots of IDs going in at once. Thanks.
Post #632816
Posted Thursday, January 08, 2009 1:57 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, February 24, 2010 3:44 PM
Points: 3, Visits: 29
I guess that practically speaking you would have to do something along the lines of:

1. Create a transaction and lock the destination table.
2. Check that the current identity value is no less than the maximum value of the identity column in the table. You can use dbcc checkident (among other things) for this. Read the value of the next identity that will be inserted. This will usually be whatever dbcc checkident returns plus 1. Call this @StartID.
3. Bulk insert your data and count rows. Call the rowcount @rc
4. Since the table is locked the new identity values should be between the value @StartID and @StartID + @rc - 1.
5. Commit the transaction.
Post #632824
Posted Thursday, January 08, 2009 2:29 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 11:21 AM
Points: 9,104, Visits: 8,528
A much better way is to use the OUTPUT clause,like this:
--====== Make our test table to insert to
Create Table InsertIDTst(
ID int identity primary key
, ColName nvarchar(255)
, object_id int);
GO

--====== Make a table variable to hold the new ID's
Declare @IDList Table(ID int);

--====== Insert a bunch of rows,
-- and save the new IDs at the same time
INSERT into InsertIDTst(ColName, object_id)
Output INSERTED.ID Into @IDList(ID)
Select name, object_id
From sys.columns

--====== Show that we have the new IDs
SELECT * from @IDList



-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Post #632864
Posted Thursday, January 08, 2009 4:12 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, March 10, 2010 6:47 PM
Points: 26, Visits: 82
Fantastic. Thanks for everyone who took the time to think about, and reply to, my question. Thanks especially to RBarryYoung who's solution elegantly solves the whole problem. i had no idea about the Output thingy. it will come in very handy.

-drew
Post #632960
Posted Thursday, January 08, 2009 4:22 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 11:21 AM
Points: 9,104, Visits: 8,528
Glad we could help.

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Post #632965
Posted Sunday, January 11, 2009 4:59 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, March 10, 2010 6:47 PM
Points: 26, Visits: 82
is there a way to mark a topic as finished, or mark that the question has been answered correctly/helpfully? seems like this would be quite a useful feature.

-d
Post #634427
Posted Sunday, January 11, 2009 5:28 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, February 24, 2010 3:44 PM
Points: 3, Visits: 29
The way that these things usually work is that the post drifts downwards in the list as people fail to reply. If someone replies usually the post goes to the top of the list. Of course that might not be the case here :P.
Post #634436
Posted Sunday, January 11, 2009 5:41 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 11:21 AM
Points: 9,104, Visits: 8,528
bagofbirds (1/11/2009)
is there a way to mark a topic as finished, or mark that the question has been answered correctly/helpfully? seems like this would be quite a useful feature.

-d

Once you tell us that you have what you want, we usually stop replying. Of course sometime's we end up talking about something else... :)

You can also unsubscribe from the topic.


-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Post #634439
« Prev Topic | Next Topic »

123»»»

Permissions Expand / Collapse