all groups > sql server mseq > september 2003 >
You're in the

sql server mseq

group:

Automating export from Sql server to Ms Excel using a script


Automating export from Sql server to Ms Excel using a script Ella
9/29/2003 4:34:38 PM
sql server mseq: Please I am trying to automate a dump from sql server to
an excel sheet. I know it's possible via the dts, but
since this is something I do 10 times a day, I was
wondering if it's possible to run a script in the Query
Analyser to export my query results to an excel sheet or
maybe I could create a button on a form and once I click
on it, it creates a dump of my data.

Thanks for your help in advance.

Re: Automating export from Sql server to Ms Excel using a script Vishal Parkar
9/30/2003 3:24:59 PM
exporting data to EXCEL.

Ex:
bcp "select * from northwind..orders" queryout c:\cust.xls -c -S<server> -Usa -P

above command will create a file with extension XLS but, in fact its a tab delimited flat file but
since it has extension XLS by default it will get opened in EXCEL and tab character is considered
as a column delimiter. I've put TAB as a column delimiter assuming this character is not used in
any of the varchar/char field. because if you are
including such a character which exists in the varchar/char datatype whole format will get
disturbed. comma seperated file is a good example for this.
Also point to be noted that if any of the char/varchar field contains TAB character even this
file's format will get disturbed.

you can also run above command line using xp_cmdshell and thus can be used in T-SQL script.

Ex

exec master..xp_cmdshell 'bcp "select * from northwind..orders" queryout
c:\cust.xls -c -S<server> -Usa -P'

Also refer to following URL

http://support.microsoft.com/default.aspx?scid=kb%3ben-us%3b306125

--
- Vishal

AddThis Social Bookmark Button