Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Output results of stored procedure to text file

Status
Not open for further replies.

FancyPrairie

Programmer
Oct 16, 2001
2,917
US
I have a view for which I want the results to be output to a text file. To do this I'm using bcp and xp_cmdshell. It works fine. However, I want the column names to appear at the top of the output file. So, I hardcoded the column names and added them to my view. So my view looks something like this:
Code:
Select 1 as SortMe, 'c1' as c1, 'c2' as c2
Union
Select 2 as SortMe, c1, c2, c3
From MyTable
Order By SortMe, c1, c2
My stored procedure looks something like this:
Code:
DECLARE @sqlCmd nvarchar(255)
set @sqlCmd = 'bcp "' + 'Select * from MyView Order By SortMe, c1, c2"  queryout MyOutputFile.txt -U MyUserName -P MyPassword -c -t"|"'

exec master..xp_cmdshell @sqlCmd
Here's my problem. The output file is in binary format. However, if I remove SortMe from the Order By clause above, the output file is in text format (which is what I want). So, why does adding SortMe to the Order by clause change the output format?
 
I guess everything is ok. If I include SortMe in the order by clause and open the file up in Notepad, the data consists of nothing but a page full of symbols. However, if I don't include SortMe, Notepad shows everything as text.

If I open the file in Wordpad, no matter how it's sorted, Wordpad displays the data as text.

So, I guess it works.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top