If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Re: help finding last Item saved n MS SQL DB

Never mind that he has the wrong Sorting Order.
The question is more: Does he use IDENTITY in his Table?

One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
For health reasons i try to avoid reading unformatted Code

Re: help finding last Item saved n MS SQL DB

First: Why are you asking for all the row data if you only need the ID of the last thing inserted into the table? You should only be asking for the thing you need to be returned.
Next: How is the record inserted? Is it by Stored Procedure? In-line code? If by Stored Procedure and the column is an IDENTITY then add a variable to hold the result of Scope_Identity() to be run right after the insert and return that from the proc

Code:

Declare @var1 INT
SELECT @var1 = SCOPE_IDENTITY()

You can add that to you inline code also and get a return if you did it that way.
You can also get the MAX(WorkOrderId) from the table if you want. This of course might not be your insert since someone else could have inserted a record there after you did if more that one person can use the system

Re: help finding last Item saved n MS SQL DB

I tried the command limit but its not working with MS SQL,
I only want the last record entered

Thanks

Well, for starters that'sbecaue the SQL is wrong... the TOP goes with the select:

Code:

SELECT TOP 1 * from WorkOrders ORDER BY WorkOrderID Desc

that would have woked... it also would have needlessly selected everything in the row when all you want it the id...

That said, there is no such thing as MSSQL ... MS is Microsft, which is a company, and SQL is an acronym for Structured Query Language. Microsoft produces multiple database systems. Access is one. SQL Server is another. FoxPro is yet another (albiet a dying one). Usually people mean SQL Server when they use MS SQL, but there really isn't a such thing.