HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlModerate

How can I execute a stored procedure whose name is in a variable?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
storedcanwhoseprocedurenamehowvariableexecute

Problem

Given a variable that contains a stored procedure name:

DECLARE @stored_procedure_name varchar(512);
SET @stored_procedure_name = 'some_stored_procedure_name';


How can I execute the stored procedure (without passing in any arguments)?

Solution

You can use the variable directly:

exec @stored_procedure_name;


Please see the BOL Reference on EXECUTE.

Code Snippets

exec @stored_procedure_name;

Context

StackExchange Database Administrators Q#46980, answer score: 14

Revisions (0)

No revisions yet.