简体   繁体   中英

SSIS Export table with different types of columns into flat file

I'm working on a SSIS Package.

I have a table as below:

Table Name: Employee_table

EmployeID     EmployeeName     EmployeeDataXML
==============================================
1             Mark            <Age>32</Age><Role>Manager</Role>
2             Albert          <Age>31</Age><Role>Staff</Role>
==============================================

This table has to be exported into a flat file with name: Employeedata.dat

Content in the file should look like this:

<EmployeeID>1</EmployeeID><EmployeeName>Mark</EmplyeeName><EmployeeDataXML><Age>32</Age><Role>Manager</Role></EmployeeDataXML>
<EmployeeID>2</EmployeeID><EmployeeName>Albert</EmplyeeName><EmployeeDataXML><Age>31</Age><Role>Staff</Role></EmployeeDataXML>

Basically, the employeeid and employeename columns are not in xml format but still when the export happens they should be wrapped up in xml too.

Can someone guide me which is the best way to do it? Do i need to use any transformation here? Is there any control/task which is readily available? Can writing a SQL Select Statement which could simply solves this?

Please guide.

Yes, a simple SELECT using FOR XML PATH should take care of this:

DECLARE @TestData TABLE
(
  EmployeID INT NOT NULL,
  EmployeeName NVARCHAR(50) NOT NULL,
  EmployeeDataXML XML
);

INSERT INTO @TestData (EmployeID, EmployeeName, EmployeeDataXML)
  VALUES (1, N'Mark', N'<Age>32</Age><Role>Manager</Role>');
INSERT INTO @TestData (EmployeID, EmployeeName, EmployeeDataXML)
  VALUES (2, N'Albert', N'<Age>31</Age><Role>Staff</Role>');


SELECT EmployeID, EmployeeName, EmployeeDataXML
FROM   @TestData
FOR XML PATH(N'Employee');

produces the following:

<Employee>
  <EmployeID>1</EmployeID>
  <EmployeeName>Mark</EmployeeName>
  <EmployeeDataXML>
    <Age>32</Age>
    <Role>Manager</Role>
  </EmployeeDataXML>
</Employee>
<Employee>
  <EmployeID>2</EmployeID>
  <EmployeeName>Albert</EmployeeName>
  <EmployeeDataXML>
    <Age>31</Age>
    <Role>Staff</Role>
  </EmployeeDataXML>
</Employee>

You didn't have the parent <Employee> element shown in the sample output, but I don't think the file would be usable without some element wrapping the field elements into a "row".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM