Sql server 在sql server中查询xml值

Sql server 在sql server中查询xml值,sql-server,xml,tsql,xpath,xquery,Sql Server,Xml,Tsql,Xpath,Xquery,我在数据列的下表中有xml字段 ID |网站|数据 下面是xml字段 <Product> <field name="IsCustomer" type="System.Boolean, mscorlib"> <boolean>false</boolean> </field> </product> 有人能帮我吗?我已经写了一篇文章,可能会对你有所帮助 注意:XQuery区分大小写,您需要正确定位自己。看看我链接的

我在数据列的下表中有xml字段

ID |网站|数据

下面是xml字段

<Product>
 <field name="IsCustomer" type="System.Boolean, mscorlib">
    <boolean>false</boolean>
  </field>
</product>

有人能帮我吗?

我已经写了一篇文章,可能会对你有所帮助

注意:XQuery区分大小写,您需要正确定位自己。看看我链接的帖子,如果没有帮助,我会用一个关于你的问题的解决方案更新这个评论

更新


回答你的问题

首先,您需要正确格式化XML,这意味着标记需要匹配其大小写敏感度,当您在XQuery中引用标记时,同样的规则也适用

因此,解决方案之一是:

DECLARE @XML as XML
SET @XML = '<Product>
 <field name="IsCustomer" type="System.Boolean, mscorlib">
    <boolean>false</boolean>
  </field>
</Product>'

SELECT EMP.t.value('boolean[1]','varchar(20)') as EmployeeID
FROM   @XML.nodes('/Product/field') as EMP(t)
将@XML声明为XML
SET@XML=
假的
'
选择EMP.t.value('boolean[1],'varchar(20')作为EmployeeID
从@XML.nodes('/Product/field')作为EMP(t)

我已经写了一篇文章,可能会对你有所帮助

注意:XQuery区分大小写,您需要正确定位自己。看看我链接的帖子,如果没有帮助,我会用一个关于你的问题的解决方案更新这个评论

更新


回答你的问题

首先,您需要正确格式化XML,这意味着标记需要匹配其大小写敏感度,当您在XQuery中引用标记时,同样的规则也适用

因此,解决方案之一是:

DECLARE @XML as XML
SET @XML = '<Product>
 <field name="IsCustomer" type="System.Boolean, mscorlib">
    <boolean>false</boolean>
  </field>
</Product>'

SELECT EMP.t.value('boolean[1]','varchar(20)') as EmployeeID
FROM   @XML.nodes('/Product/field') as EMP(t)
将@XML声明为XML
SET@XML=
假的
'
选择EMP.t.value('boolean[1],'varchar(20')作为EmployeeID
从@XML.nodes('/Product/field')作为EMP(t)

首先:XML严格区分大小写您的XML甚至无效。。。前导的
是另一个元素名称,然后是结束的
。因为你似乎在所有地方都使用小写字母,所以我用这种方式改变了它

您自己的查询已关闭,但某些大写字母错误,并且未正确使用
.value()
-函数(缺少参数)

试试这个:

DECLARE @mockup TABLE(ID INT IDENTITY,Descr VARCHAR(100),Data XML);
INSERT INTO @mockup VALUES
('Your Sample','<product>
 <field name="IsCustomer" type="System.Boolean, mscorlib">
    <boolean>false</boolean>
  </field>
</product>')
,('Your sample plus another field','<product>
 <field name="IsCustomer" type="System.Boolean, mscorlib">
    <boolean>true</boolean>
  </field>
 <field name="SomeOther" type="System.Boolean, mscorlib">
    <boolean>true</boolean>
  </field>
</product>')
,('No "IsCustomer" at all','<product>
 <field name="SomeOther" type="System.Boolean, mscorlib">
    <boolean>true</boolean>
  </field>
</product>')
,('Two of them','<product>
 <field name="IsCustomer" type="System.Boolean, mscorlib">
    <boolean>true</boolean>
  </field>
 <field name="IsCustomer" type="System.Boolean, mscorlib">
    <boolean>false</boolean>
  </field>
</product>');

SELECT * FROM @mockup;

首先:XML严格区分大小写您的XML甚至无效。。。前导的
是另一个元素名称,然后是结束的
。因为你似乎在所有地方都使用小写字母,所以我用这种方式改变了它

您自己的查询已关闭,但某些大写字母错误,并且未正确使用
.value()
-函数(缺少参数)

试试这个:

DECLARE @mockup TABLE(ID INT IDENTITY,Descr VARCHAR(100),Data XML);
INSERT INTO @mockup VALUES
('Your Sample','<product>
 <field name="IsCustomer" type="System.Boolean, mscorlib">
    <boolean>false</boolean>
  </field>
</product>')
,('Your sample plus another field','<product>
 <field name="IsCustomer" type="System.Boolean, mscorlib">
    <boolean>true</boolean>
  </field>
 <field name="SomeOther" type="System.Boolean, mscorlib">
    <boolean>true</boolean>
  </field>
</product>')
,('No "IsCustomer" at all','<product>
 <field name="SomeOther" type="System.Boolean, mscorlib">
    <boolean>true</boolean>
  </field>
</product>')
,('Two of them','<product>
 <field name="IsCustomer" type="System.Boolean, mscorlib">
    <boolean>true</boolean>
  </field>
 <field name="IsCustomer" type="System.Boolean, mscorlib">
    <boolean>false</boolean>
  </field>
</product>');

SELECT * FROM @mockup;

很抱歉那是我的错误。它的sql服务器。给定这个示例XML作为输入,预期输出是什么?只是
false
?我想将所有字段值作为单独的列获取。例如真值和假值的组合。对不起。那是我的错误。它的sql server。如果将示例XML作为输入,预期的输出是什么?只是
false
?我想将所有字段值作为单独的列获取。例如真值和假值的组合。欢迎使用堆栈溢出!虽然这可以从理论上回答这个问题,但在这里包括答案的基本部分,并提供链接供参考。欢迎使用堆栈溢出!虽然这在理论上可以回答这个问题,但在这里包括答案的基本部分,并提供链接供参考。