XQuery [value()]: 'value()' requires a singleton (or empty sequence), found operand of type 'xdt:untypedAtomic *'

后端 未结 6 1023
执念已碎
执念已碎 2020-12-02 20:14

I\'m trying to insert rows into a table using a select from XML. I think I\'m close. Where am I going wrong?

declare @xmldata xml;
set @xmldata = \'

        
6条回答
  •  一生所求
    2020-12-02 20:36

    A co-worker had tackled a similar problem before. Here is what we came up with. NOT intuitive!

    insert into PurchaseDetails
    (Upc, Quantity, PurchaseDate, PurchaseCity, PurchaseState)
    select
        pd.value('Upc[1]','char(11)'),
        pd.value('Quantity[1]','int'),
        pd.value('PurchaseDate[1]','varchar(7)'),
        pd.value('PurchaseCity[1]','varchar(50)'),
        pd.value('PurchaseState[1]','char(2)')
    from @xmlData.nodes('//Database/PurchaseDetails') as x(Rec)
    cross apply @xmlData.nodes('//Database/PurchaseDetails/PurchaseDetail') as i(pd)
    

提交回复
热议问题