Here is something to get you started. It does not pick up all the columns, and it is probably not the shortest; it is simple and mechanical rather than clever and short.
INSERT INTO dbo.Test VALUES
(1, 21, 22.7, 24, 257, 3,7,9,1),
(2, 31, 32.7, 44, 557, 6,9,11,61)
SELECT
'TEST' AS [table/@t],
(
SELECT
'A' AS [group/@g],
(
SELECT
Code AS [u/@val],
'A' AS [col/@c],
A AS [col/value]
FROM
dbo.Test t2
WHERE
t2.Code = t1.Code
FOR XML PATH(''),TYPE
) [group]
FROM
dbo.test t1
FOR XML PATH(''), TYPE
) AS [table]
FOR XML PATH('');
Thank you @JamesK for the answer, that's what I had in mind of doing but because I have a lot of tables (and many more groups) I was thinking if there is a more dynamic way of doing it.
What if I create a table like the following, any ideas if this can work somehow?
SELECT
'A' as [@g],
(SELECT
Code [@val],
(SELECT
name as [col/@c],
[name] as[col/value]
FROM sys.columns CROSS JOIN dbo.TEST
WHERE object_id = OBJECT_ID('dbo.TEST')
and (name='A' or name='B' or name='C' or name='D')
and Code=1
ORDER BY column_id
FOR XML PATH(''),TYPE)
FROM dbo.TEST
WHERE Code=1
FOR XML PATH('u'),TYPE
)
FROM dbo.TEST
WHERE Code=1
FOR XML PATH('group'),TYPE
I get the format I want but instead of the value of the column in I get the name. Any ideas how to get the value?
Thank you.
Instead of using name in the line that reads " [name] as[col/value]", use the actual column name, for example like this:
....
SELECT
name AS [col/@c] ,
CASE name
WHEN 'A' THEN A
WHEN 'B' THEN B
WHEN 'C' THEN C
WHEN 'D' THEN D
END AS [col/value]
FROM
sys.columns
CROSS JOIN dbo.TEST
....
SELECT
'TEST' AS [table/@t],
(
SELECT
GroupName as [@g],
(SELECT
Code [@val],
(SELECT
ColumnName as [col/@c],
CASE name
WHEN 'A' THEN A
WHEN 'B' THEN B
WHEN 'C' THEN C
WHEN 'D' THEN D
WHEN 'E' THEN E
WHEN 'F' THEN F
WHEN 'G' THEN G
WHEN 'H' THEN H
END AS [col/value]
FROM sys.columns
CROSS JOIN TEST c1
INNER JOIN COLUMN_GROUP r1
ON sys.columns.name=r1.columnname
WHERE object_id = OBJECT_ID('TEST')
and r1.GroupName=r2.GroupName and c1.Id=c2.Id
ORDER BY column_id
FOR XML PATH(''),TYPE)
FROM TEST c2
FOR XML PATH('u'),TYPE
)
FROM sys.columns
CROSS JOIN TEST c3
INNER JOIN COLUMN_GROUP r2
ON sys.columns.name=r2.columnname
WHERE object_id = OBJECT_ID('TEST')
GROUP BY GroupName
FOR XML PATH('group'),TYPE
)AS [table]
FOR XML PATH('')