1

here is my xml which i want to show as tabular form

DECLARE @xml XML 
SET @xml = '<root>
  <row>one</row>
  <row>two</row>
  <row>three</row>
</root>'

SELECT 
    x.y.value('(row/text())[1]', 'varchar(5)')
FROM @xml.nodes('root') x(y)

it is displaying only first rows. i want to display data like

Mycolumn
-----------
one
two
three

please guide me what i need to change in my sql. thanks

i could not issue select into with xquery like the below way

DECLARE @xml XML 
SET @xml = '<root>
  <row>one</row>
  <row>two</row>
  <row>three</row>
</root>'

SELECT *
INTO #Fields
FROM (SELECT 
    x.y.value('text()[1]', 'varchar(5)') as fields11
FROM @xml.nodes('root/row') x(y))

SELECT * FROM #Fields
DROP TABLE #Fields

but i could use insert into with xuqery like below way

DECLARE @xml XML 
SET @xml = '<root>
  <row>one</row>
  <row>two</row>
  <row>three</row>
</root>'

CREATE TABLE #Fields(Field varchar(MAX))

INSERT INTO #Fields
SELECT 
    x.y.value('text()[1]', 'varchar(5)') 
FROM @xml.nodes('root/row') x(y)

SELECT * FROM #Fields
DROP TABLE #Fields

so anyone can help me to construct select into with xquery. thanks

NOW Select into works with XQUERY

DECLARE @xml XML 
SET @xml = '<root>
  <row>one</row>
  <row>two</row>
  <row>three</row>
</root>'

SELECT  x.y.value('text()[1]', 'varchar(5)') Mycolumn into #Fields
FROM    @xml.nodes('root/row') x(y)

select * from #Fields
drop table #Fields

1 Answer 1

2

This should work:

SELECT  x.y.value('text()[1]', 'varchar(5)') Mycolumn
FROM    @xml.nodes('root/row') x(y)

Path in nodes method from which the rows are generated should be root/row, this way three rows will be generated and for each of the values in rows expression text()[1] should extract it's value.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks i have updated my question can u plzz have a look. thanks again for answer.
You're welcome, I'm glad it helped. For SELECT...INTO...FROM you just have to add table alias. Last line of the failing query should be for instance: FROM @xml.nodes('root/row') x(y)) tbl where tbl is alias.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.