>>>Ok, I see what you're doing, excelt what's "Query"?
>>
>>Query is a field in the CompassQueries table.
>
>Ok, so I modified wht you posted...
>
>
>DECLARE @result varchar(max)
>SELECT @result = coalesce(@result + ', ', '') + CompassQuery
> FROM CompassQueries
> WHERE CampaignId = 140
>
>declare @SQL nvarchar(max)
>SET @SQL = 'SELECT VoterID from tblCamp_CT WHERE ' + @result
>print @SQL
>
>
>
>The @SQL variable now contains the following (a bit long)
>
>
>
>
>Notice at the end it's cut off. The string length is proably too long.
>
>Any other ideas? Any way to do with with a join?
Do you have more than 1 record in the CompassQuery that has CompaignID = 140?
Why you're not using what I posted - it should have lead to the correct query.
For your case try
>
>DECLARE @result nvarchar(max)
>SET @Result = (select top (1) CompassQuery
> FROM CompassQueries
> WHERE CampaignId = 140)
>
>declare @SQL nvarchar(max)
>SET @SQL = 'SELECT VoterID from tblCamp_CT WHERE ' + @result
>print @SQL
>
>
>
If it's not broken, fix it until it is.
My Blog