?
Solved

sql server CROSS APPLY and XML

Posted on 2011-03-07
4
Medium Priority
?
908 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 2000 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

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

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

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
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…

777 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