FOR XML Clause

UmaShankar-Patel, 2012-12-26

FOR XML CLAUSE:  Sometimes we need to data in form of XML from

Database.SQL Server provide FOR XML Clause. For XML clause returns result of

query as XML.There are some options with FOR XML clause

  • Raw
  • Path
·        

Auto

  • Elements(can be used with raw,auto,path)
  • Explicit
You will

find all differences from below query output

CREATE TABLE

[dbo].[emp](

      [emp_id] [nchar](10) NULL,

      [emp_name] [nchar](10) NULL

) ON

[PRIMARY]

insert into

emp (emp_id,emp_name) values

(‘1’,‘ABC’),

(‘2’,‘XYZ’),

(‘3’,‘PQR’)

  1. select *from dbo.emp for xml raw

<row emp_id=1        

emp_name=ABC       />

<row emp_id=2        

emp_name=XYZ       />

<row emp_id=3        

emp_name=PQR       />

  1. select *from dbo.emp for xml raw,elements

<row>

  <emp_id>1         </emp_id>

  <emp_name>ABC       </emp_name>

</row>

<row>

  <emp_id>2         </emp_id>

  <emp_name>XYZ       </emp_name>

</row>

<row>

  <emp_id>3         </emp_id>

  <emp_name>PQR       </emp_name>

</row>
  1. select *from dbo.emp for xml auto

<emp emp_id=1        

emp_name=ABC       />

<emp emp_id=2        

emp_name=XYZ       />

<emp emp_id=3        

emp_name=PQR       />

  1. select *from dbo.emp for xml auto,elements

<emp>

  <emp_id>1         </emp_id>

  <emp_name>ABC       </emp_name>

</emp>

<emp>

  <emp_id>2         </emp_id>

  <emp_name>XYZ       </emp_name>

</emp>

<emp>

  <emp_id>3         </emp_id>

  <emp_name>PQR       </emp_name>

</emp>

  1. select *from emp for xml auto

<emp emp_id=1          emp_name=ABC      

/>

<emp emp_id=2        

emp_name=XYZ       />

<emp emp_id=3        

emp_name=PQR       />

  1. select *from emp for xml path(‘uma’)

<uma>

  <emp_id>1         </emp_id>

  <emp_name>ABC       </emp_name>

</uma>

<uma>

  <emp_id>2         </emp_id>

  <emp_name>XYZ       </emp_name>

</uma>

<uma>

  <emp_id>3         </emp_id>

  <emp_name>PQR       </emp_name>

</uma>

Rate

Share

Share

Rate

Related content

Database Mirroring FAQ: Can a 2008 SQL instance be used as the witness for a 2005 database mirroring setup?

Question: Can a 2008 SQL instance be used as the witness for a 2005 database mirroring setup? This question was sent to me via email. My reply follows. Can a 2008 SQL instance be used as the witness for a 2005 database mirroring setup? Databases to be mirrored are currently running on 2005 SQL instances but will be upgraded to 2008 SQL in the near future.

Robert Davis

2009-02-23

1,567 reads

Networking – Part 4

You may want to read Part 1 , Part 2 , and Part 3 before continuing. This time around I’d like to talk about social networking. We’ll start with social networking. Facebook, MySpace, and Twitter are all good examples of using technology to let…

Andy Warren

2009-02-17

1,530 reads

Speaking at Community Events – More Thoughts

Last week I posted Speaking at Community Events – Time to Raise the Bar?, a first cut at talking about to what degree we should require experience for speakers at events like SQLSaturday as well as when it might be appropriate to add additional focus/limitations on the presentations that are accepted. I’ve got a few more thoughts on the topic this week, and I look forward to your comments.

Andy Warren

2009-02-13

360 reads