Solved

sql server CROSS APPLY and XML

Posted on 2011-03-07
4
885 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
  • 2
4 Comments
 
LVL 40

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now