Skip to main content

FOR XML

In a FOR XML clause, you specify one of these modes:

RAW - generates a single <row> element per row in the rowset that is returned by the SELECT statement
AUTO - generates nesting in the resulting XML by using heuristics based on the way the SELECT statement is specified
EXPLICIT - provides the most flexibility in generating the XML
PATH - a simpler alternative to writing EXPLICIT mode queries

Examples for RAW:  

Query 1:  Basic
                      SELECT doc_id ,
                                     media_id
                      FROM DOC
                      FOR XML RAW
Result 1:
                      <row doc_id="114" media_id="-117" />
                      <row doc_id="115" media_id="-113" />

Query 2:  Using Customized name instead of "row" in the result
                      SELECT doc_id ,
                                     media_id
                      FROM DOC
                      FOR XML RAW('DOC')
Result 2:
                      <DOC doc_id="114" media_id="-117" />
                      <DOC doc_id="115" media_id="-113" />



Examples for AUTO:

Query 1:  Basic
                      SELECT doc_id ,
                                     media_id
                      FROM DOC
                      FOR XML AUTO
Result 1:
                      <DOC doc_id="114" media_id="-117" />
                      <DOC doc_id="115" media_id="-113" />

Note : If we specifies a four-part name in the query, the server name is not returned in the resulting XML document when the query is executed on the local computer. However, the server name is returned as the four-part name when the query is executed on a network server.

Query 2: Four Part Name
                      SELECT doc_id ,
                                     media_id
                      FROM Server.Database.Owner.Table
                      FOR XML AUTO
Result 2:
                      <Server.Database.Owner.Table doc_id="114" media_id="-117" />
                      <Server.Database.Owner.Table doc_id="115" media_id="-113" />

Query 3: Using Alias
                      SELECT doc_id ,
                                     media_id
                      FROM Server.Database.Owner.Table DOC
                      FOR XML AUTO
Result 3:
                      <DOC doc_id="114" media_id="-117" />
                      <DOC doc_id="115" media_id="-113" />

Examples for EXPLICIT:

Query 1:  Basic
                      SELECT TOP 1                      
                              1 AS Tag ,                                        --- statements to be used as is
                              NULL AS Parent ,                            --- statements to be used as is
                              E.Doc_Id AS [Layer1!1!Emp] ,                             --- outer column
                              E.Custodian_Id AS [Layer2!2!Layer3!ELEMENT]  --- inner column
                      FROM    dbo.Doc AS E
                      UNION ALL
                      SELECT TOP 1
                              2 AS Tag ,                                          --- statements to be used as is
                              1 AS Parent ,                                      --- statements to be used as is
                              E.Doc_Id ,                                                            --- outer column
                              E.Custodian_Id                                                        --- inner column
                      FROM    dbo.Doc AS E
                      ORDER BY [Layer1!1!Emp] ,[Layer2!2!Layer3!ELEMENT] -- order by is very important to get a nested loop
                      FOR     XML EXPLICIT ;

Result 1: 
                      <Layer1 Emp="1569">
                        <Layer2>
                          <Layer3>0</Layer3>
                        </Layer2>
                      </Layer1>

Examples for PATH:

Query 1:  Basic
                       SELECT  doc_id ,
                              media_id
                      FROM    dbo.Doc
                      FOR     XML PATH('')     
 Result 1: 
                      <doc_id>11491190</doc_id>
                      <media_id>-11737</media_id>

Query 2:  Using Alias
                      SELECT  doc_id ,
                              media_id
                      FROM    dbo.Doc
                      FOR     XML PATH('CustomerInfo') ;
Result 2: 
                      <CustomerInfo>
                        <doc_id>11491190</edoc_id>
                        <media_id>-11737</media_id>
                      </CustomerInfo>

Comments

Popular posts from this blog

Insufficient access rights to perform the operation. (Exception from HRESULT: 0x80072098)

While accessing the active directory (AD) and authorization manager (AZMAN) , If you get “   Insufficient access rights to perform the operation. (Exception from HRESULT: 0x80072098)  “ message check the    account that is being used to get the LDAP query from AD .  ERROR DETAILS Exception Details:  System.Runtime.InteropServices.COMException: Insufficient access rights to perform the operation. (Exception from HRESULT: 0x80072098) Source Error: Line 154:    'Session("FullName") = System.Security.Principal.WindowsIdentity.GetCurrent.Name.ToString() Line 155: Line 156:    If Not User.IsInRole("Role1") Then Line 157:          Response.Redirect("./Login.aspx") Line 158:    End If  Stack Trace : .... SOLVE IT Steps to do check the app pool rights: Click on the website name that you are having problem with in IIS  In the right panel you will se...

Do's and Don't SQL

Do's: Writing comments whenever something is not very obvious, as it won’t impact the performance.  (--) for single line  (/*…*/) to mark a section Use proper indentation Use Upper Case for all SQL keywords. SELECT, UPDATE, INSERT, WHERE, INNER JOIN, AND, OR, LIKE. Use BEGIN... END block for multiple statements in conditional code  Use Declare and Set in beginning of Stored procedure Create objects in same database where its relevant table exists otherwise it will reduce network performance. Use PRIMARY key in WHERE condition of UPDATE or DELETE statements as this will avoid error possibilities. If User table references Employee table than the column name used in reference should be UserID where User is table name and ID primary column of User table and UserID is reference column of Employee table. Use select column name instead of select * Use CTE (Common Table Expression); its scope is limited to the next statement in SQL query, instead of...

SQL Server 2008 - Inline Variable Assignment

Microsoft SQL Server 2008 brings in a new feature of 'Declaring and Assigning' a variable all in a single line. Earlier, while using SQL 2005 and earlier versions, we had to declare a variable before assigning it. This feature is explained in detail below. In SQL 2005 and earlier versions we had to write: Declare @age int Declare @name nvarchar(25) Declare @date date Set @age = 25 Set @name = "Garry" Set @date = GETDATE() With SQL 2008 we can combine both these statements to write Declare @age int = 25 Declare @name nvarchar(25) Declare @date @date = GETDATE() Advantage : This helps us to write less code, yet get the same functionality. Even though this feature has been available it is not being used in many projects because of backward compatibility issue. If the same code is being used on both 2005 and 2008 server we would prefer writing in a format compatible to both. Do share with us :  Would you like to use this new method? If not, why?