I need to create a stored procedure which queries a table and creates a CSV file for that data in a specified directory location.
how do i do it?
I need to create a stored procedure which queries a table and creates a CSV file for that data in a specified directory location.
how do i do it?
It is possible to do but you need to have enough rights to run xp_cmdshell and ad hoc distributed queries.
You can call BCP via exec xp_cmdshell 'bcp dbname..table out filename.bcp .....'
this is a very similar question except it for importing. Use bcp to import csv file to sql 2005 or 2008
See here for more information on BCP.
You can't do it with a stored procedure directly. I suggest right-clicking your database in SQL Server Management Studio, and selecting "Export Data".
Supply it with a query or a stored procedure that returns rows. Tell it you want the file to be delimited by commas and text-qualified with quotes.
When the wizard asks you if you want to just execute it or save it, save it to disk.
This makes something called an SSIS package. You can then use Windows Explorer to run it, or use a command line program called dtexec.exe to run it with parameters, or give it to your DBA to have them run it inside SQL Server.