I know how to do this in a simple scenario, for example
DECLARE @commaSeparatedValues NVARCHAR(MAX) DECLARE @xml XML = N'<id>1</id> <name>test</name> <istest>1</istest>' ;WITH nodes AS ( SELECT Tbl.Col.value('.', 'nvarchar(max)') as Value FROM @xml.nodes('*/text()') Tbl(Col) ), prepareStrings AS ( SELECT IIF(ISNUMERIC(n.value) = 1, n.Value, '''' + n.Value + '''') AS Value FROM nodes n ) SELECT @commaSeparatedValues = CASE WHEN @commaSeparatedValues IS NULL THEN s.Value ELSE @commaSeparatedValues + ',' + s.value END FROM prepareStrings s SELECT @commaSeparatedValues as csv
It works great. The problem occurs when I want to parse this way the following XML data. I am having trouble writing the correct query.
DECLARE @xml XML = N' <e> <id>1</id> <name>test</name> <istest>1</istest> </e> <e> <id>2</id> <name>test2</name> <istest>0</istest> </e> '
I can get items line by line using
select Tbl.col.query('.') as [xml] from @xml.nodes('e') Tbl(col)
I do not know how to move on. I don’t know how to use this query and now request the [xml] column.
xml sql-server tsql xquery
lszk
source share