none
Must declare the scalar variable

    Question

  • Tools: SQL Server 2008

    Environment:Windows Vista

    Problem: I declared global variables in my SQL code. It was recognized in 1st code block, Whereas, at the end of my code at the bottom of the page was not recognized. In other words, @Event_Date_Range_1 was consumed/ used with no problem but at the bottom of the page was a problem. An error message showed

    Msg 137, Level 15, State 2, Line 87
    Must declare the scalar variable "@Event_Date_Range_1".



    • Edited by Sandra VO Tuesday, July 31, 2012 8:31 PM
    Tuesday, July 31, 2012 6:37 PM

Answers

  • Try using the variables exactly the same as you declared them (e.g. @Event_Date_Range_1 -- notice the casing). There is a slight chance you're using case sensitive SQL Server collation (which is very troublesome).

    Also, make sure you don't have GO in the middle of the batch that will be a batch separator. In this case the variables declared at the top are not visible to the second batch.


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog

    • Marked as answer by Sandra VO Tuesday, July 31, 2012 6:46 PM
    Tuesday, July 31, 2012 6:41 PM

All replies

  • Try using the variables exactly the same as you declared them (e.g. @Event_Date_Range_1 -- notice the casing). There is a slight chance you're using case sensitive SQL Server collation (which is very troublesome).

    Also, make sure you don't have GO in the middle of the batch that will be a batch separator. In this case the variables declared at the top are not visible to the second batch.


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog

    • Marked as answer by Sandra VO Tuesday, July 31, 2012 6:46 PM
    Tuesday, July 31, 2012 6:41 PM
  • it was the GO statement. Thanks Naomi 
    Tuesday, July 31, 2012 6:46 PM
  • It could be that you are using a case sensitive instance of SQL Server.

    SELECT SERVERPROPERTY('Collation') AS server_collation;
    GO
    If so, then use in the WHERE clause exactly same names you used to declare the variables.

    AMB

    Some guidelines for posting questions...


    Tuesday, July 31, 2012 6:46 PM
  • Thanks It was the Go statement eghrrrrrrr
    Tuesday, July 31, 2012 7:37 PM