Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Select query to find previous record ID

Author  Topic 

cplusplus
Aged Yak Warrior

567 Posts

Posted - 2008-09-10 : 19:20:22
I am passing orderid to my select, How to get the previous order id from my table orders.

I am passing orderid = 155, now ould like to get the previous records orderid. it could be any number lower than 155....

select prev(orderid) from table_orders where orderid=@orderid

please help thank you very much for the information..

afrika
Master Smack Fu Yak Hacker

2706 Posts

Posted - 2008-09-10 : 19:30:50
what are u using to pass this record ? IF a SP

look into the scope_identity()

declare @previous int
set @previous = scope_identity()
Go to Top of Page

hey001us
Posting Yak Master

185 Posts

Posted - 2008-09-10 : 19:33:02
select top 1 orderid from table_orders where orderid=@orderid
order by orderid desc

hey
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-09-11 : 05:29:10
or

select max(orderid) from table_orders where orderid<@orderid
order by orderid desc


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -