Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.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 server 如何使用XQuery和xml数据类型在SQL Server中添加节点或新元素_Sql Server_Xquery - Fatal编程技术网

Sql server 如何使用XQuery和xml数据类型在SQL Server中添加节点或新元素

Sql server 如何使用XQuery和xml数据类型在SQL Server中添加节点或新元素,sql-server,xquery,Sql Server,Xquery,这是我的示例SQLServerXML DECLARE @abc varchar(max),@Settingsxml XML,@DoesDefExist varchar(10) SELECT @Settingsxml='<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xa

这是我的示例SQLServerXML

DECLARE @abc varchar(max),@Settingsxml XML,@DoesDefExist varchar(10)

SELECT @Settingsxml='<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib">
  <sys:Int32 x:Key="abc">1200</sys:Int32>
  <sys:Int32 x:Key="xyz">300</sys:Int32>
  <sys:Int32 x:Key="ghi">300</sys:Int32>
  <sys:String x:Key="def">Forgot your login or password? Please contact the IT Dept.</sys:String>
</ResourceDictionary>'

我想在Key=abc元素之后添加节点300,您可以使用如下代码实现这一点:

-- declare a table variable to hold the data
DECLARE @table TABLE (ID INT IDENTITY PRIMARY KEY, XmlContent XML)

-- insert your XML into that table variable
INSERT INTO @table(XmlContent) VALUES(@Settingsxml)

-- define the relevant XML namespaces and UPDATE the table
;WITH XMLNAMESPACES('http://schemas.microsoft.com/winfx/2006/xaml/presentation' AS ns,
'clr-namespace:System;assembly=mscorlib' AS sys,
'http://schemas.microsoft.com/winfx/2006/xaml' as x)
UPDATE @table
SET XmlContent.modify('insert <sys:Int32 x:Key="def">300</sys:Int32> after (/ns:ResourceDictionary/sys:Int32[@x:Key="abc"])[1]')
WHERE ID = 1

-- now, your XML stored in the table variable contains the new element where you wanted to have it
SELECT XmlContent FROM @table
最后的输出是:

<ResourceDictionary 
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       xmlns:sys="clr-namespace:System;assembly=mscorlib">
  <sys:Int32 x:Key="abc">1200</sys:Int32>
  <sys:Int32 x:Key="def">300</sys:Int32>
  <sys:Int32 x:Key="xyz">300</sys:Int32>
  <sys:Int32 x:Key="ghi">300</sys:Int32>
  <sys:String x:Key="def">Forgot your login or password? Please contact the IT Dept.</sys:String>
</ResourceDictionary>
与给出的答案相同,但使用set on XML变量代替update on table变量。无法在此处与xmlnamespaces一起使用,因此

set @Settingsxml.modify('declare default element namespace "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
                         declare namespace x="http://schemas.microsoft.com/winfx/2006/xaml"; 
                         declare namespace sys="clr-namespace:System;assembly=mscorlib";
                         insert <sys:Int32 x:Key="def">300</sys:Int32> 
                         after (/ResourceDictionary/sys:Int32[@x:Key="abc"])[1]')

我了解了如何查询abc节点是否存在,以及如何在abc之后添加def 下面是完整的代码

DECLARE @abc varchar(max),@Settingsxml XML,@DoesDefExist varchar(10)

SELECT @Settingsxml='<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib">
  <sys:Int32 x:Key="abc">1200</sys:Int32>
  <sys:Int32 x:Key="xyz">300</sys:Int32>
  <sys:Int32 x:Key="ghi">300</sys:Int32>
  <sys:String x:Key="def">Forgot your login or password? Please contact the IT Dept.</sys:String>
</ResourceDictionary>'  


SELECT @DoesDefExist=@Settingsxml.value('declare namespace hrn="http://schemas.microsoft.com/winfx/2006/xaml/presentation";
                          declare namespace x="http://schemas.microsoft.com/winfx/2006/xaml";
                          declare namespace sys="clr-namespace:System;assembly=mscorlib";
                          /hrn:ResourceDictionary/sys:Int32/@x:Key="def"','varchar(10)')

IF (@DoesDefExist='false')
BEGIN
SET @Settingsxml.modify('declare namespace hrn="http://schemas.microsoft.com/winfx/2006/xaml/presentation";
                          declare namespace x="http://schemas.microsoft.com/winfx/2006/xaml";
                          declare namespace sys="clr-namespace:System;assembly=mscorlib";
                          insert <sys:Int32 x:Key="def">300</sys:Int32> after
                          (/hrn:ResourceDictionary/sys:Int32[@x:Key="abc"])[1]')
END       

SELECT     @Settingsxml     

有趣的我一直试图这样做,但我使用了SELECT@settingsxml.modify。。。。不断地犯错误。。。。。为什么我没有想到布景呢-@marc_s-我知道SET可以工作,但我不得不在名称空间的语法上挣扎。Relly希望使用默认名称空间。昨晚太晚了,但我现在明白了。