Md Ghousemohi
Md Ghousemohi

Reputation: 197

How to write stored procedure for getting top record

How can I write a SQL stored procedure for getting top n records from stored procedure?

ALTER PROCEDURE GetRe
     (@a INT, @b INT)
AS
BEGIN
    DECLARE @Sum INT
    SET @Sum = @a + @B

    SELECT TOP @Sum * 
    FROM Customer
END

I'm getting an error:

Incorrect syntax near '@Sum'

Upvotes: 1

Views: 47

Answers (1)

Siyual
Siyual

Reputation: 16917

Based on the error, it looks like you’re using SQL Server.

You need to wrap the @Sum in parenthesis:

SELECT TOP (@Sum) * 
FROM Customer

Upvotes: 4

Related Questions