I have a table returning me a series of boolean(0/1) paired with an operator (and/or) as follow
e.g.
| ID | Operator | Value |
|---|---|---|
| 1 | and | 0 |
| 2 | and | 0 |
| 3 | or | 1 |
I am trying to find an efficient way to parse these rows into a single result that would be equivalent to
(false and false or true) //returning true
I think this would be possible with CLR, but it appear to be something I could do in regular T-SQL. I've thought about scalar function receiving a table variable. Would this be my best course of action at this stage?
The table will never be very complex and always start with an AND operator which can be ignored.
As pointed out in comment, I was going to do this with a simple AND and if I have any OR it would shortcut the entire thing, but I am doubting my own self as when I tried this in JS it doesn't appear to be the case: https://jsfiddle.net/3ctshxae/
Edit: I incorrectly stated I would process them in as a stack since that is what made sense to me initially. I never thought about orders of operators without brackets as one would simply never write them that way when programming. I indeed would like to respect precedence rules of Boolean logic of ANDs taking precedence.
I know I could do this in a CLR and simply throw the logic in it and get the returned value, but I was hoping to avoid stepping outside of T-SQL.
ANDs be applied beforeORs or they should be processed in a stack manner?andprecedesor" I can't see a way to do this without recursion, and I think that's never going to perform as well as a simplewhileloop. Can't believe I've just said that.