Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

xml: number of rows in openxml

Posted on 2011-03-09
1
Medium Priority
?
1,148 Views
Last Modified: 2012-05-11
I have this XML
declare @hind int
declare @xmldoc nvarchar(1000)

set @xmldoc = N' <Inventory>
<Item ProductID = "10" ProductDesc = "Aluminum Beam, Angle">
      <Trx TrxID = "11" TrxDate = "4/19/2008" />
      <Trx TrxID = "18" TrxDate = "4/27/2008" />
</Item>
<Item ProductID = "11" ProductDesc = "Steam Beam, Angle">
      <Trx TrxID = "25" TrxDate = "1/1/2008" />
      <Trx TrxID = "27" TrxDate = "2/13/2008" />
</Item>
</Inventory>'

query 1
exec sp_xml_preparedocument @hind output, @xmldoc
select *
from openxml (@hind,'Inventory/Item',2)
with
      (
            ProductID int '@ProductID',
            TrxID int './Item/@Trx',
            TrxDate varchar(10) './Item/@TrxDate'
      )
exec sp_xml_removedocument @hind
only show productid 10 and 11

query 2
exec sp_xml_preparedocument @hind output, @xmldoc
select *
from openxml (@hind,'Inventory/Item',2)
with
      (
            ProductID int '@ProductID',
            TrxID int './Item/@Trx',
            TrxDate varchar(10) './Item/@TrxDate'
      )
exec sp_xml_removedocument @hind
only show productid 10 and 11 (2 rows)

query 2
exec sp_xml_preparedocument @hind output, @xmldoc
select *
from openxml (@hind,'Inventory/Item/Trx',1)
with
      (
            ProductID int '../@ProductID',
            TrxID int,
            TrxDate varchar(10)
      )
exec sp_xml_removedocument @hind

show productid 10 and 11 two times (4 rows)

what is the difference? 2 and 4 rows?
0
Comment
Question by:enrique_aeo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1000 total points
ID: 35096244
Because you are selecting different sets of nodes with the different XPaths (in the OPENXML lines).

In the top two queries, you select nodes using the path "Inventory/Item" and there are two Item nodes; this results in two "rows" being created.

In the bottom query, you select nodes using the path "Inventory/Item/Trx". There are four Trx nodes in the example you provided, so four "rows" are being created.
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question