Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Export file using xp_cmdshell

Author  Topic 

pvccaz
Yak Posting Veteran

87 Posts

Posted - 2010-10-19 : 12:32:14
Hi,
I am trying to export the sql data into a excel file. So i used the bcp along with xp_cmdshell.

The excel file after it gets generated gets stored in the DBServer.

1) Is there a way to direct the output xls it to some other server that has a share directory ?

2) The column headings are not getting exported. Do i need to generate a temp table, store the column headings along with data and then output it. Is there any other way. ?

I had given the code below. Any ideas please ?

DECLARE @FileName varchar(50),
@bcpCommand varchar(2000)
SET @FileName = REPLACE('c:\Data'+ CONVERT(char(8),GETDATE(),1)+'.xls','/','-')
SET @bcpCommand = 'bcp "SELECT CustomerName, CustomerEmail FROM DBSVR.DbName.dbo.tableName" queryout "'
SET @bcpCommand = @bcpCommand + @FileName + '" -c -T'
print @bcpCommand

EXEC master..xp_cmdshell @bcpCommand

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-10-19 : 12:39:02
1,its possible. you need to use UNC path for that (like \\server\...)
2.see http://sqlblogcasts.com/blogs/madhivanan/archive/2008/10/10/export-to-excel-with-column-names.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

pvccaz
Yak Posting Veteran

87 Posts

Posted - 2010-10-19 : 19:54:27
Thank you vishak. It worked !!..
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-10-20 : 12:45:10
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -