SQL Server 2008选择由变量定义的前100个

时间:2012-02-02 16:36:43

标签: sql sql-server-2008

有没有办法选择顶级@top通过变量定义值,如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()

1 个答案:

答案 0 :(得分:10)

将变量放在括号中:

SELECT top (@top)

This functionality was added in SQL Server 2005 ... 2000是您必须执行SET ROWCOUNT @top

的最后一个版本