Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Create a Comma Delimited CSV File from Any Table/View with Debugging

By Paul Andrew,

Standard usage:

  • Create the stored procedure in your database containing the objects you require CSV files for.
  • Execute the procedure passing the required parameters for the source object schema and object name. Plus the destination path for the generated file. Ensure you have write permissions to this location, the BCP utility supports local drives on your SQL Server and UNC paths only.

Optional parameters:

  • If you require a text qualifier in your CSV file this can be included as a parameter. For example: "
  • Setting the debug parameter to 1 will print the dynamically created SQL strings plus the BCP command output.

Note; if you object contains complex data types that SQL Server cannot implicitly, for example sql_varient please create a view of the objects to handle this separately.

Happy CSVing.

Thanks for looking.

Total article views: 5505 | Views in the last 30 days: 16
Related Articles

Fixed server role required for Object creation in sys databases?

What Fixed server role required for Object creation in sys databases?


How to create the report using parameters in SSRS

How to create the report using parameters in SSRS


Hi..How to create report using Parameters

Hi..How to create report using Parameters


Default parameters changing when new database is created

Default parameters when database is created with Studio


Compress All Objects

Creates compression scripts for all objects in all databases in the instance.

bcp utility    
dynamic sql    

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones