steven
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,extru der,Pieces PerMachHr, qtyordered ,operation start,oper ationend,r awmaterial ,unit_weig ht,u_m,too ling,us_le ngth,suffi x from einc_app.dbo.EnsIntouch1
order by extruder,operationstart" queryout "\\eincintouch\Intouch2\Do wnload\Syt elineSch.c sv" -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
DECLARE @data nvarchar(2000);
SELECT @data = ' bcp "select job,item,description,extru
order by extruder,operationstart" queryout "\\eincintouch\Intouch2\Do
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
Seems like a typo..
-SEINCDATA01there 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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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..
Nice catch, Thanks for reminding.. forgot this one as using SSIS nowadays and bcp was history..
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.