Link to home
Create AccountLog in
Avatar of pansophy
pansophy

asked on

SQL table export to text file sorted by 2 columns

I have a query that I would like to export to a text file using SQL Server Import and Export Wizard.  The query itself has the columns to sort by, but upon export the text file is not sorted.  

What is an easy way to export the query sorted?

Thanks...
ASKER CERTIFIED SOLUTION
Avatar of Ephraim Wangoya
Ephraim Wangoya
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Did you tried to run the query and you can see that it is actualy sorted? Then you should be able to just replace it in the code below and generate an output file that will be ON the server where you run the query and not on the client from where you run it - it MUST be sorted...


exec master..xp_cmdshell N'sqlcmd -E -Q"select * from server_name.dba_name.schema_name.table_name" -h-1 -s"," -W -o"c:\output.txt"', no_output

for more info on sqlcmd switches and impact in output file format please see:
http://msdn.microsoft.com/en-us/library/ms162773.aspx