Solved

sql server CROSS APPLY and XML

Posted on 2011-03-07
4
883 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

13 Experts available now in Live!

Get 1:1 Help Now