Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL XQuery-将整数SQL变量值插入xml变量_Sql_Sql Server 2008_Xquery Sql - Fatal编程技术网

SQL XQuery-将整数SQL变量值插入xml变量

SQL XQuery-将整数SQL变量值插入xml变量,sql,sql-server-2008,xquery-sql,Sql,Sql Server 2008,Xquery Sql,我得到了这个问题: DECLARE @UserId INT DECLARE @StateChangeInformation XML SET @UserId = 1 SET @StateChangeInformation = '<stateChangeInformation EnableOverrideMarking="1"></stateChangeInformation>' SET @command = 'DECLARE @UserIdXml XML; SET @Us

我得到了这个问题:

DECLARE @UserId INT
DECLARE @StateChangeInformation XML

SET @UserId = 1
SET @StateChangeInformation = '<stateChangeInformation EnableOverrideMarking="1"></stateChangeInformation>'
SET @command = 'DECLARE @UserIdXml XML; SET @UserIdXml = ''<userID>{ sql:variable("@UserId") }</userID>''; SET @StateChangeInformation.modify(''insert sql:variable("@UserIdXml") into (/stateChangeInformation)[1]'')'

EXEC sp_executesql @stmt = @command,
                   @params = N'@StateChangeInformation xml out',
                   @StateChangeInformation= @StateChangeInformation OUTPUT

SELECT @StateChangeInformation
DECLARE@UserId INT
声明@statechangeinformationxml
设置@UserId=1
设置@StateChangeInformation=“”
SET@command='DECLARE@UserIdXml;SET@UserIdXml='{sql:variable(@UserId”)}';设置@StateChangeInformation.modify(“'insert sql:variable(@UserIdXml”)到(/StateChangeInformation)[1]”)
EXEC sp_executesql@stmt=@命令,
@params=N'@StateChangeInformation xml out',
@StateChangeInformation=@StateChangeInformation输出
选择@StateChangeInformation
我要做的是将XML输出为:

<stateChangeInformation>
  <userID>1</userID>
</stateChangeInformation>

1.
目前,我得到以下输出:

<stateChangeInformation EnableOverrideMarking="1">
  <userID>"{ sql:variable("@UserId") }"</userID>
</stateChangeInformation>

“{sql:variable(@UserId”)}”

请帮忙

您需要将
@UserId
作为参数发送到动态SQL,并使用连接来获取XML中的值

DECLARE @command nvarchar(max)
DECLARE @UserId INT
DECLARE @StateChangeInformation XML

SET @UserId = 1
SET @StateChangeInformation = '<stateChangeInformation EnableOverrideMarking="1"></stateChangeInformation>'
SET @command = 'DECLARE @UserIdXml XML; 
                SET @UserIdXml = ''<userID>''+cast(@UserId as varchar(10))+''</userID>''; 
                SET @StateChangeInformation.modify(''insert sql:variable("@UserIdXml") into (/stateChangeInformation)[1]'')'

EXEC sp_executesql @stmt = @command,
                   @params = N'@StateChangeInformation xml out, @UserId int',
                   @StateChangeInformation= @StateChangeInformation OUTPUT, @UserId = @UserId

SELECT @StateChangeInformation
DECLARE@command-nvarchar(最大值)
声明@UserId INT
声明@statechangeinformationxml
设置@UserId=1
设置@StateChangeInformation=“”
SET@command='DECLARE@UserIdXml;
将@UserIdXml=''''设置为+cast(@UserId为varchar(10))+'';
设置@StateChangeInformation.modify(“'insert sql:variable(@UserIdXml”)到(/StateChangeInformation)[1]”)
EXEC sp_executesql@stmt=@命令,
@params=N'@StateChangeInformation xml out,@UserId int',
@StateChangeInformation=@StateChangeInformation输出,@UserId=@UserId
选择@StateChangeInformation

是的,我已经知道了。我完全按照你说的做了,所以我会记下你的答案:谢谢!