Link to home
Start Free TrialLog in
Avatar of steven
stevenFlag for United States of America

asked on

BCP Queryout with Order by clause

Can anyone tell if its possible to use bcp queryout to do the below as I cannot get it, we have sql 2012 r2 (If I simply bcp the table out it works but I don't want at the columns and we need the order by?):  


DECLARE @data nvarchar(2000);
SELECT @data = '      bcp "select job,item,description,extruder,PiecesPerMachHr,qtyordered,operationstart,operationend,rawmaterial,unit_weight,u_m,tooling,us_length,suffix       from einc_app.dbo.EnsIntouch1
                        order by extruder,operationstart" queryout "\\eincintouch\Intouch2\Download\SytelineSch.csv" -c -t, -T -SEINCDATA01';
PRINT @data
exec xp_cmdshell @data ;

When run SQL gives me
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]
NULL



Thanks
Avatar of ste5an
ste5an
Flag of Germany image

I would do it in PowerShell as scheduled task or in SSIS.

Cause one problem is security: to make this work, you need to run either SQL Server using an account having network access permissions or use a proxy account for xp_cmdshell.
Seems like a typo..
-SEINCDATA01
there should be a space between S and the Server name, kindly try once and you should be able to ORDER by columns in BCP..
ASKER CERTIFIED SOLUTION
Avatar of Mark Wills
Mark Wills
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of steven

ASKER

This worked to perfection.  Thank you very much for all the help.
>> Has to be on one line (no carriage returns)

Nice catch, Thanks for reminding.. forgot this one as using SSIS nowadays and bcp was history..