【问题标题】:Cannot user "Select TOP @Count ..."无法用户“选择 TOP @Count ...”
【发布时间】:2010-09-16 02:57:09
【问题描述】:

我正在创建一个类似下面的过程。当没有“TOP @Count”时它工作正常,或者当我放置一个混凝土值“TOP 100”时它工作正常。

那么为什么我不能在那里传递值???我怎么能绕着它走???

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE MyProcedure    
    @Count int = 100
AS
BEGIN

  SELECT TOP @Count 
         t1.id AS ID, 
         t1.name AS Name, 
         t2.type AS TYPE    
    FROM sampleTable1 as t1 with (noloack), 
         sampleTable2 as t2 with (noloack)          
   WHERE (t1.t2Id = t2.Id)     
ORDER BY t1.name asc

END
GO

【问题讨论】:

    标签: sql sql-server tsql stored-procedures


    【解决方案1】:

    假设2005+,你需要使用括号:

      SELECT TOP (@Count) 
             t1.id AS ID, 
             t1.name AS Name, 
             t2.type AS TYPE
        FROM sampleTable1 as t1 with (noloack)
        JOIN sampleTable2 as t2 with (noloack) ON t2.id = t1.t2.id
    ORDER BY t1.name
    

    我的理解是bracket support was added in v2005是为了不需要dynamic SQL

    【讨论】:

    • 哇。我从来不知道这一点。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 2014-08-25
    • 1970-01-01
    • 2010-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多