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:
01518191
Views:
58
>My client has a table called CompassQueries which stores WHERE clauses:
>
>
>ID            CampaignId   Query
>----------------------------------------------------------------------------------------------------------------------------
>425         140               [Assembly]='151' AND [Party]='R' AND [Propensity]='4'
>429         140               [Assembly]='151' AND [Party]='R' AND [Propensity]='3'
>430         140               [Assembly]='151' AND [Party]='R' AND [Propensity]='2'
>
>
>So I now need to run a query that returns a DS of voters who match these queryies:
>
>It would be the same as running this:
>
>SELECT VoterID FROM tblCamp_CT WHERE  [Assembly]='151' AND [Party]='R' AND [Propensity]='2'
>
>
>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)
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform