Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm writing a MS SQL procedure that receives data, inserts it into a table, and then is supposed to retrieve the new index where the data was inserted. The only UNIQUE field is the ID, and I wouldn't like to count simply of the IDENTITY, because IDENTITY_INSERT Could mess things up... Is there anything safe that I could do?

That's what I was writing, is it any good?
SQL
CREATE PROCEDURE dbo.Func(@var text)
        AS
	BEGIN
	INSERT INTO Table (Col) VALUES(@var)
	SELECT TOP 1 TableID FROM Table ORDER BY TableID DESC
	RETURN TableID
	END
Posted
Updated 16-Oct-11 10:04am
v2

1 solution

Have a look at the OUTPUT Clause[^] - the insert will execute as an atomic unit of work, assuming you actually use an identity column, you will be able to do what you are attempting to do in a safe manner.

Best regards
Espen Harlinn
 
Share this answer
 
Comments
ShacharK 17-Oct-11 9:44am    
Found it by myself eventually, thanks a lot anyway :)
Espen Harlinn 17-Oct-11 9:47am    
Excellent :)
[no name] 17-Oct-11 11:26am    
great, that us what we need, please share your solution as well
Simon Bang Terkildsen 18-Oct-11 16:25pm    
My 5
Espen Harlinn 18-Oct-11 17:02pm    
Thank you, Simon!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900