Hi there,
I've below data export/import requirement from XML to SQL Server table (and vice-versa).
SOURCE (Export)
- Below is my query to export data into plain text file [no header in XML file, of course].
- This query is working fine.
SELECT
(
SELECT
'AdventureWorks' AS '@Table'
,'ContactID' AS 'Column/@Name'
, c.ContactID AS 'Column/*'
, NULL
,'FirstName' AS 'Column/@Name'
, c.FirstName AS 'Column/*'
, NULL
,'LastName' AS 'Column/@Name'
, c.LastName AS 'Column/*'
, NULL
,'Phone' AS 'Column/@Name'
, c.Phone AS 'Column/*'
, NULL
,'DateOfBirth' AS 'Column/@Name'
, c.DateOfBirth AS 'Column/*'
, NULL
FROM dbo.Contact c
ORDER BY ContactID
FOR XML PATH('DataRow'), Type
)
FOR XML PATH('Root')
- Output XML Example :
<Root>
<DataRow Table="AdventureWorks">
<ColumnName="ContactID">1</Column>
<ColumnName="FirstName">Gustavo</Column>
<ColumnName="LastName">Achong</Column>
<ColumnName="Phone">398-555-0132</Column>
<ColumnName="DateOfBirth">1985-11-18T00:00:00</Column>
</DataRow>
<DataRow Table="AdventureWorks">
<ColumnName="ContactID">2</Column>
<ColumnName="FirstName">Catherine</Column>
<ColumnName="LastName">Abel</Column>
<ColumnName="Phone">747-555-0171</Column>
<ColumnName="DateOfBirth">1987-10-04T00:00:00</Column>
</DataRow>
</Root>
DESTINATION (Import)
- How can I import above XML data into SQL Server 2008 R2 table? Any T-SQL codeplease.
- NOTE-Table structure like column name, etc... is same at source and destination as well.
Many Thanks.