English 中文(简体)
Sql 相交条件
原标题:Sql intersect conditional

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
最佳回答

空置的集 INTERSECT 与任何其他集一起的集将永远是空的。 它像乘以 0。 您总是得到 0 。

有条件 INTECT 需要动态查询或中转台,例如:

<强度 > 初始化 < /强 >

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

<强度 > 处理

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;

< 强 > 后继 < /强 >

1
2
问题回答
declare @sql nvarchar(4000),
    @params nvarchar(4000)

 if @can_use_table1 = 1
    select @sql=  select abc from @table1 

 if @can_use_table2 = 1
 begin
    if  @can_use_table1 = 1
       select @sql = @sql +   intersect  
    select @sql = @sql +  select abc from @table2 
 end

 if @can_use_table3 = 1
 begin
   if  @can_use_table1 = 1 or @can_use_table2 = 1
      select @sql= @sql +   intersect          
   select @sql= @sql +  select abc from @table3 
 end

 if @can_use_table4 = 1
 begin
   if  @can_use_table1 = 1 or @can_use_table2 = 1 or @can_use_table3 = 1
      select @sql= @sql +   intersect   
   select @sql= @sql +  select abc from @table4     
 end

select @params = @can_use_table1 bit, @can_use_table2 bit,
         @can_use_table3 bit, @can_use_table4 bit,
 @table1 table, @table2 table, @table3  table, @table4 table  

exec sp_executesql @sql,@params, 
       @can_use_table1,@can_use_table2, @can_use_table3, @can_use_table4,
       @table1, @table2, @table3, @table4 

您可以在单一的查询中做此操作 :

select ABC
from (SELECT ABC, @CAN_USE_TABLE1 as CanUse1, 0 as CanUse2, 0 as CanUse3, 0 as CanUse4
      FROM @TABLE1 
      union all
      SELECT ABC, 0, @CAN_USE_TABLE2, 0, 0 FROM @TABLE2
      union all
      SELECT ABC, 0, 0,  @CAN_USE_TABLE3, 0 FROM @TABLE3
      union all
      SELECT ABC, 0, 0, 0, @CAN_USE_TABLE4 FROM @TABLE4
     ) t
group by ABC
having max(Canuse1) = @CAN_USE_TABLE1 AND
       max(CanUse2) = @CAN_USE_TABLE2 AND
       max(CanUse3) = @CAN_USE_TABLE3 And
       max(CanUse4) = @CAN_USE_TABLE4




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签