Must declare the scalar variable

前端 未结 8 1292
小蘑菇
小蘑菇 2020-11-29 01:41

@RowFrom int

@RowTo int

are both Global Input Params for the Stored Procedure, and since I am compiling the SQL query inside the St

8条回答
  •  长情又很酷
    2020-11-29 02:09

    This is most likely not an answer to the issue itself but this question pops up as first result when searching for Sql declare scalar variable hence i share a possible solution to this error.

    In my case this error was caused by the use of ; after a SQL statement. Just remove it and the error will be gone.

    I guess the cause is the same as @IronSean already posted in an comment above:

    it's worth noting that using GO (or in this case ;) causes a new branch where declared variables aren't visible past the statement.

    For example:

    DECLARE @id int
    SET @id = 78
    
    SELECT * FROM MyTable WHERE Id = @var; <-- remove this character to avoid the error message
    SELECT * FROM AnotherTable WHERE MyTableId = @var
    

提交回复
热议问题