【问题标题】:Satisfied complicated condition then doing something满足复杂的条件然后做某事
【发布时间】:2018-12-08 03:02:35
【问题描述】:

我有三种情况,如果都满意就做点什么。

  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'); 它应该为 null 或不存在。
  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

不确定最好的方法是什么,感谢脚本帮助。

【问题讨论】:

  • 你的问题很不清楚,涉及到几个表。您能发布示例数据和预期输出吗?

标签: sql oracle stored-procedures plsql


【解决方案1】:

您可以将它们全部放入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;
/

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

【讨论】:

  • 谢谢,我想从存储过程中返回1_flag。我想在我的 WCF 代码中使用该值(我会将其从 sql 转换为 C#)。如果为 0 则做某事。那么我们可以在存储过程中返回它吗?我不想使用函数。
  • @Bigeyes:不客气。我不了解 C# ,但您可以创建一个过程并将其作为输出变量传递,在我的代码中使用 begin..end 的内容。查看this example 以了解如何使用 OUT 参数。如果您在使用适当的 c# 标记的代码时遇到困难,您可能会问一个单独的问题。我希望我回答了你原来的问题。请考虑接受它,因为它也会对其他人有所帮助。
  • 我将其标记为答案。但是缺少任何右括号吗?
猜你喜欢
  • 1970-01-01
  • 2018-05-10
  • 1970-01-01
  • 2022-11-27
  • 1970-01-01
  • 2016-05-18
  • 2019-08-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多