Answer simple quiz at the end of the blog post and -
Every day one winner from India will get Joes 2 Pros Volume 5.
Every day one winner from United States will get Joes 2 Pros Volume 5.
Using Root With Auto XML Mode
Now let’s add a root element (also called root node), so that our stream will be well-formed XML. Using the ROOT keyword in combination with the Auto mode produces the same result as it does with the Raw mode: your XML stream will contain a root (named <root> by default). To specify a name for the root, put this name in the parentheses inside single quotes right after the ROOT keyword in your code.

Our XML result appears the same as in the prior result, except that now all of our data is enclosed within the opening and closing tags named <root>.

Let’s take our same code and give the root node a more meaningful name. To specify a name for the root, put the name inside single quotes, within parentheses, right after the ROOT keyword in your code. We’ll name it <JproCo> after the database which both of our tables come from.


Sorting Nested Elements
XML offers a great deal of flexibility in how you can choose to organize your result output. For example, since eight JProCo employees are listed under Seattle, then there is really no need to see Seattle listed multiple times. By sorting on the higher level element, you can put all related child elements under the same parent. This way you don’t have to repeatedly list the parent element. Let’s combine our results together in a more efficient and readable manner by grouping the Seattle employees together. The figure below shows Seattle employees are scatterd and not clusterd in our XML stream.

Instead of grouping the results by the natural sort of the table (i.e., by EmployeeID), we would prefer to have the the results grouped by city. An ORDER BY clause will help achieve our goal.

This result is very readable. We now see each city (Boston, Seattle, and Spokane) listed once along with the employees working in each one.

