Mr_Bean
Mr_Bean

Reputation: 139

SQL - Execute Procedure with Variable as Parameter

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

Answers (1)

Sparky
Sparky

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

Related Questions