0

I understand that this has been asked many times and this is the syntax i use

EXECUTE master.dbo.Xp_cmdshell 'bcp "select 'I_CLM_ID', 'I_CHARGES' union all SELECT top 100 cast(I_CLM_ID as varchar(25)), cast(I_CHARGES as varchar(10)) FROM BRS_ANALYZE_MILLION.dbo.APC_ANALYZE_LINE_PRICED_150k_2" queryout "F:\projects\0113_Jan14\OUTPUT\Test.txt" -t"|" -c -T' 

i get an error Incorrect syntax near 'I_CLM_ID'.

i run the select statement within the double quotes and it gives the result query with 101 records but when i run everything from bcp to the last -T it gives me another error

Msg 103, Level 15, State 4, Line 1

The identifier that starts with 'select 'I_CLM_ID', 'I_CHARGES' union all SELECT top 100 cast(I_CLM_ID as varchar(25)), cast(I_CHARGES as varchar(10)) FROM BR' is too long. Maximum length is 128. Msg 102, Level 15, State 1, Line 4 Incorrect syntax near 'queryout'.

what am i doing wrong

i know to do SSIS Import/Export Wizard but wanted to do i through Bcp

user176047
  • 311
  • 4
  • 20
  • You need to escape the single quotes in the query (i.e. inside the double quotes) by repeating them. See [here](http://stackoverflow.com/questions/1586560/how-do-i-escape-a-single-quote-in-sqlserver). That should fix the `Incorrect syntax` error, then see what you're left with. – aucuparia May 20 '14 at 16:46
  • @aucuparia - Thanks for your reply but i don't think it is the single quotes because when i run everything within the double quotes after bcp it does return me with the 101 rows which is what i expect – user176047 May 20 '14 at 17:28
  • @aucupuria - i am sorry. i did what you said and it worked. i escaped the single quote by adding another single quote – user176047 May 20 '14 at 18:05

0 Answers0