Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Interesting Query Problem
Message
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
01518190
Message ID:
01518200
Views:
42
>>>>except the WHERE clauses come from the CompassQueries table for all rows that match the campaign.
>>>>
>>>>How do I do this?
>>>
>>>
>>>You will need dynamic SQL here, e.g.
>>>
>>>declare @SQL nvarchar(max)
>>>
>>>select @SQL = coalesce(@SQL + '
>>>UNION ALL ','') + 'SELECT VoterID from tblCamp_CT WHERE ' 
>>>+ Query FROM CompassQueries
>>>
>>>print @SQL
>>>
>>>execute (@SQL)
>>
>>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
--execute (@SQL)
The @SQL variable now contains the following (a bit long)
SELECT VoterID from tblCamp_CT WHERE [Assembly]='151' AND [Party]='R' AND [Propensity]='4', [Assembly]='151' AND [Party]
='R' AND [Propensity]='4', [Assembly]='151' AND [Party]='R' AND [Propensity]='4', [Assembly]='151' AND [Party]='R' AND 
[Propensity]='3', [Assembly]='151' AND [Party]='R' AND [Propensity]='3', [Assembly]='151' AND [Party]='R' AND [Propensity]='2', 
[Assembly]='151' AND [Party]='R' AND [Propensity]='2', [Assembly]='151' AND [Sex]='M', [Assembly]='151' AND [Sex]='M', 
[Assembly]='151' AND [Sex]='F', [Assembly]='151' AND [Household_Mems]='2', [Assembly]='151' AND [Household_Mems]='2', 
[Assembly]='151' AND [Household_Mems]='4', [Assembly]='151' AND [Household_Mems]='4', [Assembly]='151' AND [Party]='D' AND 
[Propensity]='1', [Assembly]='151' AND [Party]='D' AND [Propensity]='1', [Assembly]='151' AND [Party]='D' AND [Propensity]='1', 
[Sex]='f' AND [Age] BETWEEN '50' AND '100', [Sex]='f' AND [Age] BETWEEN '50' AND '100', [Sex]='F' AND [Age] BETWEEN '50' 
AND '100', [Sex]='F' AND [Age] BETWEEN '50' AND '100', [Sex]='F' AND [Age] BETWEEN '2' AND '100', [Sex]='F' AND [Age] 
BETWEEN '2' AND '100', [Assembly]='151' AND [Sex]='F' AND [Age] BETWEEN '50' AND '100', [Sex]='F', [Sex]='F' AND [Age]='50', 
[Age] BETWEEN '50' AND '100', [Assembly]='99', ([Assembly]='151' AND [Party]='R' AND [Propensity]='3') OR ([Assembly]='151' 
AND [Party]='D' AND [Propensity]='1'), [Assembly]='151' AND [Sex]='F' AND [Party]='R', [Sex]='M' AND [Party]='R' AND 
[Household_Mems]='2', [Sex]='M' AND [Party]='R' AND [Household_Mems]='4', [Sex]='M' AND [Party]='R' AND [Household_Mems]
='4', [Age] Between '50' AND '99', ([Propensity] Between '2' AND '4' AND [Party]='U' AND [Assembly]='99') OR ([Assembly]='99' AND 
[Party]='R' AND [Propensity] Between '2' AND '3') OR ([Propensity] Between '1' AND '2' AND [Party]='D' AND [Assembly]='99'), 
([Propensity] Between '2' AND '4' AND [Party]='U' AND [Assembly]='99') OR ([Assembly]='99' AND [Party]='R' AND [Propensity] 
Between '2' AND '3'), [Assembly]='99', [Assembly]='99' AND [Party]='R', [Assembly]='99' AND [Party]='R' AND [Propensity] 
Between '1' AND '3', ([Assembly]='99' AND [Party]='R' AND [Propensity] Between '1' AND '3') OR ([Assembly]='99' AND [Party]='U' 
AND [Propensity] Between '1' AND '4'), [Propensity]='61' AND [Propensity]='5', [Assembly]='61' AND [Party]='R', [Party]='R' AND 
[Assembly]='61', [Assembly]='32' AND [Party]='R' AND [Propensity] Between '2' AND '4', [Assembly]='32' AND [Party]='R' AND 
[Propensity] Between '2' AND '4' AND [Sex]='F', [Assembly]='32' AND [Party]='R' AND [Propensity] Between '2' AND '4' AND [Sex]='F' 
AND [Age] Between '21' AND '30', [Assembly]='32' AND [Party]='R' AND [Propensity] Between '2' AND '4' AND [Age]='f', [Assembly]
='99' AND [Propensity] Between '2' AND '4', [Assembly]='99' AND [Party]='r' AND [Propensity] Between '1' AND '4', ([Assembly]='99' 
AND [Party]='r' AND [Propensity] Between '1' AND '4') OR ([Assembly]='99' AND [Party]='u' AND [Propensity] Between '1' AND '4') OR 
([Assembly]='99'), [Assembly]='99' AND [Party]='r' AND [Propensity] Between '1' AND '4', ([Assembly]='99' AND [Party]='r' AND 
[Propensity] Between '1' AND '4') OR ([Assembly]='99' AND [Party]='u' AND [Propensity] Between '1' AND '4'), [State]='CT' AND 
[Assembly]='99' AND [Party]='R', (([State]='CT' AND [Assembly]='99') AND (([Party]='r') OR ([Party]='u' AND [Propensity] Between '1' 
AND '4'))), (([State]='CT' AND [Assembly]='99') AND (([Party]='r') OR ([Party]='u' AND [Propensity] Between '1' AND '4') OR ([Party]='d' 
AND [Propensity] Between '1' AND '2'))), [State]='CT' AND [Assembly]='99' AND [Party]='r', [State]='CT' AND [Assembly]='99' AND 
[Party]='r' AND [Propensity] Between '1' AND '4', (([State]='CT' AND [Assembly]='99') AND (([Party]='r' AND [Propensity] Between '1' 
AND '4') OR ([Party]='u' AND [Propensity] Between '1' AND '4'))), (([State]='CT' AND [Assembly]='99') AND (([Party]='r' AND 
[Propensity] Between '1' AND '4') OR ([Party]='u' AND [Propensity] Between '1' AND '4') OR ([Party]='d' AND [Prope
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?
Everything makes sense in someone's mind
public class SystemCrasher :ICrashable
In addition, an integer field is not for irrational people
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform