Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.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 如何根据SQL中XML属性名称的值筛选行_Sql Server_Xml_Tsql_Xquery - Fatal编程技术网

Sql server 如何根据SQL中XML属性名称的值筛选行

Sql server 如何根据SQL中XML属性名称的值筛选行,sql-server,xml,tsql,xquery,Sql Server,Xml,Tsql,Xquery,我有一个带有列属性的表Web.Log,其中包含以下内容 <properties> ... <property key="ActivityId" /> <property key="UserName">John Doe</property> </properties> 感觉像是解决方法:-使用XQUERY并存在: 选择{Columns} 从Web.Log.L 在存在的位置选择1 从L.[Properties].节点'/Pro

我有一个带有列属性的表Web.Log,其中包含以下内容

<properties>
  ...
  <property key="ActivityId" />
  <property key="UserName">John Doe</property>
</properties>
感觉像是解决方法:-

使用XQUERY并存在:

选择{Columns} 从Web.Log.L 在存在的位置选择1 从L.[Properties].节点'/Properties/property'pp 其中p.p.value'text[1],'nvarchar50'='John Doe' 使用XQUERY并存在一个:

选择{Columns} 从Web.Log.L 在存在的位置选择1 从L.[Properties].节点'/Properties/property'pp 其中p.p.value'text[1],'nvarchar50'='John Doe'
使用原生XML方法.exist应该更快

首先创建一个模拟场景来模拟您的问题:

DECLARE @tbl TABLE(ID INT IDENTITY, SomeText VARCHAR(100),Properties XML);
INSERT INTO @tbl VALUES('John Doe exists'
                       ,'<properties>
                            <property key="ActivityId" />
                            <property key="UserName">John Doe</property>
                         </properties>')
                        ,('John Doe doesn''t exist'
                       ,'<properties>
                            <property key="ActivityId" />
                            <property key="UserName">Someone else</property>
                         </properties>')
                        ,('John Doe exists'
                       ,'<properties>
                            <property key="ActivityId" />
                            <property key="UserName">John Doe</property>
                         </properties>');
-以下是查询:

SELECT *
FROM @tbl t 
WHERE t.Properties.exist('/properties/property[@key=sql:variable("@SearchFor") and text()[1]=sql:variable("@FindThis")]')=1;
简而言之:

我们可以在WHERE子句中直接使用.exist。 当给定XPath至少命中一次时,WHERE为TRUE 可以将=1设置为=0,以返回负值集 XPath使用XQuery谓词搜索具有给定键和值的。
使用原生XML方法.exist应该更快

首先创建一个模拟场景来模拟您的问题:

DECLARE @tbl TABLE(ID INT IDENTITY, SomeText VARCHAR(100),Properties XML);
INSERT INTO @tbl VALUES('John Doe exists'
                       ,'<properties>
                            <property key="ActivityId" />
                            <property key="UserName">John Doe</property>
                         </properties>')
                        ,('John Doe doesn''t exist'
                       ,'<properties>
                            <property key="ActivityId" />
                            <property key="UserName">Someone else</property>
                         </properties>')
                        ,('John Doe exists'
                       ,'<properties>
                            <property key="ActivityId" />
                            <property key="UserName">John Doe</property>
                         </properties>');
-以下是查询:

SELECT *
FROM @tbl t 
WHERE t.Properties.exist('/properties/property[@key=sql:variable("@SearchFor") and text()[1]=sql:variable("@FindThis")]')=1;
简而言之:

我们可以在WHERE子句中直接使用.exist。 当给定XPath至少命中一次时,WHERE为TRUE 可以将=1设置为=0,以返回负值集 XPath使用XQuery谓词搜索具有给定键和值的。
Larnu,使用XML.exist应该更快。使用XML.nodes和WHERE将需要读取整个集合,只需检查一个键值对的存在性…Larnu,使用XML.exist应该更快。使用XML.nodes和WHERE将需要读取整个集合,只需检查是否存在一个键值对。。。