title: Using bcp utility to export SQL queries to a text file
body: <p>I debug a stored procedure (SQL Server 2005) and I need to find out some values in a datatable.</p>
  
  <p>The procedure is run by an event of the application and I watch just the debugging output.</p>
  
  <p>I do the following my stored procedure (SQL Server 2005), I took a system table (master.dbo.spt_values) as example:</p>
  
  <pre><code>set @logtext = 'select name, type from master.dbo.spt_values where number=6'
  --set @logtext = 'master.dbo.spt_values'
  SET @cmd = 'bcp ' + @logtext + ' out "c:\spt_values.dat" -U uId -P uPass -c'
  EXEC master..XP_CMDSHELL @cmd 
  </code></pre>
  
  <p>So, when I uncomment the second like everything works, a file apprears on the C:\ drive... but if I coment it back leaving only the first line, any output is generated. </p>
  
  <p>How to fix this problem?</p>