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

x
?
Solved

sql server CROSS APPLY and XML

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

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