Skip to content
Advertisement

How to insert the xml nodes with same name and ignore based on the attribute

I have a below xml

`

`

I need to insert only the dob nodes which has D and M and Y attributes in table (Table A), if only Y attribute is specified then the entire tag need to be inserted in another table (Table B), how to achieve this is SQL.

Thanks for your suggestions

Advertisement

Answer

Are you looking for something like this?

–complete dobs for Table A

–Y-only dobs for Table B

Both queries use a XQuery predicate in .nodes(). The first query will return <dob> nodes where there is a D-, a M- and an Y-attribute. The second query will return <dob> nodes, where the M-attribute is empty (=> not existing).

It’s easy to collect the values for your insert. Simply use .value() instead of .query() and ask either for an attribute by using e.g. @Y or ask for the node’s content by using text():

Hint: Within XML any date/time value should be ISO8601 encoded (e.g. 1970-10-17). Otherwise you are open to culture mix-ups… Furthermore: With ISO8601 you could use date as data type in .value() in order to retrieve the value typesafe. Without this, you are prone to casting errors…

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement