Note that there are some explanatory texts on larger screens.

plurals
  1. POTSQL FOR XML EXPLICIT - add attribute to Element
    primarykey
    data
    text
    <p>I have a SQL query like this using FOR XML EXPLICIT:</p> <pre><code>declare @address table ( AddressID int, AddressType varchar(12), Address1 varchar(20), Address2 varchar(20), City varchar(25) ) insert into @address select 1, 'Home', 'abc', 'xyz road', 'RJ' union all select 2, 'Office', 'temp', 'ppp road', 'RJ' union all select 3, 'Temp', 'eee', 'olkiu road', 'CL' SELECT 1 AS Tag,NULL AS Parent, 0 AS 'AddressCollection!1!Sort!hide', NULL AS 'AddressCollection!1!', NULL AS 'Address!2!AddressID', NULL AS 'Address!2!AddressType!Element', NULL AS 'Address!2!Address1!Element', NULL AS 'Address!2!Address2!Element', NULL AS 'Address!2!City!Element' UNION ALL SELECT 2 AS Tag,1 AS Parent,AddressID * 100,NULL, AddressID,AddressType, Address1, Address2, City FROM @address ORDER BY [AddressCollection!1!Sort!hide] FOR XML EXPLICIT </code></pre> <p>And the XML result is:</p> <pre><code>&lt;AddressCollection&gt; &lt;Address AddressID="1"&gt; &lt;AddressType&gt;Home&lt;/AddressType&gt; &lt;Address1&gt;abc&lt;/Address1&gt; &lt;Address2&gt;xyz road&lt;/Address2&gt; &lt;City&gt;RJ&lt;/City&gt; &lt;/Address&gt; &lt;Address AddressID="2"&gt; &lt;AddressType&gt;Office&lt;/AddressType&gt; &lt;Address1&gt;temp&lt;/Address1&gt; &lt;Address2&gt;ppp road&lt;/Address2&gt; &lt;City&gt;RJ&lt;/City&gt; &lt;/Address&gt; &lt;Address AddressID="3"&gt; &lt;AddressType&gt;Temp&lt;/AddressType&gt; &lt;Address1&gt;eee&lt;/Address1&gt; &lt;Address2&gt;olkiu road&lt;/Address2&gt; &lt;City&gt;CL&lt;/City&gt; &lt;/Address&gt; &lt;/AddressCollection&gt; </code></pre> <p>How can I add attributes to Element? I've correctly add them on parent tag but I don't know how can add them to element tag.</p> <p>I need something like this:</p> <pre><code>&lt;AddressCollection&gt; &lt;Address AddressID="1"&gt; &lt;AddressType&gt;Home&lt;/AddressType&gt; &lt;Address1&gt;abc&lt;/Address1&gt; &lt;Address2&gt;xyz road&lt;/Address2&gt; &lt;City status="modified"&gt;RJ&lt;/City&gt; &lt;/Address&gt; &lt;Address AddressID="2"&gt; &lt;AddressType status="modified"&gt;Office&lt;/AddressType&gt; &lt;Address1&gt;temp&lt;/Address1&gt; &lt;Address2&gt;ppp road&lt;/Address2&gt; &lt;City&gt;RJ&lt;/City&gt; &lt;/Address&gt; &lt;Address AddressID="3"&gt; &lt;AddressType&gt;Temp&lt;/AddressType&gt; &lt;Address1 status="modified"&gt;eee&lt;/Address1&gt; &lt;Address2&gt;olkiu road&lt;/Address2&gt; &lt;City&gt;CL&lt;/City&gt; &lt;/Address&gt; &lt;/AddressCollection&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload