We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

XQuery returns one row where XML item has many occurrences.  (SQL Server 2005)

jflanner
jflanner asked
on
Medium Priority
261 Views
Last Modified: 2008-03-10
folks:

I'm trying to learn SQL server 2005 XML support.  As an academic exercise, I put together the below script.  Everything is working fine except when i do my xquery.  What I'm getting back is one row containing a concatination of the strings I'm trying to query out.  Specifically, I get:
Item
-----------------------------
Tricycle Wicker Basket

(1 row(s) affected)

where I want
-----------------------------
Tricycle
Wicker Basket

(2 row(s) affected)

The XML I'm querying against is:
<Sale>
<EmployeeID>2</EmployeeID>
<LineItems>
<LineItem><Item>Tricycle</Item><Cost>28.65</Cost></LineItem>
<LineItem><Item>Wicker Basket</Item><Cost>1.65</Cost></LineItem>
</LineItems>
</Sale>
This is stored in an XML datatype column "Contract" in the "Sale" table.

The xquery statement is:
select contract.query('for $var in data(/Sale/LineItems/LineItem/Item) return string($var)') as Item
    from Sale
    where EmployeeID = 2

I appreciate any advice you can give me.




If it helps - the entire script is:
CREATE TABLE Employee
      (
           EmployeeID int identity(1,1) NOT NULL primary key,
           EmployeeName varchar(50) NOT NULL
      )
GO

CREATE TABLE Sale
    (
         SaleID int identity(1,1) not null primary key,
         EmployeeID int not null,
         StoreId int not null,
         Contract xml not null
     )
go
ALTER TABLE Sale ADD CONSTRAINT
      FK_Sale_Employee FOREIGN KEY
      (
          EmployeeID
      )
    REFERENCES Employee
      (
          EmployeeID
      )
GO


insert into Employee (EmployeeName)
   values('John')
go
insert into Employee (EmployeeName)
   values('Rich')
go

insert into Sale (EmployeeID, StoreID, Contract)
   values (1, 2, '<Sale><EmployeeID>1</EmployeeID><LineItems><LineItem><Item>Boat</Item><Cost>4268.65</Cost></LineItem><LineItem><Item>Space Shuttle</Item><Cost>4523745341.65</Cost></LineItem></LineItems></Sale>')
insert into Sale (EmployeeID, StoreID, Contract)
   values (1, 2, '<Sale><EmployeeID>1</EmployeeID><LineItems><LineItem><Item>Plain</Item><Cost>44268.65</Cost></LineItem><LineItem><Item>Train</Item><Cost>745341.65</Cost></LineItem></LineItems></Sale>')
insert into Sale (EmployeeID, StoreID, Contract)
   values (2, 2, '<Sale><EmployeeID>2</EmployeeID><LineItems><LineItem><Item>Tricycle</Item><Cost>28.65</Cost></LineItem><LineItem><Item>Wicker Basket</Item><Cost>1.65</Cost></LineItem></LineItems></Sale>')

select contract.query('for $var in data(/Sale/LineItems/LineItem/Item) return string($var)') as Item
    from Sale
    where EmployeeID = 2


drop table Sale
go
drop table Employee
go



SQl Server 2000 solution:  http://www.experts-exchange.com/Databases/Microsoft_SQL_Server/Q_20177696.html?query=xml+result+set&topics=42
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2012
Commented:
If the query method returns an untyped XML and you want a resultset, should you not be using OPENXML?

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.