简体   繁体   中英

Filter SQL Queries on XML attributes

I have a column otar.Results in my sql database containing xml data. Here is an example of the xml data:

<certInformationList>
  <certItem layoutSeqOrSealDescrID="1" qxCertInformation="8;20021" />
  <certItem layoutSeqOrSealDescrID="2" qxCertInformation="" />
  <certItem layoutSeqOrSealDescrID="3" qxCertInformation="3308ASX1D1I1S1V4AAR5AE00910WA3WK1M5C1Q8P1W1" />
  <certItem layoutSeqOrSealDescrID="4" qxCertInformation="2014-04-29" />
  <certItem layoutSeqOrSealDescrID="5" qxCertInformation="13:07:43" />
  <certItem layoutSeqOrSealDescrID="6" qxCertInformation="" />
  <certItem layoutSeqOrSealDescrID="7" qxCertInformation="" />
  <certItem layoutSeqOrSealDescrID="8" qxCertInformation="" />
  <certItem layoutSeqOrSealDescrID="9" qxCertInformation="" />
  <certItem layoutSeqOrSealDescrID="10" qxCertInformation="9150077-263" />
  <certItem layoutSeqOrSealDescrID="11" qxCertInformation="260201" />
  <certItem layoutSeqOrSealDescrID="12" qxCertInformation="" />
  <certItem layoutSeqOrSealDescrID="13" qxCertInformation="" />
  <certItem layoutSeqOrSealDescrID="14" qxCertInformation="" />
  <certItem layoutSeqOrSealDescrID="15" qxCertInformation="" />
  <certItem layoutSeqOrSealDescrID="16" qxCertInformation="" />
  <certItem layoutSeqOrSealDescrID="17" qxCertInformation="" />
  <certItem layoutSeqOrSealDescrID="18" qxCertInformation="" />
</certInformationList>

I want to filter my query so I can get the records where layoutSeqOrSealDescrID="1" and qxCertInformation="8;20021 "

I've tried

otar.Results.exist('/certInformationList/certItem[layoutSeqOrSealDescrID="1" and qxCertInformation="8;20021"]') = 1

But it doesn't return any records.

Note: I have to be able to build the query dynamically

Any help would be very appreciated.

Best regards John

在属性名称的开头使用@来引用XPath中的属性,否则将其误认为是同名的子元素:

/certInformationList/certItem[@layoutSeqOrSealDescrID="1" and @qxCertInformation="8;20021"]

There is a table with two records, one is as your example (shortend!), the second has got the "8;20021" changed. Only the first comes back in the select:

DECLARE @otar TABLE(id INT, Results XML);
INSERT INTO @otar VALUES
 (1,'<certInformationList>
        <certItem layoutSeqOrSealDescrID="1" qxCertInformation="8;20021" />
     </certInformationList>')
,(2,'<certInformationList>
        <certItem layoutSeqOrSealDescrID="1" qxCertInformation="8;20022" />
     </certInformationList>');

SELECT *
FROM @otar AS o
WHERE o.Results.exist('//certItem[@layoutSeqOrSealDescrID="1" and @qxCertInformation="8;20021"]')=1;

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