How to return multiple tables as one XML?
This gets you the result you want, based on the data we have:
SELECT (SELECT Caption,
Width
FROM @columns
FOR XML PATH('Column'),TYPE) AS [Columns],
(SELECT Id,
[Name]
FROM @rows
FOR XML PATH('Row'),TYPE) AS [Rows]
FOR XML PATH ('Results');
This query generates exactly the result you're expected:
SELECT (SELECT clm.Caption,
clm.Width
FROM @columns clm
FOR XML PATH('Column'), TYPE) AS Columns,
(SELECT rs.Id,
rs.[Name]
FROM @rows rs
FOR XML PATH('Row'),TYPE) AS [Rows]
FOR XML PATH ('Results');