I need to insert rows into database table from XML file. My XML format is like below :
<Main>
<Parent>
<Title>Title1</Title>
<Code>ABC123</Code>
<Name>name1</Name>
<company>test1</company>
<Children>
<Child>
<Title>t1</Title>
<ContentType>T1</ContentType>
<TimeStarted>2018-03-01T10:47:46</TimeStarted>
<TimeFinished>2018-03-01T10:48:08</TimeFinished>
</Child>
<Child>
<Title>t2</Title>
<ContentType>T1</ContentType>
<TimeStarted>2018-03-01T10:47:46</TimeStarted>
<TimeFinished>2018-03-01T10:48:08</TimeFinished>
</Child>
</Children>
</Parent>
<Parent>
<Title>Title2</Title>
<Code>def123</Code>
<Name>name2</Name>
<company>test2</company>
<Children>
<Child>
<Title>t1</Title>
<ContentType>T1</ContentType>
<TimeStarted>2018-03-01T10:47:46</TimeStarted>
<TimeFinished>2018-03-01T10:48:08</TimeFinished>
</Child>
<Child>
<Title>t2</Title>
<ContentType>T1</ContentType>
<TimeStarted>2018-03-01T10:47:46</TimeStarted>
<TimeFinished>2018-03-01T10:48:08</TimeFinished>
</Child>
</Children>
</Parent>
</Main>
I need to insert parent level data (under parent tag - Title,Code,Name,company) in Table1 and Children level data in Table2. Table2 is having foriengy key reference to Table1.(which we need to get using scope identity once inserted parent record.)
This can be very large xml file. Main problem is I need to insert records one by one, means in loop as if first Parent has some issue then the process should not be aborted but should log error and proceed to second or next Parent tag.
I tried searching best approach but mostly all suggestions goes for not iterating through xml and direct insert to tables. Below are some references that I tried at.
TSQL Inserting records from XML string
How to get individual identity in an XML Insert?
I need suggestion that for particularly my requirement can be achieved without looping through? Also which would be best approach to do this, like first insert into some temp table and then process it or using xquery first fetch all records in flat data and then cursor over it?? or any other approach
Please suggest.
` (which must be unique in this case)?
– Shnugo Mar 27 '18 at 10:52