Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. How to count values in XML structure using XQuery statement

How to count values in XML structure using XQuery statement

Scheduled Pinned Locked Moved Database
questiondatabasexmltutorialannouncement
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    David Mujica
    wrote on last edited by
    #1

    Here is what my XML document looks like.

    <DATAMATRIX>
    <FSDATAMATRIX>
    <DM_OBJECT_TYPE>ITEM0</DM_OBJECT_TYPE>
    <FIELD1>BELGIUM</FIELD1>
    <FIELD2>BEVERAGES, NON-ALCOHOLIC</FIELD2>
    <FIELD4>NO</FIELD4>
    <FIELD5>YES</FIELD5>
    </FSDATAMATRIX>
    <FSDATAMATRIX>
    <DM_OBJECT_TYPE>ITEM0</DM_OBJECT_TYPE>
    <FIELD1>BRAZIL</FIELD1>
    <FIELD2>CHEESE</FIELD2>
    <FIELD4>NO</FIELD4>
    <FIELD5>YES</FIELD5>
    </FSDATAMATRIX>
    </DATAMATRIX>

    What I need to do it perform a count on the above XML document to see if an entry for "BELGIUM" already exists. If it doesn't exist, I will perform an insert, otherwise I will update the values. How do I perform a count on FIELD1 where it matches a variable ? Something like: count FIELD1=sql:variable("@XCOUNTRY") Thanks again, my data loading project is nearly complete. This should be the last step.

    L 1 Reply Last reply
    0
    • D David Mujica

      Here is what my XML document looks like.

      <DATAMATRIX>
      <FSDATAMATRIX>
      <DM_OBJECT_TYPE>ITEM0</DM_OBJECT_TYPE>
      <FIELD1>BELGIUM</FIELD1>
      <FIELD2>BEVERAGES, NON-ALCOHOLIC</FIELD2>
      <FIELD4>NO</FIELD4>
      <FIELD5>YES</FIELD5>
      </FSDATAMATRIX>
      <FSDATAMATRIX>
      <DM_OBJECT_TYPE>ITEM0</DM_OBJECT_TYPE>
      <FIELD1>BRAZIL</FIELD1>
      <FIELD2>CHEESE</FIELD2>
      <FIELD4>NO</FIELD4>
      <FIELD5>YES</FIELD5>
      </FSDATAMATRIX>
      </DATAMATRIX>

      What I need to do it perform a count on the above XML document to see if an entry for "BELGIUM" already exists. If it doesn't exist, I will perform an insert, otherwise I will update the values. How do I perform a count on FIELD1 where it matches a variable ? Something like: count FIELD1=sql:variable("@XCOUNTRY") Thanks again, my data loading project is nearly complete. This should be the last step.

      L Offline
      L Offline
      leoinfo
      wrote on last edited by
      #2

      SELECT @XMLDOC.value('count(/DATAMATRIX/FSDATAMATRIX[FIELD1=sql:variable("@XCOUNTRY")])','int')

      Please... SAVE my time by rating the posts that you read!


      There are 10 kinds of people in the world: those who understand binary and those who don't.

      D 1 Reply Last reply
      0
      • L leoinfo

        SELECT @XMLDOC.value('count(/DATAMATRIX/FSDATAMATRIX[FIELD1=sql:variable("@XCOUNTRY")])','int')

        Please... SAVE my time by rating the posts that you read!


        There are 10 kinds of people in the world: those who understand binary and those who don't.

        D Offline
        D Offline
        David Mujica
        wrote on last edited by
        #3

        Perfect. My data load project is now complete. I could not have done it without your help. Thanks again. David

        L 1 Reply Last reply
        0
        • D David Mujica

          Perfect. My data load project is now complete. I could not have done it without your help. Thanks again. David

          L Offline
          L Offline
          leoinfo
          wrote on last edited by
          #4

          You're welcome :) So... what's the next project about?

          Please... SAVE my time by rating the posts that you read!


          There are 10 kinds of people in the world: those who understand binary and those who don't.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups