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
 General SQL Server Forums
 New to SQL Server Programming
 select @var using top in SQL 2000

Author  Topic 

mattt
Posting Yak Master

194 Posts

Posted - 2008-06-25 : 10:19:52
How come I can do this?

select top 1 auditID from [jobs]..AdAudit order by auditID desc

But not this?

declare @var int
select @var = top 1 auditID from [jobs]..AdAudit order by auditID desc

Is there a workaround?

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-06-25 : 10:23:23
declare @var int
set @var=1

Set rowcount @var
select auditID from [jobs]..AdAudit order by auditID desc
Set rowcount 0

Other method is using dynamic sql


Madhivanan

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

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-06-25 : 10:27:57
select top 1 @var = auditID from [jobs]..AdAudit order by auditID desc

which is the same as

SELECT @var = max(auditid)
from [jobs]..AdAudit


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

mattt
Posting Yak Master

194 Posts

Posted - 2008-06-25 : 10:33:20
Thanks!
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-06-25 : 10:45:34
Well. I misunderstood the question

Madhivanan

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

- Advertisement -