简体   繁体   中英

Creating a temp table by using xml.nodes function in sql server

I have the xml structure below. I am trying to create a temp table by using return result but I couldnt do it.

declare @xml xml
set @xml = '
<Root>
  <ValueHolder>
     <Value>3.00</Value>
     <IsNoteDirty>false</IsNoteDirty>
     <Timestamp>
        <StampType>Month</StampType>
        <Stamp>3</Stamp>
        <Year>2007</Year>
     </Timestamp>
     </ValueHolder>
  <ValueHolder>
     <Value>23.00</Value>
     <IsNoteDirty>false</IsNoteDirty>
     <Timestamp>
        <StampType>Month</StampType>
        <Stamp>3</Stamp>
        <Year>2007</Year>
     </Timestamp>
     </ValueHolder>
</Root>'


select Tab.Col.value('(Value)[1]','MONEY') 
          from @xml.nodes('/Root/ValueHolder') Tab(Col)

This code works perfect. What I am trying to do is, putting this result to a temp table. I tried this one but it doesnt work

select * into #TempTable
    from (select Tab.Col.value('(Value)[1]','MONEY') 
          from @xml.nodes('/Root/ValueHolder') Tab(Col))

The error message is

Msg 102, Level 15, State 1, Line 26 Incorrect syntax near ')'.

Try something like this instead:

select MoneyValue = Tab.Col.value('(Value)[1]', 'MONEY') 
into #TempTable
from @xml.nodes('/Root/ValueHolder') Tab(Col) 

Works just fine if I do it this way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM