Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8000453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:48:00+00:00 2026-06-04T15:48:00+00:00

I want to know if we can do an intersect conditional. theres is somes

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T15:48:01+00:00Added an answer on June 4, 2026 at 3:48 pm

    An empty set INTERSECTed with any other set will always be empty. It’s like multiplying by 0. You always get 0.

    Conditional INTERSECTing will require either a dynamic query, or a staging table, like this:

    Initializations

    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 )
    DECLARE @TABLE2 AS TABLE ( ABC INT )
    DECLARE @TABLE3 AS TABLE ( ABC INT )
    DECLARE @TABLE4 AS TABLE ( ABC INT )
    DECLARE @RESULT AS TABLE ( ABC INT ) --Adding this result 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 = 0
    SET @CAN_USE_TABLE4 = 0
    

    Processing

    INSERT INTO @RESULT
    SELECT ABC FROM @TABLE1 WHERE @CAN_USE_TABLE1=1 UNION
    SELECT ABC FROM @TABLE2 WHERE @CAN_USE_TABLE2=1 UNION
    SELECT ABC FROM @TABLE3 WHERE @CAN_USE_TABLE3=1 UNION
    SELECT ABC FROM @TABLE4 WHERE @CAN_USE_TABLE4=1
    
    DELETE r FROM @RESULT r
    WHERE NOT EXISTS(SELECT 1 FROM @TABLE1 WHERE ABC=r.ABC)
    AND @CAN_USE_TABLE1=1;
    
    DELETE r FROM @RESULT r
    WHERE NOT EXISTS(SELECT 1 FROM @TABLE2 WHERE ABC=r.ABC)
    AND @CAN_USE_TABLE2=1;
    
    DELETE r FROM @RESULT r
    WHERE NOT EXISTS(SELECT 1 FROM @TABLE3 WHERE ABC=r.ABC)
    AND @CAN_USE_TABLE3=1;
    
    DELETE r FROM @RESULT r
    WHERE NOT EXISTS(SELECT 1 FROM @TABLE4 WHERE ABC=r.ABC)
    AND @CAN_USE_TABLE4=1;
    
    SELECT * FROM @RESULT;
    

    Result

    1
    2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to know how can we know the other users of Oracle 10g
I want to know how can I perform VS2008 load test from five different
I want to know how can i fill only certain columns while filling the
I want to know how can I read attachment messages without using scriplets in
I want to know that can mouse click event detects colors or even strings.
I want to know i can do something similar to this (not working) code
I want to know how can i format a number like 2000000 into standard
I just want to know: How can I delete 'log.txt' if the last modification
I'm learning PLT Scheme and I want to know how can I build a
I am new to Xcode. I want to know how can I know all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.