满足复杂条件然后做点什么

时间:2018-12-08 03:02:36

标签: sql oracle stored-procedures plsql

我有三种情况,如果所有人都满意,那就做些事。

  1. select count(*) from schema.member where condition1为0
  2. select id from schema.ta a, schema.b where a.id=b.id and a.id !='input_id' and a.id in ('M','N');应该为空或不存在。
  3. select member from schema.tc, x schema.tb where condition3;它也应该为null或不存在。

因此,基本上,如果所有3种情况都得到满足,那么我会做些事情。

我想使用存储过程来做到这一点。我的主意是 从每个方案的计数总和中获得一个整数;如果为0,则表示全部满足。

select count(id) from schema.member where condition1 + select count(id) from ... where condition2 + select count(member) from ... where condition3 = 0

不确定最好的方法是什么,脚本帮助将受到赞赏。

1 个答案:

答案 0 :(得分:0)

您可以将它们全部放入NOT EXISTS支票中

DECLARE
     l_flag   INTEGER;
BEGIN
     SELECT
          CASE
               WHEN NOT EXISTS (
                    SELECT 1
                    FROM schema.member
                    WHERE condition1 = 'somecondition'
               ) AND NOT EXISTS (
                    SELECT 1
                    FROM schema.ta a
                    JOIN schema.b ON a.id = b.id --use proper join syntax
                             WHERE a.id != 'input_id' AND a.id IN ('M','N') 
                             AND a.id IS NOT NULL -- no nulls as you said.
               ) AND NOT EXISTS (
                    SELECT 1
                    FROM schema.tc
                    JOIN schema.tb ON condition3 = '<somecondition>'
                    WHERE member IS NOT NULL -- no nulls as you said.
               ) THEN 1
               ELSE 0
          END
     INTO l_flag
     FROM dual;

     IF
          l_flag = 1
     THEN
          do_something;
     END IF;
END;
/

我已经显示了一个匿名块。您可以在过程中编写的内容相同。