[英]How do I store a SQL Proc name with an argument in a string?
This is all within a TSQL stored procedure. 所有这些都在TSQL存储过程中。 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. 我正在使用troulbe装配一个确实在同一个模具中引起争论的proc。
In my main driver procedure I have a line EXECUTE @process_name
and @process_name
is normally something like 'database..procedure' with no arugments. 在我的主驱动程序过程中,我有一行EXECUTE @process_name
而@process_name
通常是类似'database..procedure'的东西,没有任何用处。 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? 如何将存储的proc的名称及其参数放入一个字符串中,以便当我调用EXECUTE @process_name
,它将使用参数执行该过程?
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 . 我根本无法更改此执行行,我真的需要将proc名称和参数放入单个字符串@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: 您在@process_name中输入了相同的内容,就像在键入时一样:
--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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.