Each of the JProCo employees from Boston is listed as a child element below the top level element <Location> which has a City attribute with a value of “Boston”. We similarly see the Seattle employees nested beneath the Location with a City attribute of “Seattle”.
Note: If you want to setup the sample JProCo database on your system you can watch this video. For this post you will want to run the SQLInteropChapter1.0Setup.sql script from Volume 5.
Question 31
You have a query which joins tables. You want to create a well-formed XML stream, which is attribute-based and nests the results in the table from the first field of the select list. Which code do you append to the SQL statement?
- FOR XML AUTO
- FOR XML RAW
- FOR XML AUTO, ROOT
- FOR XML RAW, ROOT
Rules:
Please leave your answer in comment section below with correct option, explanation and your country of resident.
Every day one winner will be announced from United States.
Every day one winner will be announced from India.
A valid answer must contain country of residence of answerer.
Please check my facebook page for winners name and correct answer.
Every day one winner from India will get Joes 2 Pros Volume 5.
Every day one winner from United States will get Joes 2 Pros Volume 5.
The contest is open till next blog post shows up at http://blog.sqlauthority.com which is next day GTM+2.5.
Reference: Pinal Dave (http://blog.SQLAuthority.com)
Hi,
Option 3 FOR XML AUTO, ROOT is correct as this allows our stream to be in well-formed XML.
Thanks
Sudhir Chawla
New Delhi, India
Correct answer is option #3
3.FOR XML AUTO, ROOT
Thanks.
Country – India
Option 3 and 4 are th corre`ct answer. Using the ROOT keyword in combination with the Auto mode produces the same result as it does with the Raw mode:
Uday Bhoopalam
USA
Correct Answer Option : 3
3.FOR XML AUTO, ROOT
Option 1 : FOR XML AUTO – It arrange the tags withour Root Node
Option 2 : FOR XML RAW – It simples returns values with Row Tags
Option 3 : FOR XML AUTO ROOT – it returns all records with proper Root node
Option 4 : FOR XML RAW ROOT – It returns records in Row tag wirh Root node
Chennai, Tamilnadu, India
Correct Answer is # 3 & 4.
3.FOR XML AUTO, ROOT
4.FOR XML RAW, ROOT
Because using the ROOT keyword with RAW mode produces the same result as with AUTO mode.
Using root element (also called root node), our stream will be well-formed XML.
Using the ROOT keyword in combination with the Auto mode produces the same result as it does with the Raw mode.
Gopalakrishnan Arthanarisamy
Unisys, Bangalore, India.
Correct answer is No 3:
FOR XML AUTO, ROOT
This is because using the ROOT keyword in combination with the Auto mode produces a well-formed XML
Rene Castro
El Salvador
I believe that the correct answer is #3
FOR XML AUTO, ROOT
Also, this article demonstrated nice nested output especially when the result was ordered.
I am from USA
HI,
Correct answer is option #3
3.FOR XML AUTO, ROOT
I am from India.
Question 31
Ans : #3—-FOR XML AUTO, ROOT
Chennai, INDIA
3) is the correct answer
This allows the stream to be in well-formed XML.
Leo Pius
USA
Correct Answer :3)FOR XML AUTO, ROOT
krishan kumar mishra
India
The correct answer is Option 3.
FOR XML AUTO, ROOT
Pranav
Hyderabad
Answer is Option# :: 3
ANS is 3.FOR XML AUTO, ROOT
FOR XML AUTO – It arrange the tags without root node its not an well formed xml.
FOR XML RAW – It simples returns values with Row Tags and it has no root node too.
FOR XML AUTO, ROOT – it returns all records with proper root node.Its an well formed xml.
FOR XML RAW, ROOT – It returns records in row tag with root node.
Partha,
India
Correct Option is 3, i.e.
3. FOR XML AUTO, ROOT
Reason:
The above option offers organizes the result output in a more efficient and readable manner by grouping the result from a table’s first field in a select statement.
Regards,
Geetika Bhambri
Ahmedabad(Gujarat)-INDIA
Correct Answer is:
3)FOR XML AUTO, ROOT
City: Baroda
Country: INDIA
Hi,
Option 3 and 4 are close to Answer but as in RAW user in option 4 it says “RAW mode generates single elements, which are named row, for each row returned.”(From MSDN). And if i go in details as describe here ” our stream will be well-formed XML. Using the ROOT keyword in combination with the Auto mode produces the same result as it does with the Raw mode”
So Option 3 is the correct Answer “FOR XML AUTO, ROOT”
Thanks….
Rajneesh Verma
(INDIA)
Option 3 is correct Answer.
Description:
When we use AUTO, it will include an attribute and a node name for child elements just after the root node. But when we include RAW then all child elements named as ROW.
Vinay Kumar
Jaipur, India
Correct answer should be option #3.
3. FOR XML AUTO, ROOT
New Delhi
India
option 1 & 3.
if we dont want to form it in root then we can use 1 and if we want it to be formed in root then we can use 3.
india
ghanshyam
Correct Answer is Option 3
Explanation :
suppose we have two tables tblorganization,tblemployee and for each organization there could be number of employees now if we apply the following join in these two tables
select org.orgname,emp.employeename,emp.employeeadress from employee as emp inner join organization as org on emp.employeeid=org.employeeid
for xml auto,root(‘organizations’)
this query will prompt us to a well organized xml where for each orgganization there will be employee names and addresses with root as organizations.
Thanks
Vishal Patwarhdhan
Indore(India)
the correct option is option 3.i.e.
Option 3 FOR XML AUTO, ROOT
as this allows our stream to be in well-formed XML.
India
The correct answer is Option 3.
3. FOR XML AUTO, ROOT
Reason: it returns all records with proper root node.Its an well formed xml.
Hiren Bavishi
India
The answer is :
3.FOR XML AUTO, ROOT
Sudeepta,
India.
The correct answer is #3 – FOR XML AUTO, ROOT
Country of residence: India
Correct answer is option 3
Thanks
Prasad
India
HI,
Correct answer #3
3.FOR XML AUTO, ROOT
Somnath Desai
India
Answer option 3
FOR XML AUTO, ROOT – it returns all records with proper Root node
Nikhildas
Cochin
INDIA
Ans : 3
(3) FOR XML AUTO, ROOT
Explanation
==========
it create a well-formed XML stream, which is attribute-based and nests the results in the table from the first field of the select list.
Mitesh Modi
(India)
Option # 3
Sreeram
India
Option 3. FOR XML AUTO, ROOT is the right code to append to the SQL statement to create a well-formed XML stream, which is attribute-based and nests the results in the table from the first field of the select list.
(Sale, Nigeria)
Pingback: SQL SERVER – Win a Book a Day – Contest Rules – Day 0 of 35 Journey to SQLAuthority
Option 3 will result in the desired well-formed XML stream.
Country: United States
Option #3
USA
Mike Michalicek
Correct answer: 3
3.FOR XML AUTO, ROOT
Country: India
Answer is:
3.FOR XML AUTO, ROOT
Vinay
Pune, India.
The correct and obvious answer is option no #3)
FOR XML AUTO, ROOT
because
Every row will be populated in the same name as the first element and in well formed xml format manner as nicely explained.
Diljeet Kumari
Country : INDIA
Correct Answer – 3. FOR XML AUTO, ROOT
This will creat a well formed XML stream with the attributes nested and enclosed within opening and closing tags.
Ron A. Farris – USA
Anwer is #3
3. FOR XML AUTO, ROOT
all records will be returned under a proper root node and is well formed xml.
USA
Hi Sir,
Option 3 “FOR XML AUTO, ROOT”
is the correct answer as this will create a well formed XML stream
and the output is attribute-based and nests the results in a table
from the first field of the select list.
Regards,
P.Anish Shenoy,
INDIA, Bangalore, Karnataka
Correct answer is option #3
3.FOR XML AUTO, ROOT
Thanks.
Country – USA
3.FOR XML AUTO, ROOT
Gordon Kane
Allen TX
USA
The correct option is option #3
FOR XML AUTO, ROOT
Well formed XML contains a single root, so adding the ROOT clause after FOR XML AUTO satisfies this condition. Option 1 and 2 don’t have the ROOT statement so they are incorrect. RAW mode transforms each row in the query result set into an XML element that has the generic identifier so option 4 is incorrect. Option 3 gives us what we need.
Country of Residence: USA
Option 3 is correct
Robin Thomas
India
Option 3
Kevin Scharnhorst
United States
Correct option is 3) FOR XML AUTO, ROOT
Because this is going create a well-formed XML stream, which is attribute-based and nests the results in the table from the first field of the select list.
Dilip Kumar Jena
Country : India
Options 3 and 4 are correct. When we refer to the yesterdays post regarding XML it conveys that option 4: “FOR XML RAW, ROOT” creates a well formed XML. This tutorial explains that “OPTION 3.FOR XML AUTO, ROOT” is used to create a well formed xml.
Vaishali Jain
Hyderabad, India
The correct answer is option 3.
Ramdas
USA
Option 1 & 3
Explanation :
FOR XML RAW produces what we call attribute-based XML. FOR XML RAW essentially creates a flat representation of the data where each row returned becomes an element and the returned columns become the attributes of each element. FOR XML RAW also doesn’t interpret joins in any special way.
FOR XML AUTO produces attribute-based XML by default and can create nested results based on the tables in the query’s join clause.
So, using FOR XML AUTO and FOR XML AUTO ROOT, we can fulfill our requirement for creating a well formed, attribute based XML.
-Richa
Country – USA
Correct Answer is: Option 3
Thanks,
Basavaraj
Correct Answer is: Option 3
Thanks,
Basavaraj
India
Hi Pinal,
Challenge:
Question 31
You have a query which joins tables. You want to create a well-formed XML stream, which is attribute-based and nests the results in the table from the first field of the select list. Which code do you append to the SQL statement?
1.FOR XML AUTO
2.FOR XML RAW
3.FOR XML AUTO, ROOT
4.FOR XML RAW, ROOT
Correct Answer:
The correct choice is #3: FOR XML AUTO, ROOT
Explanation:
Adding a root element or root node in combination with the auto mode with create a well-formed XML stream.
Country:
United States
Thanks for the knowledge!
Regards,
Bill Pepping
Hi sir The answer is option 3: FOR XML AUTO
Thanks
Shekhar Bahuguna
Delhi India
Ans : 3
3. FOR XML AUTO, ROOT
–India
Answer : option (3) FOR XML AUTO, ROOT
country : India
Thanks
SachinKulshreshtha
Correct Answer is Option : 3 as
3.FOR XML AUTO, ROOT
because it returns all records with proper Root node and it is well-formed XML stream
Correct Answer is Option : 3 as
3.FOR XML AUTO, ROOT
because it returns all records with proper Root node and it is well-formed XML stream
Varinder Sandhu (India)
Pinal Dave,
The answer is option 3.
David
USA
3) 3rd option is correct
JD Varu
Ahmedabad – India
Correct Ans is: 3. FOR XML AUTO, ROOT
because The AUTO mode employs a heuristic to infer a simple, one element name-per-level hierarchy based on the lineage information and the order of the data in a SELECT statement, and ROOT will create root element for that xml
–
Haresh Ambaliya (INDIA)
Option 3
3.FOR XML AUTO, ROOT
Country : India
Correct Answer:Option 3
3.FOR XML AUTO, ROOT
Regards
Rajesh
From india
A.) 3.FOR XML AUTO, ROOT
Winner from USA: Ron A. Farris
Winner from India: Vinay Kumar
I thank you all for participating here. The permanent record of this update is posted on facebook page.
Correct Answer:Option 3
3.FOR XML AUTO, ROOT
Regards
Azhar Iqbal
From Pakistan
Correct Answer : Option 3 . FOR XML AUTO, ROOT
Thanks,
Raghavndra
India
Pingback: SQL SERVER – A Guide to Integrating SQL Server with XML, C#, and PowerShell – Book Available for SQL Server Certification Journey to SQLAuthority
Pingback: SQL SERVER – Understanding XML – Contest Win Joes 2 Pros Combo (USD 198) – Day 5 of 5 « SQL Server Journey with SQL Authority