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 2000 Forums
 SQL Server Development (2000)
 DYNAMIC TOP

Author  Topic 

vladimir_grigoro
Yak Posting Veteran

62 Posts

Posted - 2005-01-05 : 08:51:22
Hi All,

I have the following problem, whish I had to solve. I want to select dynamically "top" from particular result e.g.:

SELECT TOP 10 * FROM TABLE_A but I want to make the number "10" as a variable....something like:

SELECT TOP @X * FROM TABLE_A

Is there anybody who knows how to receive such result?

Thanks in advance!

The Rebel

nr
SQLTeam MVY

12543 Posts

Posted - 2005-01-05 : 08:56:22
set rowcount @X
SELECT * FROM TABLE_A
set rowcount 0

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

vladimir_grigoro
Yak Posting Veteran

62 Posts

Posted - 2005-01-05 : 09:02:33
Thanks a lot!!! You were so fast:-)
Tnx one more time...


quote:
Originally posted by nr

set rowcount @X
SELECT * FROM TABLE_A
set rowcount 0

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.



The Rebel
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-01-06 : 01:00:04
other solution is

declare @X numeric
set @x=1
print @x
exec('SELECT top '+@X+' * FROM table')

Madhivanan

Go to Top of Page
   

- Advertisement -