Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Nicola writes "dear SqlTeam,I'm writing a TSQL stored procedure (in sqlserver 6.5) that need to export data from a table into an external text file.The table contains a first column that defines the sort order and a second one with the text that need to be exported. The output file must contain only the second column.BCP (6.5) do not support SELECT...ORDER BY statement in place of the table name The only way a see is to use a cursor with a loop like: exec xp_cmdshell 'echo ' + @myLine + '>> myfile.txt'but I suppose there are better ways. Any Idea?"
robvolk
Most Valuable Yak
15732 Posts
Posted - 2002-05-10 : 09:32:26
eeeeeeeeeeeeeeewwwwwwwwwwwwwwwwwwww cursors!Take a look at isql, it has a flag to specify a column separator if you need one, and it will run any query or .SQL file you like (so you can use the ORDER BY clause) You can redirect the output to a text file, and you should get the equivalent of bcp.It's documented in Books Online, I don't have the 6.5 version, but the 2000 version indicates it's definitely possible.
YellowBug
Aged Yak Warrior
616 Posts
Posted - 2002-05-10 : 11:18:05
This may help, and should work on any version:1. SELECT.. INTO tmpTABLE FROM origTABLE ORDER BY x2. exec xp_cmdshell 'BCP tmpTABLE'3. DROP TABLE tmpTABLE