保留SQL导出中的前导零到TXT

时间:2014-11-21 23:29:50

标签: sql file text export ssms

我需要将会计数据从SQL导出到一系列TXT文件中,并使用" ServiceCode" 02,35,55或70.除了" 02之外,一切正常。"我不能让领先的零点坚持下去。

这是SQL脚本:


USE [DsPcDb]
GO
/****** Object:  StoredProcedure [dbo].[JRServiceUsage_S]    Script Date: 11/21/2014 14:26:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO



-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[JRServiceUsage_S]
    -- Add the parameters for the stored procedure here
    @serviceCode varchar(2)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    DECLARE @beginDate datetime, @endDate datetime, @itemCount int, @total decimal(10,2)
    SET @beginDate = DATEADD(day,DATEDIFF(day,1,GETDATE()),0)
    SET @endDate = DATEADD(day,DATEDIFF(day,0,GETDATE()),0)
    --PRINT @beginDate
    --PRINT @endDate

    CREATE TABLE #tblData( CostCenter nvarchar(50), ItemCount int, ServerName nvarchar(50), ServiceCode int, Price decimal(10,2) )
    CREATE TABLE #tblTotals( UsageDate datetime, CostCenter nvarchar(50), ServiceCode int, ItemCount int, Total decimal(10,2) )

    INSERT INTO #tblData SELECT ServiceConsumer_T_Projekt.Name, ServiceUsage_T.Cardinality, ServiceProvider_T.ServerName, Service_T.ServiceCode, ServiceUsage_T.AmountPaid
    FROM ServiceConsumer_T AS ServiceConsumer_T RIGHT OUTER JOIN
    ServiceUsage_T AS ServiceUsage_T ON ServiceConsumer_T.ID = ServiceUsage_T.ServiceConsumer LEFT OUTER JOIN
    ServiceConsumer_T AS ServiceConsumer_T_Group ON ServiceUsage_T.ServConsGroup = ServiceConsumer_T_Group.ID LEFT OUTER JOIN
    ServiceConsumer_T AS ServiceConsumer_T_Projekt ON ServiceUsage_T.ServConsProject = ServiceConsumer_T_Projekt.ID LEFT OUTER JOIN
    ServiceProvider_T AS ServiceProvider_T ON ServiceUsage_T.ServiceProvider = ServiceProvider_T.ID INNER JOIN
    Service_T AS Service_T ON ServiceUsage_T.Service = Service_T.ID
    WHERE (ServiceUsage_T.UsageEnd >= @beginDate) AND (ServiceUsage_T.UsageEnd < @endDate)

    UPDATE #tblData SET CostCenter = '' WHERE CostCenter IS NULL

    -- SELECT COST CENTER GROUPS --
    DECLARE @costCenter varchar(50)
    DECLARE tempCCCursor CURSOR 
    FOR SELECT CostCenter
        FROM #tblData
        GROUP BY CostCenter
    OPEN tempCCCursor 
    FETCH NEXT FROM tempCCCursor 
    INTO @costCenter 
    WHILE @@FETCH_STATUS = 0 
    BEGIN 
        SET @total = 0
        SET @itemCount = 0

        IF @serviceCode = '02'
        BEGIN
            SELECT @total = COALESCE(SUM(Price),0), @itemCount = COALESCE(SUM(ItemCount),0)
            FROM #tblData
            WHERE CostCenter = @costCenter AND ServiceCode IN(SELECT DISTINCT [ServiceCode]
                FROM [DsPcDb].[dbo].[Service_T]
                WHERE [Name] NOT LIKE '%color%' AND [Name] NOT LIKE '%scan%' AND [Name] NOT LIKE '%fax%' AND [ServiceCode] <= 196623)
        END

        IF @serviceCode = '55'
        BEGIN
            SELECT @total = COALESCE(SUM(Price),0), @itemCount = COALESCE(SUM(ItemCount),0)
            FROM #tblData
            WHERE CostCenter = @costCenter AND ServiceCode IN(SELECT DISTINCT [ServiceCode]
                FROM [DsPcDb].[dbo].[Service_T]
                WHERE [Name] LIKE '%color%' AND [Name] NOT LIKE '%scan%' AND [Name] NOT LIKE '%fax%' AND [ServiceCode] <= 262159)
        END

        IF @serviceCode = '70'
        BEGIN
            SELECT @total = COALESCE(SUM(Price),0), @itemCount = COALESCE(SUM(ItemCount),0)
            FROM #tblData
            WHERE CostCenter = @costCenter AND ServiceCode IN(SELECT DISTINCT [ServiceCode]
                FROM [DsPcDb].[dbo].[Service_T]
                WHERE [Name] LIKE '%scan%')
        END

        IF @serviceCode = '35'
        BEGIN
            SELECT @total = COALESCE(SUM(Price),0), @itemCount = COALESCE(SUM(ItemCount),0)
            FROM #tblData
            WHERE CostCenter = @costCenter AND ServiceCode IN(SELECT DISTINCT [ServiceCode]
                FROM [DsPcDb].[dbo].[Service_T]
                WHERE [Name] LIKE '%fax%' AND [ServiceCode] <= 393231)
        END

        IF (@itemCount <> 0)
        BEGIN
            INSERT INTO #tblTotals (CostCenter, ServiceCode, ItemCount, Total) 
            VALUES (@CostCenter, @serviceCode, @itemCount, @total)
        END

        FETCH NEXT FROM tempCCCursor INTO @costCenter 
    END 

    CLOSE tempCCCursor 
    DEALLOCATE tempCCCursor
    -- END SELECT COST CENTER GROUPS --

    SELECT * FROM #tblTotals ORDER BY CostCenter

    DROP TABLE #tblData
    DROP TABLE #tblTotals

END

...这里有一些来自&#34; 02&#34;输出.txt文件,从第三列中删除前导零:


21分之11/ 2014,00000-24057,2,1125
21分之11/ 2014,00001-MKTG12,2,150
21分之11/ 2014,40059-00395,2,62
21分之11/ 2014,40059-00409,2,32
21分之11/ 2014,40059-00428,2,12
21分之11/ 2014,40059-00432,2,2
21分之11/ 2014,40059-00434,2,218


这不会让接收数据库导入数据。 (&#34; 02&#34;是一个特定的代码。)正如我所说,其他三个txt文件都是正确导入的。

请帮忙! 谢谢!

1 个答案:

答案 0 :(得分:0)

您将列定义为整数,因此毫不奇怪它是作为整数输出。使它成为一些变量类型:

CREATE TABLE #tblTotals (
    UsageDate datetime,
    CostCenter nvarchar(50),
    ServiceCode varchar(255),
----------------^
    ItemCount int,
    Total decimal(10,2)
)

我只是将varchar(255)用于任意不太长的字符串。你可能有更好的类型。

如果代码作为字符串存储在数据库中,这应该可以解决您的问题。