create table #emp
(
id int,
name varchar(10)
);
insert into #emp values(1,'A'),(2,'B');
create table #prd
(
prid int,
price int
);
insert into #prd values(1,299),(2,295);
Expected Output: I want to display result as shown below.
<SampleData>
<EmpData id="1" name="A" />
<EmpData id="2" name="B" />
<PrdData prid="1" price="299" />
<PrdData prid="2" price="295" />
</SampleData>
Query:
SELECT
(
SELECT
(
SELECT id,
name
FROM #emp
FOR XML RAW('EmpData'),TYPE
),
(
SELECT prid,
price
FROM #prd
FOR XML RAW('PrdData'),TYPE
)
FOR XML PATH('SampleData'),TYPE
);
Output:
<SampleData>
<_x0078_ml>
<EmpData id="1" name="A" />
<EmpData id="2" name="B" />
</_x0078_ml>
<_x0078_ml>
<PrdData prid="1" price="299" />
<PrdData prid="2" price="295" />
</_x0078_ml>
</SampleData>
Note: The query works fine in SQL Server but not getting extra XML element _x0078_ml in Babelfish. I need this query to be compatible for both (SQL Server and Babelfish).
Use
FOR XML PATHinstead, and specify the attribute names explicitly.Alternatively use
FOR XML AUTO, although this is more difficult to control the exact shape of the XML.db<>fiddle