FancyPrairie
Programmer
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:
My stored procedure looks something like this:
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?
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
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