Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Must declare the scalar variable

@RowFrom int

@RowTo int

are both Global Input Params for the Stored Procedure, and since I am compiling the SQL query inside the Stored Procedure with T-SQL then using Exec(@sqlstatement) at the end of the stored procedure to show the result, it gives me this error when I try to use the @RowFrom or @RowTo inside the @sqlstatement variable that is executed.. it works fine otherwise.. please help.

"Must declare the scalar variable "@RowFrom"." 

Also, I tried including the following in the @sqlstatement variable:

'Declare @Rt int' 'SET @Rt = ' + @RowTo 

but @RowTo still doesn't pass its value to @Rt and generates an error.

like image 928
bill Avatar asked Aug 24 '11 20:08

bill


People also ask

What does must DECLARE the scalar variable?

What Must Declare the Scalar Variable Error Means? Must Declare the Scalar Variable means that DECLARE statement wasn't used to declare the function in a query. From the error stated above, there is a local variable within the function, script, or stored procedure that must be used with the Declare statement.

How do you define a scalar variable?

A scalar variable, or scalar field, is a variable that holds one value at a time. It is a single component that assumes a range of number or string values. A scalar value is associated with every point in a space.

What is scalar variable SQL example?

A scalar variable stores a value with no internal components. The value can change. A scalar variable declaration specifies the name and data type of the variable and allocates storage for it. The declaration can also assign an initial value and impose the NOT NULL constraint.

Why is the scalar variable?

Scalar variables are used to represent individual fixed-size data objects, such as integers and pointers. Scalar variables can also be used for fixed-size objects that are composed of one or more primitive or composite types. D provides the ability to create both arrays of objects as well as composite structures.


1 Answers

You can't concatenate an int to a string. Instead of:

SET @sql = N'DECLARE @Rt int; SET @Rt = ' + @RowTo; 

You need:

SET @sql = N'DECLARE @Rt int; SET @Rt = ' + CONVERT(VARCHAR(12), @RowTo); 

To help illustrate what's happening here. Let's say @RowTo = 5.

DECLARE @RowTo int; SET @RowTo = 5;  DECLARE @sql nvarchar(max); SET @sql = N'SELECT ' + CONVERT(varchar(12), @RowTo) + ' * 5'; EXEC sys.sp_executesql @sql; 

In order to build that into a string (even if ultimately it will be a number), I need to convert it. But as you can see, the number is still treated as a number when it's executed. The answer is 25, right?

In your case you can use proper parameterization rather than use concatenation which, if you get into that habit, you will expose yourself to SQL injection at some point (see this and this:

SET @sql = @sql + ' WHERE RowNum BETWEEN @RowFrom AND @RowTo;';  EXEC sys.sp_executesql @sql,   N'@RowFrom int, @RowTo int',   @RowFrom, @RowTo; 
like image 142
Aaron Bertrand Avatar answered Sep 28 '22 10:09

Aaron Bertrand