简体   繁体   English

如何将数据从查询导出到具有固定列宽的文本文件?

[英]How do I export data from a Query to a text file with fixed column widths?

Am I the only person who thinks SSIS is a piece of * ?我是唯一认为 SSIS 是一块*的人吗?

I have a query:我有一个问题:

SELECT * FROM Table1

And I want to do is put this into a text file with specific lengths in the destination file (I have specs from a client).我想做的是将其放入目标文件中具有特定长度的文本文件中(我有来自客户的规格)。

For example, Field 1 size: 20, field 2 size: 3, and so on.例如,字段 1 大小:20,字段 2 大小:3,依此类推。

I also have the field positions, Field 1: 1-20, Field 2: 21-23, etc.我还有场位,场1:1-20,场2:21-23,等等。

I created an SSIS package that has a source as a SQL query and a flat file as the destination.我创建了一个 SSIS package,它的源是 SQL 查询,平面文件是目标。 I CANNOT FOR THE LIFE OF ME GET THIS TO WORK.我一辈子都无法让它工作。

The data on the text file is all messed up (its scattered all over the place, no columns).文本文件上的数据全乱了(散落在各处,没有列)。

This is the simplest task in the world and I cannot do it.这是世界上最简单的任务,我做不到。 I don't know if it's me or if SSIS is just a piece of crap.我不知道是我的问题还是 SSIS 只是一坨屎。

Go to the Connection Manager you defined for your flat file. 转到您为平面文件定义的连接管理器。 In the left box, select General and set the Format to "Fixed Width". 在左侧框中,选择“常规”,然后将“格式”设置为“固定宽度”。 Then in the left box, select Advanced and here you can set the OutputColumnWidth for each field. 然后在左侧框中,选择“高级”,在这里您可以为每个字段设置OutputColumnWidth。 Lastly, in the left box, select Preview to verify the results before executing the SSIS package. 最后,在左侧框中,选择“预览”以验证结果,然后再执行SSIS包。

Hope this helps. 希望这可以帮助。 Kosh 信贷基金

I actually just did this last week. 我上周实际上只是这样做。 I wrote a function that does this then for each field I call the function. 我编写了一个函数,然后针对每个字段调用此函数。

Here is the function 这是功能

/*
USE [Newton-Dev]
GO
/****** Object:  UserDefinedFunction [dbo].[CharPad]    Script Date: 1/10/2015 11:38:27 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/*
Script  : Character Padding Function; Assist with fixed width file creation
Version : 1.0 (01/08/2015)
Author  : Jeffery Williams

*/
ALTER FUNCTION [dbo].[CharPad] (
     @Input VARCHAR(255)
    ,@OutputWidth INT
    ,@OutputAlign VARCHAR(5)
    ,@PadCharacter CHAR(1) )
RETURNS VARCHAR(255)
AS
BEGIN
DECLARE @Output VARCHAR(255)
DECLARE @InputWidth INT

SET @InputWidth = LEN(@Input)

IF @InputWidth > @OutputWidth
    BEGIN 
        IF @OutputAlign = 'LEFT'
            BEGIN
            SET @Output = LEFT(@Input,@OutputWidth)
            END
        IF @OutputAlign = 'RIGHT'
            BEGIN
            SET @Output = RIGHT(@Input,@OutputWidth)
            END
    END

IF @InputWidth < @OutputWidth 
    BEGIN 
        IF @OutputAlign = 'RIGHT'
            BEGIN
            SET @Output = REPLICATE(@PadCharacter, @OutputWidth - @InputWidth ) + @Input
            END
        IF @OutputAlign = 'LEFT'
            BEGIN
            SET @Output =@Input+ REPLICATE(@PadCharacter, @OutputWidth - @InputWidth )
            END
    END

IF @InputWidth = @OutputWidth 
    SET @Output = @Input

RETURN (@Output)
END

Here is the query that calls the function and provides the fixed width output: 这是调用该函数并提供固定宽度输出的查询:

SELECT --dbo.CharPad (DeltaLineId,8,'LEFT',' '), dbo.CharPad (DeltaElgId,8,'LEFT',' ')
       dbo.CharPad (CARRIER_ID,6,'LEFT',' ')
       ,dbo.CharPad (GROUP_NUM,7,'RIGHT','0')
      ,dbo.CharPad (LEFT('0000' + SUB_GROUP_ID, 4),9,'LEFT',' ')
      ,dbo.CharPad (SVC_TYPE,1,'LEFT',' ')
      ,dbo.CharPad (FILLER_1,1,'LEFT',' ')
      ,dbo.CharPad (FILLER_2,5,'LEFT',' ') 
      ,dbo.CharPad (RATE_CODE,2,'LEFT',' ')
      ,dbo.CharPad (FILLER_3,1,'LEFT',' ')
      ,dbo.CharPad (ELIG_CODE,1,'LEFT',' ')
      ,dbo.CharPad (EFF_DATE,8,'LEFT',' ') 
      ,dbo.CharPad (TERM_DATE,8,'LEFT',' ') 
      ,dbo.CharPad (SUBSC_SSN,9,'LEFT',' ')  
      ,dbo.CharPad (INDIV_SSN,9,'LEFT',' ') 
      ,dbo.CharPad (CHNG_SSN,9,'LEFT',' ')   
      ,dbo.CharPad (REL_CODE,2,'LEFT',' ') 
      ,dbo.CharPad (HIRE_DATE,8,'LEFT',' ')  
      ,dbo.CharPad (DOB,8,'LEFT',' ') 
      ,dbo.CharPad (REL_TYPE,1,'LEFT',' ')  
      ,dbo.CharPad (FIRST_NAME,24,'LEFT',' ') 
      ,dbo.CharPad (MID_NAME,24,'LEFT',' ')                 
      ,dbo.CharPad (LAST_NAME,24,'LEFT',' ') 
      ,dbo.CharPad (GENDER,1,'LEFT',' ')  
      ,dbo.CharPad (POP_DESC,5,'LEFT',' ') 
      ,dbo.CharPad (ADR_LINE_1,30,'LEFT',' ')                      
      ,dbo.CharPad (ADR_LINE_2,30,'LEFT',' ') 
      ,dbo.CharPad (CITY,30,'LEFT',' ')                           
      ,dbo.CharPad ([STATE],2,'LEFT',' ') 
      ,dbo.CharPad (COUNTY_CODE,3,'LEFT',' ')  
      ,dbo.CharPad (COUNTRY_CODE,3,'LEFT',' ') 
      ,dbo.CharPad (ZIP,5,'LEFT',' ')    
      ,dbo.CharPad (ZIP_EXT,4,'LEFT',' ') 
      ,dbo.CharPad (FILLER_4,21,'LEFT',' ')               
      ,dbo.CharPad (USER_DEFINED,30,'LEFT',' ') 
      ,dbo.CharPad (WAIT_PERIOD,1,'LEFT',' ')  
      ,dbo.CharPad (CAID,9,'RIGHT','0') 
      ,dbo.CharPad (FILLER_5,9,'LEFT',' ') 
FROM export.DeltaLine

*/

To adjust Jeff's procedure to handle nulls, make these replacements.要调整 Jeff 的过程来处理空值,请进行这些替换。 Instead of:代替:

SET @InputWidth = LEN(@Input)

Use:利用:

SET @InputWidth = Isnull(@Input,REPLICATE(' ',@OutputWidth)

And instead of:而不是:

SET @Output = @Input

Use:利用:

SET @Output = Isnull(@Input,REPLICATE(' ',@OutputWidth)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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