简体   繁体   中英

BCP utility error

select @sql='select * from application.dbo.'+@TableName1+''+char(13)+char(10)+'except'+char(13)+char(10)+'select * from '+'application_check'+'.dbo.'+@TableName1+''

exec sp_executesql  @sql

select @script='bcp sp_executesql  '+@sql+'queryout "D:\SNB\Test\datacheck.txt" -T -c -t "|"'

EXEC master..xp_cmdshell @script

Error: Copy direction must be either 'in', 'out' or 'format' while trying to export output into a file

A couple of fixes:

  1. removed all the unnecessary concatenation from the sql query.
  2. The SQL Query must be wrapped inside double quotes "Sql Query"
  3. Execute sp_executesql not required.

Solution:

select @sql='select * from application.dbo.'+ QUOTENAME(@TableName1)
           +'  except  select * from application_check.dbo.'+ QUOTENAME(@TableName1)

SET @script='bcp "'+@sql+'" queryout "D:\SNB\Test\datacheck.txt" -T -c -t "|"'

EXEC master..xp_cmdshell @script
select @sql='select * from application.dbo.'+@TableName1+''+char(13)+char(10)+'except'+char(13)+char(10)+'select * from '+'application_check'+'.dbo.'+@TableName1+''

-- exec sp_executesql  @sql

select @script='bcp "'+@sql+'" queryout "D:\SNB\Test\datacheck.txt" -T -c -t "|"'

EXEC master..xp_cmdshell @script

You need not use sp_executesql . You can use the query itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM