Click here to Skip to main content
15,887,880 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to create a table with some data, including the name of the stored procedure that I'm supposed to run... Is it possible to execute a procedure using a text containing its name (I'm assuming that there are always 2 parameters... so it doesn't matter...)
Posted

--SP Creation
CREATE PROCEDURE Test
@param1 varchar(10),
@param2 varchar(10)
AS
BEGIN
Select Column1, column2 ... From Table1
END
GO

--Simple SP execution
exec Test '1', '2'

Declare @SPwithParameter varchar(1000)
Set @SPwithParameter = 'Test'

--SP execution from text
execute @SPwithParameter '1', '2'
 
Share this answer
 
Yes

SQL
--Assuming that you have the following SP:
create procedure CallTest
as
begin
select sellerName, 'US' as 'Country' from sellers
end
go

--Table to store SP name:
Create table sp_data (iid int, sp_name varchar(50));

--insert sp name
insert into sp_data  values (1, 'CallTest');

--get name from db:
declare @sp_name varchar(50)
select @sp_name = sp_name from sp_data

--Execute the SP
exec (@sp_name);
 
Share this answer
 
Comments
ShacharK 24-Oct-11 8:18am    
Great, and if I assume that data is variant, can it be converted automatically (or throwing an exception, I don't care...) to the input type of the destinated procedure?

@v1 and @v2 are variants that are supposed to be passed into the procedure...

I mean, exec (@sp_name) @v1 @v2

and the specified procedure is actually expecting integers...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900