Skip to content
Advertisement

Sql Server Global Blocked List and Item specific Allow List

This seems easy at first but after thinking about it for the day I’m stuck.

Imagine you have a global block list for veggies.

This table says these veggies are never allowed at dinner.

However, there is also an override table for a specific child that says for this child ignore the global block and allow the veggie.

Here is what the query looks like:

How can I cancel out the first query where I find a hit in the second query? Put another way I would want an empty result set returned.

I can’t combine them because I could have this scenario…

Here there is no childname so there is not override of the global block.

Advertisement

Answer

I think we should use EXCEPT here, rather than UNION.

If a @childname is NULL, the second part of the query would return no rows, because a.childname = NULL is false, and the overall query will return all rows from blocked_veggie.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement