Solved

sql server CROSS APPLY and XML

Posted on 2011-03-07
4
892 Views
Last Modified: 2012-05-11
Hi experts, in this query
DECLARE @Doc XML
SET @Doc = '
<store>
      <category id="1" name="Linea Blanca">
            <product id="1" name="Olla" />
            <product id="2" name="Licuadora" />
            <product id="3" name="Batidora" />
      </category>
      <category id="2" name="Ferreteria">
            <product id="4" name="Clavos" />
            <product id="5" name="Cemento" />
            <product id="6" name="Sierra" />
            <product id="7" name="Lija" />
      </category>
</store>
'
SELECT c.value('@id', 'SMALLINT') AS ProductId,
         c.value('@name', 'VARCHAR(15)') AS Name
FROM @Doc.nodes('/store/category/product') AS T(c)

you can use the CROSS APPLY??
 If so, what is it exactly?
an example, please
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
  • 2
4 Comments
 
LVL 41

Expert Comment

by:Sharath
ID: 35062914
I didn't get your question. What do you mean by CROSS APPLY here?
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 35063685
>>an example, please <<
Something like this perhaps:
DECLARE @MyTable TABLE(
			ID integer IDENTITY(1,1) NOT Null,
			MyDescription varchar(50) NOT NULL,
			Doc xml NOT Null
			)

INSERT	@MyTable(MyDescription, Doc)
VALUES  ('My description goes here',
		'<store>
			  <category id="1" name="Linea Blanca">
					<product id="1" name="Olla" />
					<product id="2" name="Licuadora" />
					<product id="3" name="Batidora" />
			  </category>
			  <category id="2" name="Ferreteria">
					<product id="4" name="Clavos" />
					<product id="5" name="Cemento" />
					<product id="6" name="Sierra" />
					<product id="7" name="Lija" />
			  </category>
		</store>')
		
SELECT  b.ID,
        b.MyDescription,
        c.value('@id', 'SMALLINT') AS ProductId,
        c.value('@name', 'VARCHAR(15)') AS Name
FROM    @MyTable b
        CROSS APPLY b.Doc.nodes('/store/category/product') AS T (c)

Open in new window

0
 

Author Comment

by:enrique_aeo
ID: 35072216
aceperkings ok! what is the advantage of using CROSS APPLY???
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 35077073
>>what is the advantage of using CROSS APPLY??? <<
One question mark (?) is quite sufficient, thank you.

Asking what is the advantage of using CROSS APPLY is like asking a carpenter what is the advantage of using a hammer.  It is a tool that can be appropriate in certain circumstances.  If the definition from SQL Server BOL and the example I posted is not suffient, than I do not know what else to tell you.
0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
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.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…

726 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