0

So, I am playing around with BCP to get to know it better, and ran into an issue I am having trouble figuring out. I am getting an error, "The Syntax of the command is incorrect".

I have tested the complete file path which is building correctly, and the select query by itself, which is completing correctly. Any thoughts?

The code I am using is:

DECLARE @fileName varchar(128)
DECLARE @filePath varchar(128)
DECLARE @completeFilePath varchar(128)
DECLARE @sqlCmd varchar(4000)
DECLARE @BCPSwitches VARCHAR(64)
SET @filePath = 'xxxxxx'

SELECT TOP 5 [EMP]
  ,[SSNO]
  ,[NAME]
  ,[STREET]
INTO #ParticipantIdentifiers
FROM xxxxxxxxx

SET @BCPSwitches = '-w -T -t |'
SET @fileName = 'xxxxx.txt'
SET @completeFilePath = @filePath + @fileName
SET @sqlCmd = 'bcp "SELECT * FROM #ParticipantIdentifiers" queryout "'+ @completeFilePath + '" ' + @BCPSwitches 

EXEC DemoDB..xp_cmdshell @sqlCmd
6
  • Could you post what is generated in the @sqlCmd variable? Commented Jan 2, 2016 at 20:23
  • If you are running this script from SQLCMD or from SSMS you can just put a "SELECT @sqlCmd" before the EXEC and it should show up in the results window or console. Commented Jan 2, 2016 at 20:37
  • bcp "SELECT * FROM #ParticipantIdentifiers" queryout "<my personal path>\NonEmployee_EEID.txt" -w -T -t | Commented Jan 2, 2016 at 20:37
  • 1
    I think the terminator "|" after -t needs to be enclosed in quotes as well since the shell might be interpreting this as a output pipe.. Commented Jan 2, 2016 at 20:41
  • 1
    You are a genius my friend :) Thanks! If you want to post that as the answer, I'll mark it. Commented Jan 2, 2016 at 21:01

1 Answer 1

1

I think the terminator "|" after -t (in the @BCPSwitches) needs to be enclosed in quotes as well since the shell might be interpreting this as a output pipe.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.