Link to home
Start Free TrialLog in
Avatar of Daniel Wilson
Daniel WilsonFlag for United States of America

asked on

SQL Server 2000 - "must declare variable '@ID'" after USE statement

I have a variable scoping problem.  The following (simplified) script generates 2 "Must declare the variable" errors.

What do I need to do to overcome the scope issue and use the variable against both DB's?

Thanks!

Msg 137, Level 15, State 2, Line 1
Must declare the variable '@ID'.
Msg 137, Level 15, State 2, Line 3
Must declare the variable '@ID'.
begin tran
 
Declare @ID int
Set @ID = 5
 
use Database1
go
Select @ID
go
 
use Database2
go
Select @ID
 
rollback tran

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of BrandonGalderisi
BrandonGalderisi
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of chapmandew
chapmandew
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Daniel Wilson

ASKER

Well, that was easy enough!  Thanks!