2
Answers

Dynamic XML to Table

Kalai

Kalai

9y
759
1

I am passing table name and XML string to Sql Server SP. I want to insert records to corresponding table. how to read the column name dynamically?


Example : 1
===========
Declare @TableName = 'Table1'
Declare @xml1 xml
set @xml1 = '<Root>
 <Nodes>
  <Id>01</Id>
  <Name>Mike</Name>
 </Nodes>
</Root>'

insert into @TableName (
--column names from @xml
) values
(
 --select ID from @xml,
 --select Name from @xml
)

Example : 2
===========

Declare @TableName = 'Table2'
Declare @xml2 xml
set @xml2 = '<Root>
 <Nodes>
  <EmpId>05</EmpId>
  <EmpName>Andrew</EmpName>
 </Nodes>
</Root>'

Answers (2)