I have three tables:
-
Subscriptions
SubscriptionID
SubscriptionName
Inherits (can be NULL) -
SubscriptionOptions
SubscriptionOptionID
SubscriptionID
OptionID
OptionValue -
Options
OptionID
OptionDefaultValue
Description
I need to pass in a query with multiple options and a subscription ID, something along these lines:
SELECT optionid, optionvalue WHERE subscriptionid = x AND options IN (a, b, c, d, e, f)
Only I need to implement the inherits by logic via a recursive call like this:
if subscriptionoption exists for subscription id & optionid
use subscriptionoption.optionvalue in the row
else
if inherits is not NULL
call this function using inherited subscription id
else
use options.optiondefaultvalue for that optionid
To do it in SQL, I think you need to incorporate a hierarchical query. Here’s a swing at it, but I haven’t been able to test it out.
Another approach would be to write a function that implements the recursive logic for a single subscriptionID and optionID, then call it like this:
The function could be something like:
or could be written to use a loop instead of recursion.