将数据加载到具有固定长度ssis的.txt文件中

时间:2018-05-31 19:46:12

标签: sql-server ssis

我有一项任务是创建SSIS包以将数据加载到.txt文件中,如下所示

有一个表格ex:employees

有两个SQL语句:

select emp_no,birth_date,first_name from employees
where emp_no in (10001,10002,10003,10004);
Result: (Fixed Lengths as Follows emp_No=5,Birth_date=10,First_Name=10, Total Length: 25)
10001   1953-09-02  abc
10002   1964-06-02  def
10003   1959-12-03  ghi
10004   1954-05-01  jkl

第二声明:

select emp_no,last_name,gender,hire_date from employees
where emp_no in (10001,10002,10003,10004);
Result: (Fixed Lengths as follows emp_No=5,lastname=9,gender=1,hire_date=10, Total Length: 25)
10001   Abcdef      M   1986-06-26
10002   Bcdefghi    F   1985-11-21
10003   Cdefghijk   M   1986-08-28
10004   Defgh       M   1986-12-01

我必须创建一个包以获取如下数据

100011953-09-02abc
10001Abcdef   M1986-06-26
100021964-06-02def
10002Bcdefghi F1985-11-21
100031959-12-03ghi
10003CdefghijkM1986-08-28
100041954-05-01jkl
10004Defgh    M1986-12-01

请建议我在SSIS或SQL服务器中如何操作

谢谢

2 个答案:

答案 0 :(得分:1)

欺骗系统。在单个查询中生成两个查询的嵌套输出,如下所示:

select 
    Cast(emp_no AS Char(5)) + Convert(Char(10), birth_date, 120) + 
    CAST(first_name as CHAR(10)) + CHAR(13) + CHAR(10) +
    Cast(emp_no AS Char(5)) + CAST(last_name as CHAR(9)) + 
    gender + Convert(Char(10), hire_date, 120) 
from employees
where emp_no in (10001,10002,10003,10004);

答案 1 :(得分:0)

在数据源中使用UNION ALL查询来获取看起来像您想要的单列结果集,然后直接执行到平面文件目标的数据流。

Psuedo-code query:

SELECT {Query #1 Concatenated into one column, with added "SortBy" column = 1}
UNION ALL {Query #2 Concatenated into one column, with added "SortBy" column = 2}
ORDER BY emp_no, SortBy

显然,不要将“SortBy”列映射到目的地,因此它不会显示在平面文件中。