Sql-server – SSIS:How retrieve and insert an XML Column

sql-server-2016ssisxml

I'm trying to Insert two columns from one table onto another. The first column is an int but the second column is an XML. I'm having issues inserting the xml column

enter image description here

In first SQL Task I'm doing a select of the the two columns and storing this information into an object.

enter image description here

In the for loop I'm passing on the information :

enter image description here

And in the Result set I'm declaring the variables :
enter image description here

I know that the Form_XML_Text is set to Varchar here but there is not XML option.

And running this insert statement:
INSERT INTO TEST
VALUES (?,?)

This is the error message I'm getting back:

enter image description here

Best Answer

This can be done by converting the XML column when initially selecting it then casting it back to an XML data type for the insert. This is outlined in further detail below.

  • In the first Execute SQL Task, select the XML column by casting it to an NVARCHAR data type. Keep the Form_XML_Text variable as a string data type.
  • In the Execute SQL Task within the Foreach Loop, set this variable to use the NVARCHAR data type on the Parameter Mapping pane.
  • Then in the SQL statement on the Execute SQL Task inside the loop cast it back to XML as done below. This example assumes that the XML column is the second one in the destination table.
  • I would also recommend always explicitly listing the columns when inserting into a table as follows. This will make debugging easier and prevent errors that can occur if additional columns are added to the table in future.

Insert Example:

INSERT INTO TEST (Column1, XMLColumn)
VALUES (?, CAST(? AS XML))