简体   繁体   English

如何在字符串中存储带有参数的SQL Proc名称?

[英]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.

相关问题 如何在 SQL/SAS PROC SQL 中找到第四条记录? - How do I find the 4th record in SQL/ SAS PROC SQL? 如何将SQL语句存储到变量中 - How do I store a SQL statement into a variable 当表被覆盖时,如何在proc sql中创建此case语句并追加? - How do I create this case statements in proc sql and append when tables are overwritten? 如何在 SAS Proc SQL 中建立只读的“连接到 ODBC”连接? - How do I make a read-only "Connect to ODBC" connection in SAS Proc SQL? 如何删除 | 使用 proc sql 的 CHAR 数据集中的特殊字符? - How do I remove the | special character in a CHAR dataset using proc sql? 如何使用proc SQL合并多个变量 - How do I merge by more than one variable using proc SQL is SAS PROC SQL:如何将多个查询合而为一,以便解决方案并排? - PROC SQL: How do i put multiple queries in one so the solutions are side by side? 如何遍历Sql Server 2005存储的proc中的分层数据? - How do I iterate through hierarchical data in a Sql Server 2005 stored proc? 如何将proc sql表中的行数限制为包含最高日期的行数 - How do I limit the number of rows in a proc sql table to ones which contain the highest date 如何使用SQL中存储过程的值填充结果视图列? - How do I Populate a Result View Column with Value from Stored Proc in SQL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM