Skip to content

Instantly share code, notes, and snippets.

@DaveHogan
Created September 3, 2013 10:05
Show Gist options
  • Select an option

  • Save DaveHogan/6421973 to your computer and use it in GitHub Desktop.

Select an option

Save DaveHogan/6421973 to your computer and use it in GitHub Desktop.
How to select from different tables in a VIEW based on a config value / IF condition
-- Sneaky way to select from a specific table using a config value
-- Views do not support IF or anything else useful
-- Obviously the table schemas need to match otherwise the UNION will fail
SELECT t.* FROM ATable t
INNER JOIN Config c on 'ConfigKeyToMatch' = c.ConfigKey
WHERE c.ConfigValue = 'True'
UNION
SELECT t.* FROM DifferentTable t
INNER JOIN Config c on 'ConfigKeyToMatch' = c.ConfigKey
WHERE c.ConfigValue = 'False'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment