sql - Get the last inserted ID -


I have to get the ID of the last line inserted in the table and use it with a variable in SQL Server.

Here is my code:

  SELECT IDENT_CURRENT ('profiles')  

I tried to add a section

  SELECT IDENT_CURRENT ('profiles') WHERE profile. User = '2'  

My error is:

multi-part identifier "profile user." Can not be constrained.

your second query

another code to WHERE To do the clause work ( to the extent that does not make a mistake ), you need multi-part identifier anchor WHERE profiles.userid = '2 from the profile Profile with SELECT IDENT_CURRENT ('Profile'). '

User / pre>

but it does not make much sense because WHERE will not block the result of changing the bottom-line result (ie, The final identification created for the code> profile ) - possibly more than once in the result set.

Back to your first query

If you really want to get the final identity value to delete the line created for profile , Your first query ...

  SELECT IDENT_CURRENT ('profiles')  

... is the direction to go.

Of course, you get the final identity value generated for profile for example (assuming that profile contains id is identity column] Code> Delete .

  DECLARE @lastID int; SELECT @lastID = IDENT_CURRENT ('profile'); Delete from profile WHERE id = @lastID;  

..., which would really be better as such:

Delete from profile WHERE id = IDENT_CURRENT ('Profile' );

Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -