-
May 14th, 2019, 07:29 PM
#1
Thread Starter
Hyperactive Member
help finding last Item saved n MS SQL DB
I need to find the autofill # named WorkOrderID in table Workorders
Code:
SQL.ExecQuery(" SELECT * " &
"FROM WorkOrders " &
" ORDER BY WorkOrderID " &
" TOP 1;")
I tried the command limit but its not working with MS SQL,
I only want the last record entered
Thanks
-
May 14th, 2019, 09:28 PM
#2
Re: help finding last Item saved n MS SQL DB
I just searched the web for "sql server select top" and the third result was the official documentation for the TOP statement.
https://docs.microsoft.com/en-us/sql...ql-server-2017
That took me a couple of seconds and then not much more time to read the examples provided there. None of them look like your code.
-
May 15th, 2019, 12:55 AM
#3
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?
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE 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.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
May 15th, 2019, 06:40 AM
#4
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
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
May 15th, 2019, 07:51 AM
#5
Re: help finding last Item saved n MS SQL DB
Originally Posted by Alfarata
I need to find the autofill # named WorkOrderID in table Workorders
Code:
SQL.ExecQuery(" SELECT * " &
"FROM WorkOrders " &
" ORDER BY WorkOrderID " &
" TOP 1;")
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.
-tg
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|