Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (4k points)

@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"."

4 Answers

0 votes
by (8.7k points)

It’s not possible to concatenate the integer with a string :

 

So you’ll require to execute the following query:

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

To become a certified SQL professional, Check out the SQL certification by Intellipaat. 

0 votes
by (7.8k points)
The error message "Must declare the scalar variable '@RowFrom'." indicates that the variable '@RowFrom' is not declared or defined within the scope where it is being used.

Since you mentioned that '@RowFrom' and '@RowTo' are global input parameters for the stored procedure, it is possible that they are not being passed correctly or declared before using them in the SQL query.

When using dynamic SQL with T-SQL, variables need to be declared within the dynamic SQL statement using the 'DECLARE' keyword or passed as parameters.

To resolve this issue, make sure that '@RowFrom' and '@RowTo' are declared either within the dynamic SQL statement or before executing the dynamic SQL.

One approach is to concatenate the values of '@RowFrom' and '@RowTo' directly into the dynamic SQL statement. For example, you can modify the '@sqlstatement' variable to include the values like this: '@sqlstatement = N'SELECT * FROM TableName WHERE Row >= ' + CAST(@RowFrom AS VARCHAR(10)) + ' AND Row <= ' + CAST(@RowTo AS VARCHAR(10))'.

Ensure that you properly validate and sanitize the input values before concatenating them into the dynamic SQL statement to prevent any potential SQL injection vulnerabilities.
0 votes
by (11.4k points)
The error message suggests that the variable '@RowFrom' is not declared in the scope where it is being used. To resolve this, ensure that '@RowFrom' and '@RowTo' are declared either within the dynamic SQL statement or before executing it. You can concatenate the values directly into the dynamic SQL statement, such as '@sqlstatement = N'SELECT * FROM TableName WHERE Row >= ' + CAST(@RowFrom AS VARCHAR(10)) + ' AND Row <= ' + CAST(@RowTo AS VARCHAR(10))'. Remember to validate and sanitize the input values to prevent SQL injection vulnerabilities.
0 votes
by (13k points)

Declare the '@RowFrom' and '@RowTo' variables before using them in the SQL query.

Browse Categories

...