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

后端 未结 6 1031
执念已碎
执念已碎 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:34

    Try this!
    query() then value()
    run this in SQL Server and 100% worked
    put a dot (.) first then the child tag.
    PurchaseDetail tag exists 2 times so the dot (.) replaces the first and the second tag.
    The dot can prevent using of [1] on XQuery.
    The dot represents the first and the second PurchaseDetail tags.

    INSERT INTO PurchaseDetails(Upc, Quantity, PurchaseDate, PurchaseCity, PurchaseState)
    SELECT col.query('./Upc').value('.', 'char(11)'),
        col.query('./Quantity').value('.', 'int'),
        col.query('./PurchaseDate').value('.', 'varchar(7)'),
        col.query('./PurchaseCity').value('.', 'varchar(50)'),
        col.query('./PurchaseState').value('.', 'char(2)')
    FROM @xmlData.nodes('/Database/PurchaseDetails/PurchaseDetail') as ref(col)
    

    It is more simplified query so far.
    See if it works

提交回复
热议问题