将数据导出到csv文件的存储过程仅导出到一个文件

时间:2012-06-14 18:37:56

标签: sql sql-server sql-server-2008 tsql stored-procedures

我有一个有趣的剧本:

DECLARE @StartDT DATE
DECLARE @MinDOS DATE
DECLARE @TableName VARCHAR(50)
SET @TableName = 'ViewAccountDetail'
SELECT @MinDOS = MIN(dos) FROM accn_demographics
SELECT @StartDT = 
    CAST(CAST(datepart(YYYY,@MinDOS) AS varchar) + '-' + CAST(datepart(mm,@MinDOS) AS varchar) + '-' + CAST('01' AS varchar) AS DATETIME)
DECLARE @FileLocation VARCHAR(50)

WHILE @StartDT < '20110901'
BEGIN
    SET @FileLocation='C:\test\'+@TableName+cast(@StartDT as varchar)+'.csv'
    EXEC BCP_Text_File @TableName, @FileLocation    
    SET @StartDT = DATEADD(MONTH,1,@StartDT)
END

应该做的是将数据导出到csv文件中。文件名应为:

C:\test\ViewAccountDetail2011-01-01.csv
C:\test\ViewAccountDetail2011-02-01.csv
C:\test\ViewAccountDetail2011-03-01.csv
C:\test\ViewAccountDetail2011-04-01.csv
C:\test\ViewAccountDetail2011-05-01.csv
C:\test\ViewAccountDetail2011-06-01.csv
C:\test\ViewAccountDetail2011-07-01.csv
C:\test\ViewAccountDetail2011-08-01.csv

但它将所有数据存储在同一个数据中:

C:\test\ViewAccountDetail2011-01-01.csv

我做了print @FileLocation并确认它正确更新了这个变量。

在这里我是否有任何明显的,显而易见的事情?

仅供参考此行:

EXEC BCP_Text_File @TableName, @FileLocation

调用此程序:

  ALTER PROCEDURE [dbo].[BCP_Text_File]
    @table    NVARCHAR(255),  
    @filename VARCHAR(100)  
AS
BEGIN
  SET NOCOUNT ON;

  IF OBJECT_ID(@table) IS NOT NULL
  BEGIN
    DECLARE 
        @sql NVARCHAR(MAX), 
        @cols NVARCHAR(MAX) = N'';

    SELECT @cols += ',' + name
      FROM sys.columns
      WHERE [object_id] = OBJECT_ID(@table)
      ORDER BY column_id;

    SELECT @cols = STUFF(@cols, 1, 1, '');

    SET @sql = N'EXEC master..xp_cmdshell ''bcp "SELECT ''''' 
        + REPLACE(@cols, ',', ''''',''''') + ''''' UNION ALL SELECT ' 
        + 'RTRIM(' + REPLACE(@cols, ',', '),RTRIM(') + ') FROM ' 
        + DB_NAME() + '..' + @table + '" queryout "' + @filename + '" -c -T''';  

    EXEC sp_executesql @sql;
  END
  ELSE
  BEGIN
    SELECT 'The table '+@table+' does not exist in the database';
  END
END

GO

非常感谢你的帮助和指导!

1 个答案:

答案 0 :(得分:7)

似乎对我来说很好。我有一些建议:

(1)停止执行所有字符串连接以构建日期。你可以更容易地做同样的事情:

SELECT @StartDT = DATEADD(MONTH, DATEDIFF(MONTH, '19000101', @MinDOS), '19000101');

(2)stop declaring varchar without length。为了确保正确的输出,我更喜欢转换:

SET @FileLocation = 'C:\test\' + @TableName
   + CONVERT(CHAR(10), @StartDT, 120) + '.csv';

(3)通过运行存储过程并检查文件夹中的输出而不是“调试”代码,为什么不首先检查输入?另外,为什么要在日期中使用两个变量?

DECLARE 
   @StartDT DATE, 
   @TableName NVARCHAR(50), 
   @FileLocation VARCHAR(255);

SET @TableName = N'ViewAccountDetail';

SELECT @StartDT = DATEADD(MONTH, DATEDIFF(MONTH, '19000101', MIN(dos)), '19000101')
   FROM dbo.accn_demographics;

   PRINT @StartDT;
-- ^^^^^ debugging 101 - what month do we think we're starting at?

WHILE @StartDT < '20110901'
BEGIN
    SET @FileLocation = 'C:\test\' + @TableName
      + CONVERT(CHAR(10), @StartDT, 120) + '.csv';

      PRINT @FileLocation;
    --^^^^^ again, debugging 101 - what does the filename currently look like?

    --EXEC BCP_Text_File @TableName, @FileLocation    
    SET @StartDT = DATEADD(MONTH, 1, @StartDT);
END