Sql 如何将表中存储的符号传递给存储过程条件?

Sql 如何将表中存储的符号传递给存储过程条件?,sql,oracle,Sql,Oracle,我们有如下两个表格: 表1: 表2: 结果如下所示: call c1 c2 STATUS ---- -- -- ------ C001 a 1 FAIL C001 b 12 PASS 请帮忙 您可以使用另一级别的大小写表达式来决定使用哪个符号: select t1a.call, t1a.c1, t1a.c2, t1b.c2, t1a.c2 - t1b.c2 as diff, t2.symbol, t2.limit, case t2.symbol when '>' t

我们有如下两个表格:

表1:

表2:

结果如下所示:

call c1 c2 STATUS
---- -- -- ------
C001 a  1  FAIL
C001 b  12 PASS

请帮忙

您可以使用另一级别的大小写表达式来决定使用哪个符号:

select t1a.call, t1a.c1, t1a.c2, t1b.c2,
  t1a.c2 - t1b.c2 as diff, t2.symbol, t2.limit,
  case t2.symbol
    when '>' then
      case when t1a.c2-t1b.c2 > t2.limit then 'PASS' else 'FAIL' end
    when '<' then
      case when t1a.c2-t1b.c2 < t2.limit then 'PASS' else 'FAIL' end
    when '=' then
      case when t1a.c2-t1b.c2 = t2.limit then 'PASS' else 'FAIL' end
  end as status
from table1 t1a
join table1 t1b on t1b.call = t1a.call and t1b.c1 = t1a.c1 and t1b.c2 < t1a.c2
join table2 t2 on t2.call = t1a.call
order by t1a.call, t1b.c1;

CALL C1 C2 C2       DIFF S      LIMIT STATUS
---- -- -- -- ---------- - ---------- ------
c001 a  11 10          1 >         10 FAIL  
c001 b  25 13         12 >         10 PASS  
但是有大量的重复,所以如果你有超过几个符号要处理,它将不能很好地扩展,至少在可维护性方面

作为一种更一般的方法,如果有点模糊,您可以通过XML类型进行一种动态评估:

select t1a.call, t1a.c1, t1a.c2, t1b.c2,
  t1a.c2 - t1b.c2 as diff, t2.symbol, t2.limit,
  xmlquery('/ROWSET/ROW/*/text()'
  passing xmltype(
    dbms_xmlgen.getxml('select case when ' || (t1a.c2-t1b.c2) || t2.symbol || t2.limit
      || ' then ''PASS'' else ''FAIL'' end from dual')
  )
  returning content) as status
from table1 t1a
join table1 t1b on t1b.call = t1a.call and t1b.c1 = t1a.c1 and t1b.c2 < t1a.c2
join table2 t2 on t2.call = t1a.call
order by t1a.call, t1b.c1;

CALL C1 C2 C2       DIFF S      LIMIT STATUS
---- -- -- -- ---------- - ---------- ------
c001 a  11 10          1 >         10 FAIL  
c001 b  25 13         12 >         10 PASS  
或者您可以使用动态SQL,构建一个类似的字符串来运行和计算

无论哪种方式,这都是痛苦和尴尬的,因此您可能需要寻找其他方式来捕获业务规则

我还不得不猜测,你可以根据一行的大小来决定从另一行减去哪一行。但这也假设它们不能相等-如果它们可以相等,那么连接条件将根本找不到它们,如果更改连接条件,它将使行与其自身匹配,因此您还必须检查不同的行ID


实现这一点的唯一方法是使用动态SQL。您的服务器实例允许这样做吗?是的。但我不知道如何做到这一点。这是一个旧的,现有的生产系统的地方,还是你正在开发一些新的?或者这只是为了练习?存储业务逻辑是一种死脑筋的方式;如果你不得不忍受15年前别人创造的东西,那是一回事,但如果这是一个新系统,那就做些别的。如果只是为了练习,那就练习别的东西;练习做一些愚蠢的事情是你能做的最糟糕的练习。这是一个新的系统。看起来这是可行的,但仍然不行。想试试看25多年前,在ANSI-92 SQL标准中,旧样式的逗号分隔表列表样式被正确的ANSI连接语法所取代,因此不鼓励使用它。。第一个问题帮了我的忙-
call c1 c2 STATUS
---- -- -- ------
C001 a  1  FAIL
C001 b  12 PASS
select t1a.call, t1a.c1, t1a.c2, t1b.c2,
  t1a.c2 - t1b.c2 as diff, t2.symbol, t2.limit,
  case t2.symbol
    when '>' then
      case when t1a.c2-t1b.c2 > t2.limit then 'PASS' else 'FAIL' end
    when '<' then
      case when t1a.c2-t1b.c2 < t2.limit then 'PASS' else 'FAIL' end
    when '=' then
      case when t1a.c2-t1b.c2 = t2.limit then 'PASS' else 'FAIL' end
  end as status
from table1 t1a
join table1 t1b on t1b.call = t1a.call and t1b.c1 = t1a.c1 and t1b.c2 < t1a.c2
join table2 t2 on t2.call = t1a.call
order by t1a.call, t1b.c1;

CALL C1 C2 C2       DIFF S      LIMIT STATUS
---- -- -- -- ---------- - ---------- ------
c001 a  11 10          1 >         10 FAIL  
c001 b  25 13         12 >         10 PASS  
select t1a.call, t1a.c1, t1a.c2, t1b.c2,
  t1a.c2 - t1b.c2 as diff, t2.symbol, t2.limit,
  xmlquery('/ROWSET/ROW/*/text()'
  passing xmltype(
    dbms_xmlgen.getxml('select case when ' || (t1a.c2-t1b.c2) || t2.symbol || t2.limit
      || ' then ''PASS'' else ''FAIL'' end from dual')
  )
  returning content) as status
from table1 t1a
join table1 t1b on t1b.call = t1a.call and t1b.c1 = t1a.c1 and t1b.c2 < t1a.c2
join table2 t2 on t2.call = t1a.call
order by t1a.call, t1b.c1;

CALL C1 C2 C2       DIFF S      LIMIT STATUS
---- -- -- -- ---------- - ---------- ------
c001 a  11 10          1 >         10 FAIL  
c001 b  25 13         12 >         10 PASS