I am just new to reading in an XML file into a table through SQL Server Management Studio. There are probably better ways but I would like to use this approach.
Currently I am reading in a standard XML file of records on people. A <record>
tag is the highest level of each row of data. I want to read all the records into separate rows into my SQL table.
I have gotten along fine so far using the following approach as follows:
SELECT
-- Record
category, editor, entered, subcategory, uid, updated,
-- Person
first_name, last_name, ssn, ei, title, POSITION,
FROM OPENXML(@hDoc, 'records/record/person/names')
WITH
(
-- Record
category [varchar](100) '../../@category',
editor [varchar](100) '../../@editor',
entered Datetime '../../@entered',
subcategory [varchar](100) '../../@subcategory',
uid BIGINT '../../@uid',
updated [varchar](100) '../../@updated',
-- Person
first_name [varchar](100) 'first_name',
last_name [varchar](100) 'last_name',
ssn [varchar](100) '../@ssn',
ei [varchar](100) '../@e-i',
title [varchar](100) '../title',
Position [varchar](100) '../position',
)
However this approach has worked fine as the tag names have all been unique to each record/person. The issue I have is within the <Person>
tag I now have an <Aliases>
tag that contains a list of more than 1 <Alias> test name </Alias>
tags. If I use the above approach & reference '../aliases' I get all the Alias elements as one long String row mixed together. If I just try '../aliases/alias' ONLY the first element is returned per record row. If there was 10 Alias elements within the Aliases tag set I would like 10 rows returned for example.
Is there a way to specify that when there are multiple tags of the same name within a higher level tag, return them all & not just one row?
The following is the example block within the XML I am referring to:
- <aliases>
<alias>test 1</alias>
<alias>test 2</alias>
<alias>test 3</alias>
</aliases>
I would like the following in the SQL table:
Record Aliases
Record 1 test 1
Record 1 test 2
Record 1 test 3
Record 2 test 4
Record 2 test 5
but all I get is:
Record 1 test 1
Record 2 test 4
Apologies if I have not explained this correctly - any help would be greatly appreciated.