【问题标题】:Error handling or Exception handling in NetezzaNetezza 中的错误处理或异常处理
【发布时间】:2013-12-03 20:19:33
【问题描述】:

我正在运行一个 netezza sql 进程作为 shell 脚本的一部分,并且在其中一个 sql 代码中,如果来自 2 个不同表的行数不匹配,我希望它引发错误或异常。

SQL 代码:

/*  The following 2 tables should return the same number of rows to make sure the process is correct */

select              count(*) 
from                (
                select distinct col1, col2,col3 
                from table_a
                where  week > 0 and rec >= 1
                ) as x ;


select              count(*) 
from                (
                select distinct col1, col2, col3
                from table_b
                ) as y ;

如何比较 2 行计数并在 netezza SQL 进程中引发异常/错误,以便在 2 行计数不相等时退出进程?

【问题讨论】:

    标签: sql netezza


    【解决方案1】:

    我同意脚本是最好的选择。但是,您仍然可以使用交叉连接来检查您的 SQL 本身

    Select a.*
    from Next_Step_table a cross join
    (select case when y.y_cnt is null then 'No Match' else 'Match' end as match
    from (select count(*) as x_cnt
    from  ( select distinct col1, col2,col3 
            from table_a
            where  week > 0 and rec >= 1
           )) x left outer join
    (select count(*) as y_cnt
    from  (select distinct col1, col2, col3
           from table_b
           )) y  on x.x_cnt=y.y_cnt) match_tbl
    where match_tbl.match='Match'
    

    【讨论】:

      【解决方案2】:

      我猜这里最好的解决方案是在脚本中执行此操作。
      即将 count(*) 的结果存储在变量中,然后比较它们。 nzsql 有命令行选项,只返回单个查询的结果数据。

      如果必须用普通的 SQL 来完成,一个可怕的、可怕的组合就是使用除以零。它很难看,但我以前在测试东西时使用过它。在我的脑海中浮现:

      with 
      subq_x as select count(*) c1 .... ,
      subq_y as select count(*) c2 ...
      select (case when (subq_x.c1 != subq_y.c1) then 1/0 else 1 end) counts_match;
      

      我有没有提到这很丑?

      【讨论】:

      • 是的。而是在 shell 脚本中执行此操作。 SQL 用于执行查询,而不是控制流。
      猜你喜欢
      • 2012-09-15
      • 2011-04-03
      • 2016-04-11
      • 2015-03-29
      • 2011-07-14
      • 1970-01-01
      • 2014-01-08
      • 2014-04-06
      • 2013-12-27
      相关资源
      最近更新 更多