| | |
LastRecord in DB
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
If the column is an identity the value can be retrieved by calling
SCOPE_IDENTITY() sql Syntax (Toggle Plain Text)
DECLARE @ID INT INSERT INTO aTable (aColumn) VALUES (aValue) SET @ID = Cast(SCOPE_IDENTITY() as INT)
•
•
Join Date: Jul 2007
Posts: 276
Reputation:
Solved Threads: 37
To get the last record in a SQL database you simply type
This will return the last record in the column you want from the table you are looking for. How to translate that into C# without using SQL I do not know.
C# Syntax (Toggle Plain Text)
SELECT TOP 1 * FROM tableName ORDER BY columnWithYourID DESC or if you just need the id then SELECT TOP 1 (columnWithYourID ) FROM tableName ORDER BY columnWithYourID DESC
This will return the last record in the column you want from the table you are looking for. How to translate that into C# without using SQL I do not know.
Last edited by rapture; Aug 3rd, 2009 at 9:41 am.
•
•
•
•
SELECT TOP 1 (columnWithYourID ) FROM tableName ORDER BY columnWithYourID DESC
@@IDENTITY or SCOPE_IDENTITY() depending on what ID you need. Consult SQL Books Online for the difference in the two values. Based on the original post SCOPE_IDENTITY() would best suit your needs. •
•
Join Date: Jul 2007
Posts: 276
Reputation:
Solved Threads: 37
If you want to get technical, you're advice is bad as well, as scope_identity will:
"SCOPE_IDENTITY(), like @@IDENTITY, will return the last identity value created in the current session, but it will also limit it to your current scope as well. In other words, it will return the last identity value that you explicitly created, rather than any identity that was created by a trigger or a user defined function."
Thus it may not return the proper id that the user entered right?
my solution was just a quick fix for a small database and someone new, it will work in most instances but the proper method would technically be
SELECT IDENT_CURRENT(’tablename’)
as this will return the last identity in the table regardless of the other variables mentioned. I suppose I should have answered this way the first time to prevent disagreement and confusion of the original poster or those who search the subject. Thanks for the advice.
Also of note, there is a possible bug when run in parallel with scope_identity as well that get more complicated
http://blog.sqlauthority.com/2009/03...-and-solution/
Darab,
You can use either method mentioned. If you're just working on a small project then scope_identity may work fine. Otherwise you can use SELECT IDENT_CURRENT(’tablename’) as stated. This should answer your question. Go ahead and ask anything else you need on this subject or mark the thread as solved.
"SCOPE_IDENTITY(), like @@IDENTITY, will return the last identity value created in the current session, but it will also limit it to your current scope as well. In other words, it will return the last identity value that you explicitly created, rather than any identity that was created by a trigger or a user defined function."
Thus it may not return the proper id that the user entered right?
my solution was just a quick fix for a small database and someone new, it will work in most instances but the proper method would technically be
SELECT IDENT_CURRENT(’tablename’)
as this will return the last identity in the table regardless of the other variables mentioned. I suppose I should have answered this way the first time to prevent disagreement and confusion of the original poster or those who search the subject. Thanks for the advice.
Also of note, there is a possible bug when run in parallel with scope_identity as well that get more complicated
http://blog.sqlauthority.com/2009/03...-and-solution/
Darab,
You can use either method mentioned. If you're just working on a small project then scope_identity may work fine. Otherwise you can use SELECT IDENT_CURRENT(’tablename’) as stated. This should answer your question. Go ahead and ask anything else you need on this subject or mark the thread as solved.
•
•
•
•
If you want to get technical, you're advice is bad as well, as scope_identity will:
"SCOPE_IDENTITY(), like @@IDENTITY, will return the last identity value created in the current session, but it will also limit it to your current scope as well. In other words, it will return the last identity value that you explicitly created, rather than any identity that was created by a trigger or a user defined function."
>> Thus it may not return the proper id that the user entered right?
Yes, it will. They did not enter an ID in this case as I didn't not see the OP indicating about enabling IDENTITY INSERT on the table.
>>my solution was just a quick fix for a small database and someone new, it will work in most instances but the proper method would technically be: SELECT IDENT_CURRENT(’tablename’)
That is the exact same problem as
Select Max(id) from Table and is still a race condition. This is far more likely to be an issue than the parallelism condition of SCOPE_IDENTITY() >> Also of note, there is a possible bug when run in parallel with scope_identity as well that get more complicated
I was not aware of this bug. However this bug occurs sometimes where parallel execution plans are generated by the SQL Server which does not happen with
Insert Into Table () Values () , and if it does I have failed to see anything from Microsoft that indicates this. That being said I doubt that bug affects this situation.I do appreciate you posting that bug though. I myself have queries which are affected by that condition so I'll be sending out a company wide email
•
•
•
•
Intersting, thank you - and what was your take on using
SELECT IDENT_CURRENT(’tablename’)
SCOPE_IDENTITY() or @@IDENTITY . Consult SQL books online for the differences.Read the Microsoft Connect report that was posted here to be aware of a potential bug when using the identity features.
•
•
Join Date: Jul 2007
Posts: 276
Reputation:
Solved Threads: 37
Can you find an example? on MSDN here http://msdn.microsoft.com/en-us/libr...7(SQL.80).aspx
it looks like it is similar to @@IDENTITY AND SCOPE_IDENTITY but just returning the last id value from any session or scope.
I see a problem if the table is null
http://support.microsoft.com/kb/835188
but if the table is created why wouldn't this be the best way to go?
--and thanks for the advice
it looks like it is similar to @@IDENTITY AND SCOPE_IDENTITY but just returning the last id value from any session or scope.
I see a problem if the table is null
http://support.microsoft.com/kb/835188
but if the table is created why wouldn't this be the best way to go?
--and thanks for the advice
Last edited by rapture; Aug 3rd, 2009 at 1:46 pm.
![]() |
Similar Threads
- Creating a "Data" folder in my C: Drive using my Java program (Java)
- Problems using a php generator (PHP)
- help with for loop (Pascal and Delphi)
Other Threads in the C# Forum
- Previous Thread: How do I round off
- Next Thread: DLL callback
| Thread Tools | Search this Thread |
.net access algorithm angle array asp.net barchart bitmap box broadcast c# capturing check checkbox client combobox control conversion csharp custom database datagrid datagridview dataset datetime dbconnection degrees delegate design development disappear draganddrop drawing encryption enum eventhandlers excel file firefox form format forms function gdi+ image index input install java label leak libraries list listbox loop mandelbrot math monodevelop mouseclick msword mysql operator path pause photoshop picturebox pixelinversion post programming radians regex remoting resourcefile richtextbox round server sleep socket sql statistics stream string table tcpclientchannel text textbox thread time timer update usercontrol validation virtualization visualbasic visualstudio webbrowser windows winforms wpf xml






