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

asked on

BCP command

I am trying to query out using with the below code, with no luck can anyone help.



 declare @outfile            nvarchar(200),
              @comrun          nvarchar(400)
 
set @outfile = '\\eincutil1\fstaskman\report\outputfiles\sa\'  + db_name() + 'ALLOpenOrder.csv'


set @comrun = 'bcp  " select  order_date,
 item,
 description,  
 qty_ordered,
 qty_avail,
 job,
 opername,
 P_M_T_code
  from einc_testapp.dbo.ensopencoA queryout"' + @outfile + ' -c -t, -T -SEINCDATA'

exec master..xp_cmdshell @comrun




The results are :

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"]
NULL
Avatar of PadawanDBA
PadawanDBA

Try:

set @comrun = N'bcp  "select  order_date,
 item,
 description,  
 qty_ordered,
 qty_avail,
 job,
 opername,
 P_M_T_code
  from einc_testapp.dbo.ensopencoA" queryout ' + @outfile + N' -c -t, -T -SEINCDATA'

edit: added the N unicode prefix to the set statement to prevent the casting
Avatar of steven

ASKER

Same result.
Avatar of steven

ASKER

Looks like its the commas that are causing the issue..any ideas?
ASKER CERTIFIED SOLUTION
Avatar of Harish Varghese
Harish Varghese
Flag of India 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
Doh, I didn't even think about this initially: could you try getting rid of the carriage returns like this:

set @comrun = N'bcp "select  order_date, item, description, qty_ordered, qty_avail, job, opername, P_M_T_code from einc_testapp.dbo.ensopencoA" queryout ' + @outfile + N' -c -t, -T -SEINCDATA'

edit: what harish said!