Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server 2008 select top 100 defined by a variable

Is there a way to select top @top defining the value by a variable like the following code in SQL Server 2008?

declare @top as integer;
set @top=100;

SELECT top @top
    T1.id as clientInfoId
    ,T1.ucsId
    ,T1.phoneHome
    ... (more columns here)......
    ,T1.businessTitle
FROM
    YELLOW_OUT_CLIENT_INFO AS T1    
LEFT JOIN 
    YELLOW_OUT_BUSINESS T2 on T2.clientInfoId = T1.id
WHERE
    T1.AgentId=5
    AND 
    T1.deleted IS NULL
    ... (more conditions here)......
ORDER BY NEWID()
like image 621
themhz Avatar asked Jun 04 '26 19:06

themhz


1 Answers

Put the variable in parentheses:

SELECT top (@top)

This functionality was added in SQL Server 2005... 2000 was the last version that you had to do the SET ROWCOUNT @top

like image 192
Michael Fredrickson Avatar answered Jun 07 '26 10:06

Michael Fredrickson