SQL stored procedure issues

I am having a problem with the following stored procedure

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

The problem arises with "SELECT TOP @count *", why can't I select the "top @VariableAmount" entries?

+3
source share
1 answer

The necessary bracket ...

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

Note: also SQL Server 2005+ for parameterized TOP

+6
source

All Articles