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
stevendeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
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.
0
Raja Jegan RSQL Server DBA & Architect, EE Solution GuideCommented:
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..
0
Mark WillsTopic AdvisorCommented:
Has to be on one line (no carriage returns)

So, I always build the three main components then execute that. Check out :
DECLARE @sqlcommand varchar(1000) = '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'
DECLARE @QryoutName varchar(1000) = '\\eincintouch\Intouch2\Download\SytelineSch.csv'
DECLARE @bcpCommand varchar(2000) = 'bcp "'+@sqlcommand+'" queryout "' + @QryoutName + '" -S EINCDATA01 -T -c -t, -CACP'

print @bcpcommand

EXEC master..xp_cmdshell @bcpCommand

Open in new window

The space after -S makes it easier to read, but isnt an error. Some params you do need a space (like -d under certain situations). You can read about the params in : https://docs.microsoft.com/en-us/sql/tools/bcp-utility?view=sql-server-2017
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
stevendeveloperAuthor Commented:
This worked to perfection.  Thank you very much for all the help.
0
Raja Jegan RSQL Server DBA & Architect, EE Solution GuideCommented:
>> 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..
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.