So I have a table called (CompanyDetails) with an XML column. The column contains data like this:
I need to extract data from this column and insert into different tables (employee, employeeLoan, etc).
I wrote a console app to add the EmployeeID attribute (it wasn't there) in the Employee element based on the the total number of employees in the CompanyDetails table
This works fine, but as soon as I have a lot of data in the CompanyDetails table, it is unbearably slow.
What's the best way to insert into the employee and employeeLoan tables (EmployeeID column being the PK and FK in the two tables respectively)? Any ideas?
Code:
<Companies>
<Company>
<CompanyID>100</ComapnyID>
<Employees>
<Employee [COLOR="#FF0000"]EmployeeID="1"[/COLOR]>
<Name>David</Name>
<Surname>Smith</Surname>
<Loans>
<Loan>
<Amt>100</Amt>
<Term>12</Term>
</Loan>
<Loan>
<Amt>500</Amt>
<Term>24</Term>
</Loan>
</Loans>
</Employee>
<Employee [COLOR="#FF0000"]EmployeeID="2"[/COLOR]>
<Name>Donald</Name>
<Surname>Jones</Surname>
<Loans>
<Loan>
<Amt>300</Amt>
<Term>12</Term>
</Loan>
<Loan>
<Amt>2000</Amt>
<Term>36</Term>
</Loan>
</Loans>
</Employee>
</Employees>
</Company>
</Companies>
I wrote a console app to add the EmployeeID attribute (it wasn't there) in the Employee element based on the the total number of employees in the CompanyDetails table
Code:
UPDATE CompanyDetails
SET XMLDATA.modify('insert attribute EmployeeID {sql:variable(""@EmployeeID"")} into (/*:Companies/*:Company/*:Employees/*:Employee[position()=sql:variable(""@NodeNr"")])[1]')
WHERE CompanyID = @CompanyID
What's the best way to insert into the employee and employeeLoan tables (EmployeeID column being the PK and FK in the two tables respectively)? Any ideas?