SQL SERVER FOR XML CASE RESULTS
In this post, i just want to show how to use FOR XML in SQL Queries and output results. The reason because i always bit confused with the result like for which type which output so i thought to simplify the concept to keep remember.
Here, i used two tables Customers & Orders. Table properties are shown below,
Query Used, Replace the highlighted portion for other FOR XML queries, RAW, AUTO, PATH.
SELECT OrderID, Name, Country, Amount, OrderDate
FROM dbo.customers c INNER JOIN dbo.orders o
ON C.CustomerID=o.CustomerID
WHERE c.customerId =101
FOR XML RAW
Query & Result
FOR XML RAW
|
<row OrderID="1" Name="Ravi" Country="India" Amount="250000"OrderDate="2014-06-25T00:00:00" />
|
FOR XML RAW, ELEMENTS
|
<row>
<OrderID>1OrderID>
<Name>RaviName>
<Country>IndiaCountry>
<Amount>250000Amount>
<OrderDate>2014-06-25T00:00:00OrderDate>
row>
|
FOR XML AUTO
|
<o OrderID="1" Amount="250000" OrderDate="2014-06-25T00:00:00">
<c Name="Ravi" Country="India" />
o>
|
FOR XML AUTO, ELEMENTS
|
<o>
<OrderID>1OrderID>
<Amount>250000Amount>
<OrderDate>2014-06-25T00:00:00OrderDate>
<c>
<Name>RaviName>
<Country>IndiaCountry>
c>
o>
|
FOR XML PATH
&
FOR XML PATH, ELEMENTS
|
<row>
<OrderID>1OrderID>
<Name>RaviName>
<Country>IndiaCountry>
<Amount>250000Amount>
<OrderDate>2014-06-25T00:00:00OrderDate>
row>
|
FOR XML PATH ('customers')
|
<customers>
<OrderID>1OrderID>
<Name>RaviName>
<Country>IndiaCountry>
<Amount>250000Amount>
<OrderDate>2014-06-25T00:00:00OrderDate>
customers>
|
Comments
Post a Comment