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 9173921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:39:55+00:00 2026-06-17T16:39:55+00:00

I found a bat script on the internet which exports a query from SQL

  • 0

I found a bat script on the internet which exports a query from SQL with a header row and into a tab delimited csv file. The script works great, however, I need the file format to be a comma separated file rather than tabular.

I have had a bit of a play around with the BCP parts of the script, but it doesn’t work. Does anyone have any ideas?

@ECHO OFF
REM -------------------------------------------------------------------------------
REM Generic script for exporting data to file from SQL Server using a SQL query.
REM The resulting file will be tab separated with newline as the row delimiter.
REM A log file is generated and kept in case of an error or when in debug mode.
REM See command syntax for details.
REM
REM History:
REM 20120327    Lars Rönnbäck   CREATED
REM -------------------------------------------------------------------------------
:constants
SET myCodePage=ACP
:variables
SET theQuery=SELECT  [D-U-N-S Number], [Company Name], [Street Address Line 1], [Street         Address Line 2], [Street Address Line 3], [Street Address Line 4], Town, [County (Actual)], [Full Postcode], [Telephone Number] FROM B2B_Jan13.dbo.DATA_HQ_1_Site_Level
SET theFile=C:\B2B_Matching_Automation\Temp_Files\Sites.csv
SET theServer=localhost
SET theDebug=%~4
SET /a aRandomNumber=%random%%%1000
FOR /F "usebackq tokens=1-7* delims=.:/,- " %%a IN (`ECHO %DATE%_%TIME%`) DO (SET myStartTime=%%a%%b%%c%%d%%e%%f%%g)
SET myColumnQuery="select top 0 * into [#columns_%myStartTime%_%aRandomNumber%] from (%theQuery%) q; select stuff((select char(9) + c.name from tempdb.sys.columns c where c.object_id = t.object_id order by c.column_id for XML path(''), type).value('.', 'varchar(max)'), 1,1,'') AS Header from tempdb.sys.tables t where t.name like '#columns_%myStartTime%_%aRandomNumber%%%'"
SET myHeaderFile=%theFile%.%aRandomNumber%.header
SET myDataFile=%theFile%.%aRandomNumber%.data
SET myLogFile=%theFile%.%myStartTime%_%aRandomNumber%.log
:checks
IF "%theQuery%"=="" (
GOTO syntax
) 
IF "%theFile%"=="" (
GOTO syntax
) 
IF "%theServer%"=="" ( 
SET theServer=%COMPUTERNAME%
)
:information
ECHO Start Time:    %myStartTime%       >> "%myLogFile%" 2>&1
ECHO Random Number: %aRandomNumber%     >> "%myLogFile%" 2>&1
ECHO File:          %theFile%           >> "%myLogFile%" 2>&1
ECHO Server Name:   %theServer%         >> "%myLogFile%" 2>&1
ECHO Query:                             >> "%myLogFile%" 2>&1
ECHO.                                   >> "%myLogFile%" 2>&1
ECHO %theQuery%                         >> "%myLogFile%" 2>&1
:export
BCP %myColumnQuery% queryout "%myHeaderFile%" -T -S "%theServer%" -a 65535 -c -C %myCodePage% -q >> "%myLogFile%" 2>&1
IF ERRORLEVEL 1 GOTO error
BCP "%theQuery%" queryout "%myDataFile%" -T -S "%theServer%" -a 65535 -c -C %myCodePage% -q >> "%myLogFile%" 2>&1
IF ERRORLEVEL 1 GOTO error
ECHO.                   >> "%myLogFile%" 2>&1
ECHO Merging files...   >> "%myLogFile%" 2>&1
ECHO.                   >> "%myLogFile%" 2>&1
COPY /A "%myHeaderFile%" + "%myDataFile%" "%theFile%" /B /Y >> "%myLogFile%" 2>&1
IF ERRORLEVEL 1 GOTO error
:cleanup
DEL "%myHeaderFile%" >NUL 2>&1
IF ERRORLEVEL 1 GOTO error
DEL "%myDataFile%" >NUL 2>&1
IF ERRORLEVEL 1 GOTO error
IF /I NOT [%theDebug%]==[Y] (
DEL "%myLogFile%"
)
IF ERRORLEVEL 1 GOTO error
GOTO end
:error
ECHO 
ECHO ERROR: An export error has occured!
IF NOT [%myLogFile: =%]==[] (
  ECHO Details can be found in:
  ECHO %myLogFile%
)
ECHO 
EXIT /B 1
:syntax
    ECHO.
ECHO SYNTAX: %0 "sql query" "output file" [server] [Y]
ECHO -------------------------------------------------------------------------------
ECHO You must specify an SQL query and an output file name in which the results of  
ECHO the query will be stored. Specifying a server is optional and defaults to the  
ECHO server you are executing on. If a fourth argument is given as Y a log file of
ECHO the command outputs will be saved in the same folder as the output file.
ECHO -------------------------------------------------------------------------------
:end
REM This is the end.
  • 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-17T16:39:56+00:00Added an answer on June 17, 2026 at 4:39 pm

    Take a look at the -t Option of the BCP Command.

    -t field_term

    Specifies the field terminator. The default is \t (tab character). Use this
     parameter to override the default field terminator. For more
     information, see Specify Field and Row Terminators (SQL Server).
    
    If you specify the field terminator in hexadecimal notation in a bcp.exe command, 
     the value will be truncated at 0x00. For example, if
     you specify 0x410041, 0x41 will be used.
    
    If field_term begins with a hyphen (-) or a forward slash (/), do not include a
      space between -t and the field_term value.
    

    Found Here

    I’ve not used this myself but maybe that will point you in the right direction.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a BAT file, which creates a number of csv files by reading
I was wondering whether the following command (from a .bat script): (1) connects to
I found the solution to run an executable file (.bat or .exe) from a
Need to write batch script which copies file from FTP to local drive I'm
I found the bat file from here: http://www.dostips.com/DtCodeBatchFiles.php#Batch.FindAndReplace I want to run another command
I have a DOS batch script that invokes a java application which interacts with
How can I call an external program which is written in bash script in
I have a build script (.bat) that invokes MSBUILD ( C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\msbuild.exe ) that used
Hi I have an Issue when trying to run a widows batch script from
Im using the sqlFile to include an sql script that creates tables using oracle

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.