Create comma separated file from SQL Server 2008 with BCP and XP_CMDSHELL

Monday, May 4, 2009 |

I was assigned a task few days back to generate comma separated file from SQL Server table, It is very easy to do so with BCP utility. Generally we used to use BCP from DOS prompt but herewith I am going to use BCP right from my SSMS with XP_CMDSHELL.


use Adventureworks

CREATE TABLE [dbo].[emps](

[Name] [varchar](50),

[Dept] [varchar](10),

[Company] [varchar](15)




--insert records



SELECT 'Rajan','MIS','mar'



declare @sql varchar(1000)

--creating emps.txt file in D drive from just created table above

select @sql = 'bcp Adventureworks..emps out d:\emps.txt -c -t, -T -S '+ @@servername

--print @sql

--generally, you should run BCP from command prompt, if you wish to do it from

--SSMS, you have to use xp_cmdshell

exec master..xp_cmdshell @sql



If your xp_cmdshell is disable, there is one small article to enable it at:

I have written some more interesting article on BCP which you can find at


Reference: Ritesh Shah
Note: Microsoft Books online is a default reference of all articles but examples and explanations prepared by Ritesh Shah, founder of


Anonymous said...

You should blog things like this more.
People will judge you based on your quality.

Ritesh Shah said...

Thanks for the comment, I always try my best to help the community more and more