We help IT Professionals succeed at work.

BCP Usage Error

faclogistics
faclogistics asked
on
3,248 Views
Last Modified: 2012-06-27
I am now getting the BCP Usage error.

"Copy direction must be either 'in', 'out' or 'format'."
for...
bcp out "exec  sp_export_Transite_qry " + @poid + " queryout d:\transite\transite_" + @poid + ".txt -c -T"

Your original instructions did not specify to put 'out' in the command, but I added it when I got the Usage error.  It didn't help.

Here is the actual code:

CREATE proc sp_export_Transite
      @poid as INT
as
--exec sp_export_Transite 'poid'
begin
declare @cmd as varchar(2000)
select @cmd = 'bcp out "exec  sp_export_Transite_qry " + @poid + " queryout d:\transite\transite_" + @poid + ".txt -c -T"'
print @cmd
EXEC master..xp_cmdshell @cmd

end
GO
Comment
Watch Question

Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
please try this:
CREATE proc sp_export_Transite
     @poid as INT
as
--exec sp_export_Transite 'poid'
begin
declare @cmd as varchar(2000)
select @cmd = 'bcp out "exec  sp_export_Transite_qry " + cast( @poid as varchar(30)) + " queryout d:\transite\transite_" + cast (@poid as varchar(30)) + ".txt -c -T"'
print @cmd
EXEC master..xp_cmdshell @cmd

end
GO

Author

Commented:
Made suggested change.  same error - Copy direction must be either 'in', 'out' or 'format'.

bcp out "exec  sp_export_Transite_qry " + cast( @poid as varchar(30)) + " queryout d:\transite\transite_" + cast (@poid as varchar(30)) + ".txt -c -T"
CERTIFIED EXPERT

Commented:
your problem is you are using DOUBLE QUOTES where u need 2 SINGLE quotes.. .

AneeshDatabase Consultant
CERTIFIED EXPERT
Top Expert 2009

Commented:
Hi faclogistics,
I think its because u use both out and queryout

Cheers!
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/coprompt/cp_bcp_61et.asp

Actually, you had the quotes and double quotes wrong:

CREATE proc sp_export_Transite
     @poid as INT
as
--exec sp_export_Transite 'poid'
begin
declare @cmd as varchar(2000)
select @cmd = 'bcp "exec  sp_export_Transite_qry ' + cast( @poid as varchar(30)) + '" queryout "d:\transite\transite_' + cast (@poid as varchar(30)) + '.txt" -c -T '
print @cmd
EXEC master..xp_cmdshell @cmd

end
GO
AneeshDatabase Consultant
CERTIFIED EXPERT
Top Expert 2009

Commented:
declare @cmd as varchar(2000)
--select @cmd = 'bcp  "exec  sp_export_Transite_qry " + @poid + " queryout d:\transite\transite_" +" out " + @poid + ".txt -c -T"'
select @cmd = 'bcp  "exec  sp_export_Transite_qry  '+ cast( @poid as varchar(30)) ++'"'+ ' queryout "d:\transite\transite_' + cast (@poid as varchar(30)) +' .txt" -c -T'
print @cmd
EXEC master..xp_cmdshell @cmd
CERTIFIED EXPERT

Commented:


select @cmd = 'bcp "exec pubs..sp_getauthor ''' + @param1 +'''" queryout c:\authors.txt -c -U sa -P sa'
------------------^ single quote
-----------------------^ double quote
------------------------------------------------------^three single quotes
-------------------------------------------------------------------------^three single quotes followed by a double quote
----------------------------------------------------------------------------------------------------------------------^terminating single quote


Jay

Author

Commented:
What goes at the 2nd @param?  See below.

select @cmd = 'bcp out "exec  sp_export_Transite_qry ''' + cast( @poid as varchar(30)) + '''" queryout d:\transite\transite_''' + cast (@poid as varchar(30)) + '''".txt -c -T'
-------------------------------------^ 3 single quotes?
----------------------------------------------------------------------------------^ 3 single, 1 double?

thanks...Crissy

Author

Commented:
removed 'out'.  Corrected quotes.

Getting:
Password:
SQLState = 37000, NativeError = 2812
Error = [Microsoft][ODBC SQL Server Driver][SQL Server]Could not find stored procedure 'sp_export_Transite_qry'.
NULL

Using -T so why asking for password?
Verified that sp_export_Transite_qry exists and is spelled correctly.  Also verified working in correct db.  Why this error?
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
as @poid is actually an integer, you don't need the 3 single quotes...
please try my suggestion above
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Now I know why DBAs make the big bucks!

Implemented your changes.  Now getting:

Error = [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'cshehadeh'. Reason: Not associated with a trusted SQL Server connection.

cshehadeh (that's me) - has admin rights to the sql server as well as the folder I am writing the output to
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
the problem is that, which jltoops pointed out, the BCP will be executed under the account the sql server service runs under, and NOT under the account that runs this sql. this is by design.
hence, in your code, you need to specify a username/password OR, which will be better, configure the sql server service to run under a dedicate domain account.

Cheers

Author

Commented:
How do I configure the sql service to run under a dedicated domain account?
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
Right-Click your computer -> Manage
in the services folder open the SQL Server Service, and set the logon information.
Once you set that, restart the service.

note that usually you use a dedicated domain account that has been added to the local admin group

Author

Commented:
Thanks...

Actually the -T worked.  I decided to go with that so that I don't have usernames and passwords floating around.

You guys are awesome!  I'm splitting the points.
CERTIFIED EXPERT

Commented:
Glad it worked....
cool!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.