denimknight
denimknight

Reputation: 321

Call a stored procedure from a column in table in SQL Server

Hope someone can help me with this. I've got a table that will hold stored procedure calls along with the priority of those calls and stored procedure that reads this table and executes the procedures.

The query like this:

DECLARE @sql VARCHAR(MAX)
SET @sql = SELECT TOP 1 procColumn FROM ProcRequest WHERE Status = 'NotRan' ORDER BY Priority, TimeSubmitted
EXEC @sql

When I execute the sql I get the following error:

Msg 2812, Level 16, State 62, Line 3
Could not find stored procedure 'dev.dbo.uspProc1 10,'2013-01-01 00:00:00','2014-01-01 00:00:00''

The procedure exists and if I copy the procedure call from the column in the table and execute it, it runs.

Procedure call looks like this:

dev.dbo.uspProc1 10,'2013-01-01 00:00:00','2014-01-01 00:00:00'

Any ideas where I'm going wrong?

Upvotes: 1

Views: 2050

Answers (1)

chridam
chridam

Reputation: 103365

You could try:

declare @sql varchar(max)
select top 1 @sql = procColumn from ProcRequest where Status = 'NotRan' order by Priority, TimeSubmitted
exec sp_executesql @sql;

Upvotes: 1

Related Questions