Using SELECT TOP with a variable

From: Tim (anonymous_at_discussions.microsoft.com)
Date: 03/08/04


Date: Mon, 8 Mar 2004 06:31:09 -0800

Using old T-SQL I can force a maximum number of rows that will be returned using SET ROWCOUNT n like this

DECLARE @num int
SET @num = 10
SET ROWCOUNT @num
SELECT * FROM authors
SET ROWCOUNT 0

Is there a way to use a varibale with TOP? The example below doesn't work but it shows what I'd like to do. Thank you in advance.

declare @num int
set @num = 10
select top @rw * from authors order by logon_id

Regards,
Tim



Relevant Pages

  • Re: TOP as parameter in a proc
    ... SET ROWCOUNT @num ... Merry Christmas and Happy New Year. ... "Carlo" wrote in message ...
    (microsoft.public.sqlserver.programming)
  • Re: Using SELECT TOP with a variable
    ... DECKARE @num INT ... "Tim" wrote in message ... using SET ROWCOUNT n like this ... > DECLARE @num int ...
    (microsoft.public.sqlserver.mseq)
  • What is wrong with this script
    ... SET ROWCOUNT 0 ... DECLARE @String nvarchar ... SELECT name AS constraintname, ... --Setting the rowcount to one allows for one row from the temp table to be picked off at a time. ...
    (microsoft.public.sqlserver.programming)
  • Re: SELECT TOP with variable not working
    ... SET ROWCOUNT @Num_records ... SQL Server MVP ... "Paul" wrote in message ... > Declare @Num_records as INTEGER ...
    (microsoft.public.sqlserver.server)
  • Re: HTREEITEM
    ... I was trying to illustrate what I wanted to do. ... list will be makes it quit hard to declare these items before it is running. ... Try to avoid commas in declaration lists. ... CString name, num; ...
    (microsoft.public.vc.mfc)