Scope of Variables

  • Can anyone tell me what the scope of a variable is...for example in an sql script one of the first commands is the declaration of a variable.

    However later in the script when I need to use the variable again, I get the usual 'must declare variable' error. How can I get around this other than declaring and setting the variable each time I want to use it (which will obviously defeat the purpose of using a variable in the first place)??

    Thanks for your help guys, greatly appreciated.

     


    If riding in a plane is flying, then riding in a boat must be swimming. To experience the element, get out of the vehicle. Skydive!

  • If you use GO within the statement then it's going to clean out the variable.

    You could just execute the statements without the go and keep the variable.



    Shamless self promotion - read my blog http://sirsql.net

  • BOL is your friend:

    The scope of a local variable is the batch, stored procedure, or statement block in which it is declared.

    Mr. or Ms. alluded to the batch, which is separated by the keyword GO.

    There is no "i" in team, but idiot has two.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply