Wrox Programmer Forums
|
SQL Server 2005 General discussion of SQL Server *2005* version only.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 21st, 2007, 02:47 AM
Authorized User
 
Join Date: Jan 2005
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to hossrad
Default Inserted Identities

Hi i have a Query Like this:

INSERT INTO TABLE1
SELECT * FROM TABLE2

TABLE1 has a identity column,
now i want to know what identities have been inserted into TABLE1 after the Query executes.



Be Sure,
Hossein
__________________
Be Sure,
-hossrad
 
Old May 21st, 2007, 09:48 AM
Authorized User
 
Join Date: Oct 2005
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Default

One way is to initially store the last identity value in your first table into a local variable and then do the insert. Anything higher than that number are the values inserted to it:

DECLARE @LastIdentity INT
SELECT @LastIdentity = MAX(ID) FROM Table1

INSERT INTO TABLE1
SELECT * FROM TABLE2

SELECT * FROM TABLE1 WHERE ID > @LastIdentity

SQL Server Helper
How well do you know SQL? Find out with the free test assessment from SQL Server Helper!!!
http://www.sql-server-helper.com/free-test/default.aspx

Got a SQL Server Question? Ask us here: http://www.sql-server-helper.com/forums/default.asp





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Edit record right after it was Inserted? aladov ASP.NET 2.0 Professional 4 December 11th, 2007 03:18 PM
Record is Inserted Twice donevco Access 1 February 27th, 2007 12:28 PM
How can I get the last inserted rows? fraijo SQL Server 2000 4 November 23rd, 2006 07:50 AM
Data to be inserted into the database countmedream BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 0 November 2nd, 2004 03:19 AM
INSERT INTO..no error but nothing is inserted!! apek PHP How-To 9 January 16th, 2004 02:43 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.