[英]SQL Store a value from an EXECUTE Command into a variable
I'm attempting to store a value into a variable from a EXECUTE command. 我正在尝试从EXECUTE命令将值存储到变量中。 I know I'm suppose to use sp_executesql command, but all examples online are only making more confused. 我知道我想使用sp_executesql命令,但是所有在线示例都更加令人困惑。 So here is what I'm trying to do. 所以这就是我想要做的。
I have a stored procedure that accepts two parameters (a table name, a room #). 我有一个存储过程,该过程接受两个参数(表名,房间号)。 To have a dynamic table name, I use dynamic SQL style while using strings. 为了具有动态表名,我在使用字符串时使用了动态SQL样式。 I'm attempting to store a phone number that is either from multiple tables. 我正在尝试存储来自多个表的电话号码。 I got this working so far. 到目前为止,我已经完成了这项工作。
DECLARE @Location varchar(MAX);
DECLARE @Room varchar(10);
DECLARE @Number char(8);
DECLARE @SQLString varchar(MAX);
SET @Location = N'CMPhone.dbo.GardenCottage';
SET @Room = N'202';
SET @SQLString ='SET @Number = (SELECT PhoneNumber FROM ' + @Location + ' WHERE Room = ''' + @Room + ''');';
PRINT(@SQLString);
OUTPUT
SET @Number = (SELECT PhoneNumber FROM CMPhone.dbo.GardenCottage WHERE Room = '202');
SET @Number = (SELECT PhoneNumber FROM CMPhone.dbo.GardenCottage WHERE Room = '202');
PRINT(@Number);
OUTPUT
123-4567
Which is the correct number. 哪个是正确的数字。 Now, here is where the problem comes in. I need to do another query using dynamic SQL so I can use multiple tables again. 现在,这里是问题所在。我需要使用动态SQL进行另一个查询,以便可以再次使用多个表。 So in my stored procedure, I need to store my EXEC(@SQLString) into a variable (@Number) so I can use that value, and that's where I'm having problems. 因此,在存储过程中,我需要将EXEC(@SQLString)存储到变量(@Number)中,以便可以使用该值,而这正是我遇到的问题。 I can't get sp_executesql to store the value into @Number. 我无法获取sp_executesql将值存储到@Number中。 The other query will look something like this 另一个查询看起来像这样
SET @SQLString = ' UPDATE PhoneNumbers SET Active = ''1'' WHERE
PhoneNumber = ''' + @Number + ''';';
EXEC(@SQLString);
If this is confusing in anyway, or you have questions, please ask. 如果这仍然令人困惑,或者您有任何疑问,请询问。 Any help is very much appreciated. 很感谢任何形式的帮助。 Thanks 谢谢
Update #1: 更新#1:
I have this new string now 我现在有了这个新字符串
@SQLString = 'SELECT PhoneNumber FROM ' + @Location ' + ' WHERE Room = ''' + @Room + ''';';
EXECUTE SP_EXECUTESQL @SQLString
gets the correct number, but I don't know how to set up a OUTPUT parameter. 获取正确的数字,但我不知道如何设置OUTPUT参数。
I'm attempting to follow this example from Microsoft 我正在尝试按照Microsoft的示例进行操作
DECLARE @SQLString NVARCHAR(500)
DECLARE @ParmDefinition NVARCHAR(500)
DECLARE @IntVariable INT
DECLARE @Lastlname varchar(30)
SET @SQLString = N'SELECT @LastlnameOUT = max(lname)
FROM pubs.dbo.employee WHERE job_lvl = @level'
SET @ParmDefinition = N'@level tinyint,
@LastlnameOUT varchar(30) OUTPUT'
SET @IntVariable = 35
EXECUTE sp_executesql
@SQLString,
@ParmDefinition,
@level = @IntVariable,
@LastlnameOUT=@Lastlname OUTPUT
SELECT @Lastlname
But I don't see how their declaring the lastlNameOUT variables. 但是我看不到他们如何声明lastlNameOUT变量。
use output variable in your EXECUTE sp_executesql
like this: 在EXECUTE sp_executesql
使用输出变量,如下所示:
EXECUTE sp_executesql @SQLString, N'@Number char(8) out',@Number out
then you will get @Number value from inside dynamc sql, then you can use that value in other part of the query. EXECUTE sp_executesql @SQLString, N'@Number char(8) out',@Number out
然后您将从动态sql内部获取@Number值,然后可以在查询的其他部分使用该值。 hope this helps 希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.