Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8091517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:04:56+00:00 2026-06-05T20:04:56+00:00

I have a fun script: DECLARE @StartDT DATE DECLARE @MinDOS DATE DECLARE @TableName VARCHAR(50)

  • 0

I have a fun script:

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

It is supposed to do is export data into csv files. The names of the files should be:

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

but it stores all the data into the same one:

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

i did a print @FileLocation and confirmed that it correctly updates this variable.

is there any apparent, OBVIOUS thing that i am missing here?

FYI this line:

EXEC BCP_Text_File @TableName, @FileLocation

calls this procedure:

  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

thank you so much for your help and guidance!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T20:04:57+00:00Added an answer on June 5, 2026 at 8:04 pm

    Seems to work fine for me. I have a few suggestions:

    (1) stop doing all that string concatenation to build a date. You can do the same thing much easier as in:

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

    (2) stop declaring varchar without length. And to ensure the right output, I prefer convert:

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

    (3) instead of "debugging" the code by running the stored procedure and inspecting the output in the folder, why not sanity-check your input first? Also, why use two variables for the date?

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've the following SMT-Lib2 script: (set-option :produce-models true) (declare-fun s0 () Int) (declare-fun table0
I have recently been working on a Pastebin script (for fun) and I've come
I am moving from jQuery to MooTools (for the fun..) and I have this
Thanks to a suggestion from Chas. Owens , I have been having fun playing
I have a problem. Client side code <html> <body onload=fun()> <script src=C:\cygwin\usr\local\lib\node\.npm\socket.io\0.6.16\package\support\socket.io-client\socket.io.js></script> <script> function
i have two files. In first (parent, address: www.myaddress.com/fun/test.php) is jQuery function: <script type=text/javascript>
I have an idea for a fun little service that relies on data from
I have a fun issue where during application shutdown, try / catch blocks are
I have a fun application written in Visual Studio (C#), and I'm wondering -
I have found myself designing a language for fun that is a cross between

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.