SQL存储过程问题

时间:2011-06-15 06:42:37

标签: sql sql-server stored-procedures

我遇到以下存储过程的问题

CREATE PROCEDURE LockRoots
    -- Add the parameters for the stored procedure here
    @lock uniqueidentifier,
    @count int
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    UPDATE R
    SET R.[Lock] = @lock
    FROM 
    (
        SELECT TOP @count *
        FROM [Root] as R
        --LEFT JOIN [Container] as C ON R.ID = C.RootID
        WHERE [Lock] IS NULL
        --ORDER BY NEWID()
    );
END
GO

“SELECT TOP @count *”出现问题,为什么我不能“选择top @VariableAmount”的记录?

1 个答案:

答案 0 :(得分:6)

Need parenthesis...

...
SELECT TOP (@count) *
...

注意:参数化TOP

的SQL Server 2005+