Sql-server – How to execute a stored procedure whose name is in a variable

sql serversql-server-2008-r2

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)?

Best Answer

You can use the variable directly:

exec @stored_procedure_name;

Please see the BOL Reference on EXECUTE.