Cry How to...

Export Data to a Comma Separated File


To export data to a comma separated file (csv), use the bcp command line utility:

bcp [<database_name.>[<owner>].]<table_name> out <csv-file> [/U<username>] [/P<password>] [/S<server_name>] /c /t,

where:

<database_name> is an optional parameter specifying the name of the database. It can only be omitted if the desired database is the default database.
<owner> the name of the table owner.
<table_name> the name of the table.
<csv-file> The name of the comma separated file to generate.
<username> The SQL Server username to use. This can be left blank if NT authentication is used.
<password> The password to use for the account.
<server_name> The name of the server on which the database resides. This can be omitted if the database is on the local machine (i.e. the same machine. as the bcp command is issued from).

for example:

bcp mydb.dbo.sales out sales.csv /U /P /Ssales_svr /c /t,

or:

bcp mydb..resellers out resellers.csv /U /P /Ssales_svr /c /t,


These notes have been tested against SQL Server 7.



About the author: is a dedicated software developer and webmaster. For his day job he develops websites and desktop applications as well as providing IT services. He moonlights as a technical author and consultant.