[英]Using Variables Passing Into MSSQL Stored Proc as part of queries
I want to use the value from variables that is passed into the stored proc.我想使用传递给存储过程的变量的值。 Is that allow?
那允许吗?
For example, I want to pass CID=5,SID=4 Into an Update Stored Proc例如,我想将 CID=5,SID=4 传递给更新存储过程
and it looks like this:它看起来像这样:
CREATE PROCEDURE Update @CID nvarchar(4),@SID nvarchar(4)
AS
DELETE FROM [User"+@CID+@SID+"]
GO;
In which is like "DELETE FROM [User54]"其中就像“DELETE FROM [User54]”
But I want to dynamically done given the parameter但是我想在给定参数的情况下动态完成
Can it be done and how is it done?能不能做到,怎么做到的?
Thanks谢谢
You must use dynamic SQL. To do it safely, ensure the created object name is properly delimited using the quotename function.您必须使用动态 SQL。为了安全地执行此操作,请确保使用引号 function 正确分隔创建的object名称。
Like this:像这样:
CREATE OR ALTER PROCEDURE UpdateSomeTable @CID nvarchar(4), @SID nvarchar(4)
AS
begin
declare @tableName nvarchar(500) = quotename(concat('User',@CID,@SID));
declare @sql nvarchar(max) = concat('DELETE FROM ',@tableName);
--print @sql
exec sp_executesql @sql
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.