1

I am trying to update a SQL Server database using an XML file that I will be given. I am trying to use XQuery to insert part of each XML record, but am having difficulty getting the values of more than one node to the corresponding columns of the database. Here is the code that I am using:

USE GOCO
GO

DECLARE @x XML

SET @x = '<Table6>
<Flt_x0020_No>1</Flt_x0020_No>
<AvNo>2046</AvNo>
<Date>2010-05-19T00:00:00+04:30</Date>
<Takeoff_x0020_Time>14:15</Takeoff_x0020_Time>
<Land_x0020_Time>15:09</Land_x0020_Time>
<Total_x0020_Flt_x0020_Time>0.9</Total_x0020_Flt_x0020_Time>
</Table6>
<Table6>
<Flt_x0020_No>3460</Flt_x0020_No>
<AvNo>2489</AvNo>
<Date>2013-01-15T00:00:00+04:30</Date>
<Takeoff_x0020_Time>8:40</Takeoff_x0020_Time>
<Land_x0020_Time>13:05</Land_x0020_Time>
<Total_x0020_Flt_x0020_Time>4.5</Total_x0020_Flt_x0020_Time>
</Table6>'


insert into Flight(
FltNo,
AvNo
)

select 

Table1.Column1.value('text() [1]', 'varchar(50)'),
Table1.Column1.value('text() [2]', 'varchar(50)')


from @x.nodes('/Table6/Flt_x0020_No, /Table6/AvNo') As Table1(Column1)

Which gives me:

FltNo|AvNo

1 |Null

3460 |Null

2046 |Null

2489 |Null

I am looking for a result of:

FltNo|AvNo

1 |2046

3460 |2489

Does anybody know how to do this? Thanks.

4

2 回答 2

0

这有效:

select  Table1.Column1.value('Flt_x0020_No[1]', 'varchar(50)'),
        Table1.Column1.value('AvNo[1]', 'varchar(50)')
from    @x.nodes('/Table6') As Table1(Column1)
于 2013-02-19T07:35:52.410 回答
0

这是一个错字吗?

Table1.Column1.value('text() [1]', 'varchar(50)'),
Table1.Column1.value('text() [2]', 'varchar(50)')

似乎第二行应该说Column2

Table1.Column1.value('text() [1]', 'varchar(50)'),
Table1.Column2.value('text() [2]', 'varchar(50)')
于 2013-02-19T07:07:57.923 回答