Search code examples
sqlsqlfiddle

SQLFiddle: must declare the scalar variable error


I'm trying to execute next SQL statement (SQL Server 2008 Profile)

DECLARE @fun int;
SET @fun = 40;

select cast(@fun as varchar(10)) + 'hello'

and SQLFiddle gives me the error: Must declare the scalar variable @fun

Where I'm wrong?

enter image description here

http://sqlfiddle.com/#!3/d41d8/42156


Solution

  • I think the semicolons are introducing the problem here.

    As described here: http://www.sql-server-helper.com/error-messages/msg-137.aspx and here: SQL Server - Variable declared but still says "Must declare the scalar variable", the problem arises when the statements are executed individually instead of as a “unit”, and the semicolons seem to trigger that.

    When I remove the semicolons from your statement, it works: http://sqlfiddle.com/#!3/d41d8/42159