Queryout is not working in bcp with xp_cmdshell

Hello Experts,

When I execute the following lines, I am getting this error,

Copy direction must be either 'in' or 'out'.
Syntax Error in 'queryout'.

What's wrong with this statements, Assume that the Table tmp_Load_vou05nov06_txt is already exist.

Declare @BcpCommand varchar(200)
Declare @TempTable varchar(200)
Declare @TempFile varchar(200)
Set @TempTable = 'tmp_Load_vou05nov06_txt'
Set @TempFile = 'C:\tmp_Load_vou05nov06_txt'
Set @BcpCommand = 'bcp "select * from '+@TempTable+'" queryout "'+@TempFile+'" -T -c'
Exec master..xp_cmdshell @bcpCommand


This is urgent.
LVL 4
batchakamalAsked:
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.

batchakamalAuthor Commented:
Also note that I want to use SQL Server trusted connection, and the statement will be executed local server.
Do I need to specify the servername, userid and password?
I think -T is for Trusted Connection. right???
0
Aneesh RetnakaranDatabase AdministratorCommented:
Set @TempTable = 'urDb.dbo.A' -----------------------------replace 'urDBB' with your database name
Set @TempFile = 'C:\tmp_Load_vou05nov06_txt'
SET @BcpCommand = 'BCP "select * from ' +@TempTable+ '" queryout "' +@TempFile+ '" -T  -c"'
0

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
batchakamalAuthor Commented:
I am getting the same error.

Declare @BcpCommand varchar(200)
Declare @TempTable varchar(200)
Declare @TempFile varchar(200)
Set @TempTable = 'SalesDB.dbo.tmp_Load_vou05nov06_txt' --- I have included the database name too
Set @TempFile = 'C:\tmp_Load_vou05nov06.txt'
SET @BcpCommand = 'BCP "select * from ' +@TempTable+ '" queryout "' +@TempFile+ '" -T  -c"'
Exec master..xp_cmdshell @bcpCommand

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Aneesh RetnakaranDatabase AdministratorCommented:
If you are using the sql authentication try this


SET @BcpCommand = 'BCP "select * from ' +@TempTable+ '" queryout "' +@TempFile+ '" -U login_id -P password   /c"'
replace Login_id and password to appropriate username and password ..

If it again fails, pls send the error message, whichwill be useful for further assistance
0
batchakamalAuthor Commented:
Declare @BcpCommand varchar(200)
Declare @TempTable varchar(200)
Declare @TempFile varchar(200)
Set @TempTable = 'ETL2..tmp_Load_vou05nov06_txt'
Set @TempFile = 'C:\tmp_Load_vou05nov06.txt'
SET @BcpCommand = 'BCP "select * from ' +@TempTable+ '" queryout "' +@TempFile+'"  -U sa -P sa /c'
Exec master..xp_cmdshell @bcpCommand

Error Message:

Copy direction must be either 'in' or 'out'.
Syntax Error in 'queryout'.
0
Aneesh RetnakaranDatabase AdministratorCommented:
Declare @BcpCommand varchar(200)
Declare @TempTable varchar(200)
Declare @TempFile varchar(200)
Set @TempTable = 'pubs.dbo.authors' --- I have included the database name too
Set @TempFile = 'C:\tmp_Load_vou05nov06.txt'
Set @BcpCommand = 'bcp "select * from '+@TempTable+'" queryout "'+@TempFile+'" -U sa -P sa /c'

print @bcpCommand
Exec master..xp_cmdshell @bcpCommand

run this without changing anything,..

also put a print statement before the exec statement of the prev query and send me ...
0
batchakamalAuthor Commented:
Print Statement returns:

bcp "select * from pubs.dbo.authors" queryout "C:\tmp_Load_vou05nov06.txt" -U sa -P sa /c
                                                                                 
and the error is,

Copy direction must be either 'in' or 'out'.
Syntax Error in 'queryout'.
usage: bcp [[db_name.]owner.]table_name[:slice_num] [partition pname] {in | out} [filename]
...........
...........
.........
0
batchakamalAuthor Commented:
Also I have tried the above statement with "out queryout" instead of "queryout"

Then print statement returns,

bcp "select * from pubs.dbo.authors" out queryout "C:\tmp_Load_vou05nov06.txt" -U sa -P sa /c

--------------------------------------------^--------------------I have included the out
and the error message shows,

Syntax Error in 'C:\tmp_Load_vou05nov06.txt'.
0
batchakamalAuthor Commented:
Hello..

I found the problem and solved it.

The problem is , I have SQL Server 2005 on my pc. So it tries to execute the bcp of that.

Now it is running fine.

thanks for ur support and I am giving this point to you.

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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.