2014-11-21 62 views
0

我需要將會計數據從SQL導出到一系列TXT文件,其中「ServiceCode」爲02,35,55或70.除「02」外,一切工作正常。我無法獲得前導零。在SQL導出到TXT時保留前導零

下面是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 

...這裏是從 「02」 輸出.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
11/21/2014,40059-00432,2,2
11月21日/ 2014,40059-00434,2,218


這不會讓接收數據庫導入數據。 (「02」是一個特定的代碼。)正如我所說的,其他三個txt文件已正確導入。

請幫忙! 謝謝!

+0

如何聲明'#tblTotals.ServiceCode'爲'char(2)'(或任何正確的類型)? – 2014-11-21 23:58:07

回答

0

您正在將列定義爲一個整數,所以毫不奇怪它將以整數形式輸出。使一些變量類型:

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

我只是用varchar(255)對於任意不太長字符串。你可能有更好的類型。

如果代碼是作爲字符串存儲在數據庫中的,這應該可以解決您的問題。