Using BCP with queryout

Johnathan Simpson 586 Reputation points
2020-11-10T15:34:42.303+00:00

ms sql server - bcp question. I've got this code

Select @sql = 'bcp "Select * FROM [dbo].[TestingBCP] queryout'
exec master..xp_cmdshell @sql

but I get this error

Msg 214, Level 16, State 201, Procedure xp_cmdshell, Line 1 [Batch Start Line 0]
Procedure expects parameter 'command_string' of type 'varchar'.
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,621 questions
0 comments No comments
{count} votes

Accepted answer
  1. Guoxiong 8,206 Reputation points
    2020-11-10T15:57:40.987+00:00

    DECLARE @alenzi varchar(8000); -- Not varchar(max)
    SELECT @alenzi = 'bcp "SELECT * FROM [DATABASE_NAME].[dbo].[TestingBCP]" queryout "C:\TestingBCP.txt" -t, -c -T';
    EXEC master..xp_cmdshell @alenzi ;


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.