I’m wondering why I can’t use a CAST function as the parameter to SET ROWCOUNT in a Stored Procedure.
This code fails:
CREATE PROCEDURE GetTransactions
@Count VARCHAR(5)
AS
SET ROWCOUNT CAST(@Count AS INT);
...
While this code with an intermediary variable works:
CREATE PROCEDURE GetTransactions
@Count VARCHAR(5)
AS
Declare @Rows AS INT;
SET @Rows = CAST(@Count AS INT);
SET ROWCOUNT @Rows;
...
I know that changing the parameter type to INT would also fix this, but the middle-tier component that calls the Stored Procedure needs everything to be a String type.
As in many cases with SQL Server’s TSQL implementation, you cannot pass an expression to
SET ROWCOUNT. The valid arguments to SET ROWCOUNT are either a number (constant) or a simple variable:The same applies to calling a stored procedure, in that you can’t have expressions as parameters.