I want to know if we can do an intersect conditional. theres is somes query, but the result is wrong (always empty). I write what it should result.
DECLARE @CAN_USE_TABLE1 BIT
DECLARE @CAN_USE_TABLE2 BIT
DECLARE @CAN_USE_TABLE3 BIT
DECLARE @CAN_USE_TABLE4 BIT
DECLARE @TABLE1 AS TABLE ( ABC INT ) -- values will be 1,2,3
DECLARE @TABLE2 AS TABLE ( ABC INT ) -- values will be 1,2
DECLARE @TABLE3 AS TABLE ( ABC INT ) --EMPTY TABLE
DECLARE @TABLE4 AS TABLE ( ABC INT ) --EMPTY TABLE
INSERT INTO @TABLE1 VALUES (1)
INSERT INTO @TABLE1 VALUES (2)
INSERT INTO @TABLE1 VALUES (3)
INSERT INTO @TABLE2 VALUES (1)
INSERT INTO @TABLE2 VALUES (2)
SET @CAN_USE_TABLE1 = 1
SET @CAN_USE_TABLE2 = 1
SET @CAN_USE_TABLE3 = 1
SET @CAN_USE_TABLE4 = 0
SELECT ABC FROM @TABLE1 WHERE @CAN_USE_TABLE1 = 1
INTERSECT
SELECT ABC FROM @TABLE2 WHERE @CAN_USE_TABLE2 = 1
INTERSECT
SELECT ABC FROM @TABLE3 WHERE @CAN_USE_TABLE3 = 1
INTERSECT
SELECT ABC FROM @TABLE4 WHERE @CAN_USE_TABLE4 = 1
--RESULT SHOULD BE :
-- NO RESULT
--
-- BECAUSE, AT THIS STAGE, TABLE1 AND TABLE2 AND TABLE3 SHOULD BE INTERSECTED. AND BECAUSE TABLE3 IS EMPTY, THE RESULT IS EMPTY.
SET @CAN_USE_TABLE1 = 1
SET @CAN_USE_TABLE2 = 1
SET @CAN_USE_TABLE3 = 0
SET @CAN_USE_TABLE4 = 0
SELECT ABC FROM @TABLE1 WHERE @CAN_USE_TABLE1 = 1
INTERSECT
SELECT ABC FROM @TABLE2 WHERE @CAN_USE_TABLE2 = 1
INTERSECT
SELECT ABC FROM @TABLE3 WHERE @CAN_USE_TABLE3 = 1
INTERSECT
SELECT ABC FROM @TABLE4 WHERE @CAN_USE_TABLE4 = 1
--RESULT SHOULD BE :
-- 1
-- 2
--
-- BECAUSE, AT THIS STAGE, TABLE1 AND TABLE2 SHOULD BE INTERSECTED
SET @CAN_USE_TABLE1 = 0
SET @CAN_USE_TABLE2 = 1
SET @CAN_USE_TABLE3 = 0
SET @CAN_USE_TABLE4 = 0
SELECT ABC FROM @TABLE1 WHERE @CAN_USE_TABLE1 = 1
INTERSECT
SELECT ABC FROM @TABLE2 WHERE @CAN_USE_TABLE2 = 1
INTERSECT
SELECT ABC FROM @TABLE3 WHERE @CAN_USE_TABLE3 = 1
INTERSECT
SELECT ABC FROM @TABLE4 WHERE @CAN_USE_TABLE4 = 1
--RESULT SHOULD BE :
-- 1
-- 2
--
-- BECAUSE, AT THIS STAGE, ONLY TABLE 2 SHOULD BE USED