select @@firstIDENTITY (1 Viewer)

24sharon

Registered User.
Local time
Yesterday, 19:15
Joined
Oct 5, 2004
Messages
147
is it possible?


I have a query

insert into myTbl (col1, col2)
select myFirstCol, mySecondCol from myTable2 where....


I want to find the first identity that insert.

if I write select @@identity, I got the last and I want the first.

is it possible
 

SQL_Hell

SQL Server DBA
Local time
Today, 03:15
Joined
Dec 4, 2003
Messages
1,360
many ways to do this...

to get the first record entered you would need to create a loop so that each record is inserted one by one. But use scope_identity() rather than @@idenitity. Because scope_identity gives you the identity for your query, @@identity could give you any scope so if someone else inserting records at the same time, you will start getting the identity of their inserts and not yours.




or alternatively you could put a datetime value in the table you are inserting into and set the default value to getdate(), then you could query like this.

select min(id)
from table2
where date = '01/09/2006'

hope this helps, any questions let me know :)
 
Last edited:

Users who are viewing this thread

Top Bottom