title: SQL Server bcp utility does not create the txt file,
body: <p>I'm new to this feature in SQL Server and could use some help. I'm experimenting with the <code>BCP</code> utility and the <code>AdventureWorks2012</code> database. </p>
  
  <p>I'm attempting to export data to a text file with the BCP utility and the code executes but a file is not created. Can you please look at my code and tell me where the problem(s) is/are. </p>
  
  <p>I'm working out of a local copy of SQL Server Express. Thank you.</p>
  
  <pre><code>Declare @sql Varchar(8000)
  
  Select @sql = 'bcp
      + SELECT FirstName, LastName
      FROM AdventureWorks2012.Person.Person ORDER BY LastName, Firstname
      + queryout C:\Users\David\Desktop\yes.txt + -c -t, -T -S'
  + @@SERVERNAME
  
  EXEC master..xp_cmdshell @sql
  </code></pre>
  
  <p>Here is my output when I run the query:</p>
  
  <pre><code>output
  usage: bcp {dbtable | query} {in | out | queryout | format} datafile
    [-m maxerrors]            [-f formatfile]          [-e errfile]
    [-F firstrow]             [-L lastrow]             [-b batchsize]
    [-n native type]          [-c character type]      [-w wide character type]
    [-N keep non-text native] [-V file format version] [-q quoted identifier]
    [-C code page specifier]  [-t field terminator]    [-r row terminator]
    [-i inputfile]            [-o outfile]             [-a packetsize]
    [-S server name]          [-U username]            [-P password]
    [-T trusted connection]   [-v version]             [-R regional enable]
    [-k keep null values]     [-E keep identity values]
    [-h "load hints"]         [-x generate xml format file]
    [-d database name]        [-K application intent]  [-l login timeout]
  NULL
  </code></pre>
  
  <p>Here is the PRINT output:</p>
  
  <pre><code>bcp
      + "SELECT FirstName, LastName
      FROM AdventureWorks2012.Person.Person ORDER BY LastName, Firstname"
      + queryout C:\Users\David\Desktop\yes.txt -c -t, -T -SHOMEPC\SQLINST01
  </code></pre>
  
  <p>TT's code worked. Here it is:</p>
  
  <pre><code>DECLARE @stmt_e VARCHAR(8000);
  SET @stmt_e=
      'BCP '+
      '"SELECT FirstName,LastName FROM AdventureWorks2012.Person.Person ORDER BY LastName,Firstname" '+
      'QUERYOUT "C:\Users\David\Desktop\yes.csv" '+
      '-c -t, -T -S ' + @@SERVERNAME;
  EXEC master.sys.xp_cmdshell @stmt_e;
  </code></pre>
  
  <p>The instructions for adding system permissions for database engine access can be found at the link below. I had to do this because my SQL Server Instance did not have permission to write to the path I was specifying. </p>
  
  <p><a href="https://msdn.microsoft.com/en-us/library/jj219062.aspx" rel="nofollow">https://msdn.microsoft.com/en-us/library/jj219062.aspx</a></p>