如何设置对齐和字段长度

时间:2016-08-08 04:55:46

标签: sql-server stored-procedures

我已经创建了一个SQL Server存储过程,如下所示。在C#代码中,我调用此存储过程将其保存为txt文件。

问题是:

  1. 输出不是我想要的。我希望每列的输出都以左对齐开始

  2. 每列都有一个特定的长度,就像我在存储过程中设置的一样......但看起来效果不好

  3. 代码:

    ALTER PROCEDURE [dbo].[AR_Ebill_claim]
    (
        @compcode varchar(15),
        @dateFrom smalldatetime,
        @dateTo smalldatetime,
        @episType varchar (10),
        @debtorCode varchar(30)
    )
    AS
        SELECT  
            CAST(ProviderCode AS VARCHAR(30)),
            CAST(MRN AS VARCHAR(25)),
            CAST(PatName AS VARCHAR(50)),
            CAST(PatNewIC AS VARCHAR(14)),
            CAST(PatOldIC AS VARCHAR(10)),
            CAST(PatID AS VARCHAR(14)),
            CAST(RefNo AS VARCHAR(15)),
            CAST(InvNo AS VARCHAR(15)),
            (SELECT CONVERT(VARCHAR, InvDate, 103)) AS InvDate,
            CAST(BillType AS VARCHAR(2)),
            CAST(SvcType AS VARCHAR(2)),
            (SELECT CONVERT(VARCHAR, RegDate,103)) AS RegDate,
            (SELECT CONVERT(VARCHAR, DischgDate,103)) AS DischgDate,
            CAST(ClaimAmt AS MONEY),
            --sum(PayableAmount),
            --SUM(AllocationAmount),
            PayableAmount           
        FROM
            AR_Ebilling
        WHERE
            Company = @compcode 
            AND InvDate BETWEEN @dateFrom AND DATEADD(DAY,1, @dateTo) 
            AND BillType = @episType 
            AND DebtorCode = @debtorCode 
            AND paramsourcecode = 'pb'
            AND transtype = 'in'
            AND status = 0
    

1 个答案:

答案 0 :(得分:0)

我不完全确定你的要求。如果您认为由于使用了CAST而在数据集中获得列大小,则可能无效。构建查询时将应用CAST,并且可能无法将相同大小传输到最终输出元数据。

  1. 试试这个,这可能有用 - 拥有你想要的大小定义的临时表。将select语句推送到临时表中。从临时表中选择*。

  2. 我不确定你是否想要这个,但是试试 - 如果你想要像ProviderCode这样的标准尺寸占用30个字符,即使该值是10个字符,也可以在末尾加上尾随空格,

    ==>左(Cast(ProviderCode as varchar(30))+“”,30) - 只连接30个字符,左边30个 - 确保你得到固定的长度。

  3. 如果您需要更多详细信息,请分享更清晰,更具体的信息。