Hello Team
i want to get a value from a column in the last record
i wrote select ID from (select top 1 * desc from items)
any idea, Thanks lot
If you want to use top you have to use order by clause with desc to fetch the last record..
Code Snippet
Select Top 1 Id from Demo Order By Id Desc
or
Select max(id) from Demo;
Another easy way is,
Code Snippet
Declare @.Id as int;
Select @.ID = ID from Demo;
Select @.ID;
here it always return the last record
|||Just select from the table ordering by the field that can be used to identify if this is a recent row or not.
Code Snippet
Select Top 1 ID
From Items
Order By ID Desc
Assuming ID is a column that is incremental for each new row. If you don't have an incremental ID, you can use a column that contains the date when the row has been added.
I hope this answers your question. If not, please post more information about the table.
Best regards,
Sami Samir
|||Thank you very much
and Mr samy too
|||If you want to obtain the ID value of the last record inserted to a table, and then use that ID value in additional INSERTS/UPDATES, you may wish to explore using the system function: SCOPE_IDENTITY. It can return the value of the immediately preceding insert WITHOUT having to execute a query.sql
No comments:
Post a Comment