Reputation: 139
I've just learned how to create stored procedures in SQL (using SQL Server 2008). I'm wondering if it's possible to do this:
USE [SomeDB]
GO
DECLARE @var int = 3
GO
EXEC SomeProcedure @param = @var
GO
When I try this, it gives the error: "Must declare the scalar variable "@var"."
I'm basically trying to pass a variable as a parameter. There surely must be a way to do this?
Upvotes: 1
Views: 5353
Reputation: 15085
The GO command separates SQL batches. The second GO command causes the DECLARE statement variable to be dropped, since the EXEC starts a new batch of commands
USE [SomeDB]
GO
DECLARE @var int = 3
-- GO
EXEC SomeProcedure @param = @var
GO
Upvotes: 5