简体   繁体   中英

Query for xml values in sql server

I have xml field in the below table in Data column

ID | website | Data

Following is the xml field

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

I need to retrieve all the IsCustomer values in my table.

Following is the code part that I tried so far.

SELECT EMP.ED.value() as EmployeeID
FROM   [dbo].[Products]
CROSS APPLY Data.nodes('/Product/Field[@Name="IsCustomer"]/Boolean') as EMP(ED)

Can anyone please help me?

First of all: XML is strictly case sensitive! Your XML is not even valid... The leading <Product> is another element-name then the closing </product> . As you seem to use lower letters in all places, I changed it this way.

Your own query is close, but wrong with some capital letters and you did not use the .value() -function properly (missing paramters).

Try this:

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;

--Your query returning various variants, one of them should be okay for you:

SELECT m.ID
      ,m.Descr
      ,fld.value('(boolean/text())[1]','bit')
FROM @mockup AS m
OUTER APPLY m.Data.nodes('/product/field[@name="IsCustomer"]') AS A(fld);

I already wrote a post about XQuerying which might be helpful to you.

Note: XQuerying is case sensitive and you need to position yourself properly. Take a look at the post i linked, and if it does not help i will update this comment with a solution regarding your query

Update


To answer your question

First and foremost you need to have properly formatted XML, which means tags need to match their case sensitivity, and same rule applies when you are referencing a tag in a XQuery.

So one of the solutions would be :

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM