Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Execute stored proc dynamically (stored as a variable)

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2006-01-24 : 08:39:35
Calvin writes "Hi,
Can someone please show me how to execute a stored proc which has been assigned to a variable. I have a table of stored procedures and I want to pick out a certain sp, based on a set of criteria, and execute it dynamically. I'm thinking of setting up a CURSOR to loop through the selected sp, assign each one to a variable and then execute it but I don't know how yet.
I would also like to know how to pass a variable of type TABLE to the above stored proc. This table type variable contains a list of parameters in a form of "key-value" pairs.

Example
DECLARE @myTable TABLE(
paramName Varchar(100),
paramValue Varchar(100))
INSERT INTO @myTable(paramName, paramValue)
VALUES ('param1', '123')

DECLARE @myStoredProc Varchar(100)
DECLARE myCursor CURSOR FOR
SELECT StoredProcName
FROM tableOfStoredProcs
WHERE something = somethingelse
OPEN myCursor
FETCH NEXT FROM myCursor
INTO @myStoredProc
...
EXEC @myStoredProc(@myTable) -- this is what i want to do but syntactically incorrect.

I'm using SQL Server 2000.
Any suggestion is greatly appreciated.
Calvin"

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-01-24 : 08:46:26
You should read this
http://www.sommarskog.se/dynamic_sql.html

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -