I have the following @temp table :
SELECT *
FROM @temp
WHERE LinkingID = 1
------------------------------------------------------------------
|Condition_ID LinkingID Operator Variable1 Variable2 |
| 1 1 == 25 2 |
| 2 1 <> 3 7 |
| 3 1 == 4 4 |
------------------------------------------------------------------
I want to implement a CASE ( ??? ) that will check if Variable1 OPERATOR Variable2 is true, make the check for all rows and IF all rows are true then do something, else break off and abort.
I was thinking something like this ( I’m not sure how to express this in T-SQL so I use pseudolanguage ) :
CASE
WHEN Operator LIKE '=='
THEN IF Variable1 == Variable2 THEN TRUE
ELSE THEN FALSE
WHEN Operator LIKE '<>'
THEN IF Variable1 <> Variable2 THEN TRUE
ELSE THEN FALSE
FROM @temp
If the result of this is true for all rows, then do something, else do something else
In the scenario above :
Row1 would return FALSE
Row2 would return TRUE
Row3 would return TRUE
Hence the result is FALSE.
Accumulating comparison results over the whole table
Checking that the whole table returns true for all rows is best done to count false ones and check whether that number is greater equal to 0. If it’s 0 then it means that all rows matched otherwise number indicates number of rows that failed
Mind the inverted comparison compared to
Operatorvalue to catch negatives:Here’s also a SQLFiddle that’s based on upper code.