Curtis
Curtis

Reputation: 103358

SQL XML - Return result set

I have the following SQL Query:

declare @x xml
set @x = '<IDs><ID>1</ID><ID>2</ID></IDs>'

SELECT @x.query('/IDs/ID') as ID

This returns the following result:

ID
--------------------
<ID>1</ID><ID>2</ID>

How can I instead get this to return:

ID
--
1
2

Upvotes: 4

Views: 387

Answers (1)

Antoine Aubry
Antoine Aubry

Reputation: 12469

Use this code instead:

declare @x xml
set @x = '<IDs><ID>1</ID><ID>2</ID></IDs>'

SELECT ID.value('.', 'int') AS ID
FROM @x.nodes('/IDs/ID') as IDS(ID)

Upvotes: 6

Related Questions