here am facing problem to pass multivalues parameter to bcp query.
In my scenario parameters are
@CustomFieldsHeaders ='CS Check','CS Date','CS Drop','CS Radio','RSP Text','RSP Date','RSP Check','RSP radio','RSP Drp','DOB'
I need to pass the above as a single parameter.
Following is my query for reference, please help
DECLARE @CustomFieldsHeaders AS VARCHAR(MAX)
SELECT @CustomFieldsHeaders = COALESCE(@CustomFieldsHeaders+',' ,'') + ''''+CAST(FieldName as varchar(max))+'''' FROM ManageFields
PRINT @CustomFieldsHeaders
DECLARE @CustFieldNames VARCHAR(MAX)
SELECT @CustFieldNames =''+ COALESCE(@CustFieldNames+',' ,'') + CAST(ContactFieldName as varchar(max))+''
from ManageFields
print @CustFieldNames
DECLARE @SQL AS VARCHAR(8000)
DECLARE @StoredProcedure AS VARCHAR(8000)
SET @StoredProcedure=N'"EXEC QA31_28thNov2013.dbo.GetAllRecsBasedOnListIds_BCPTest_1 117, NULL, '+@CustFieldNames+', '''+@CustomFieldsHeaders+''' " '
SET @SQL = N'bcp '+@StoredProcedure + ' queryout '+'D:\BCP_WEBREPORT\QA_v1_23rdSep2013\BounceReport\12-07-13\TESTREPORT13.CSV' +' -c -t, -S PSPL-06 -Usa -Psa!2013'
PRINT @SQL
EXEC MASTER..XP_CMDSHELL @SQL
You need to escape the 's in the string? you can escape a ' with ''. There's a function that will do it for you called QUOTENAME() There's also a similar question with some solutions here.