简体   繁体   中英

How do I store a SQL Proc name with an argument in a string?

This is all within a TSQL stored procedure. I have a variable that contains the name of a stored procedure I want to execute, up until now this method has been used only with stored procedures that do no take arguments. I'm having troulbe fitting a proc that does take an argument into the same mold.

In my main driver procedure I have a line EXECUTE @process_name and @process_name is normally something like 'database..procedure' with no arugments. How do I fit the name of a stored proc and its arguments into one string so that when I call the line EXECUTE @process_name it will execute the procedure with arguments?

I can't change this execute line at all, I really need to fit the proc name and arguments into the single string @process_name . I realize it may not be the best practice.

Thanks for any help!

You put the same thing in @process_name as though you were typing it out like so:

--Create your Stored Procedure
CREATE PROCEDURE dbo.YourStoredProc
    @FirstVariable VARCHAR(30),
    @SecondVariable VARCHAR(30)
AS
BEGIN
    SELECT @FirstVariable,@SecondVariable
END
GO


DECLARE @process_name VARCHAR(MAX);
--List the name of your stored procedure you want to execute
    --Followed by variables and their values
SET @process_name = 'dbo.YourStoredProc @firstvariable = ''Hello'', @SecondVariable = ''World!'''

EXEC (@process_name)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM