Netezza中的错误处理或异常处理

时间:2013-11-07 22:02:16

标签: sql netezza

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

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行计数不相等的情况下退出进程?

2 个答案:

答案 0 :(得分:1)

我猜这里最好的解决办法是在剧本中做到这一点 即将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;

我提到这是丑陋的吗?

答案 1 :(得分: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'