空值加载到Cursor?

时间:2013-08-27 13:30:33

标签: sql oracle validation plsql cursor

所以我创建了一些游标来存储正在加载到表中的数据。数据正在一次加载一个,我需要对整个数据集进行验证,而不仅仅是单个记录。所以会有这样的数据(但同样只会加载个人而不是整体)

form_no  |  type
---------------
5155-55  |  -1
5155-55  |  -1
5155-55  |   2
5155-55  |   1
         |
4568-98  |  -1
4568-98  |  -1
4568-98  |  -1
4568-98  |   2
4568-98  |   1   

3302-48  |  1
3302-45  |  2 
3302-45  | -1

然后,游标将获取总和或每种类型,然后将其与不同的form_no配对。然后在底部检查以确保每个form_no总是有1和-1(即form_no 5155-55至少有一个类型1和一个类型-1)

DECLARE
 error VARCHAR2(1) := 'N';

CURSOR XXX IS
SELECT form_no, sum(type) as sum_type_neg
  FROM my_table
 WHERE type = -1
 GROUP BY form_no;

CURSOR YYY IS
SELECT form_no, sum(type) as sum_type_pos
  FROM my_table
 WHERE type = 1
 GROUP BY form_no;

CURSOR ZZZ(xx_form_no VARCHAR2, yy_form_no VARCHAR2) IS
SELECT form_no
  FROM my_table
 WHERE form_no = xx_form_no
   AND form_no = vv_form_no
 GROUP BY form_no;

x_form  XXX%ROWTYPE;
y_form  YYY%ROWTYPE;
ZZZRow  ZZZ%ROWTYPE;

BEGIN
OPEN XXX;
LOOP
  FETCH XXX
    INTO x_form;
  EXIT WHEN XXX%NOTFOUND;

  OPEN YYY;
  LOOP
    FETCH YYY
      INTO y_form;
    EXIT WHEN YYY%NOTFOUND;

    OPEN ZZZ(x_form.form_no, y_form.form_no);
    LOOP
      FETCH ZZZ
        INTO ZZZRow;
      EXIT WHEN ZZZ%NOTFOUND;
      dbms_output.put_line('Distinct: ' || x_form.form_no);
      dbms_output.put_line('SUM x:    ' || x_form.sum_type_neg);
      dbms_output.put_line('SUM y:    ' || y_form.sum_type_pos);
      dbms_output.put_line('----------------------------');
    END LOOP;
    CLOSE ZZZ;
  END LOOP;
  CLOSE YYY;
END LOOP;
CLOSE XXX;

IF x_form.sum_type_neg = O or y_form.sum_type_pos = 0 THEN
  v_error_in_process := 'Y';
  dbms_output.put_line('ERROR MESSAGE');
ELSE
  dbms_output.put_line('GREATE JOB');
END IF;

END;

输出就像这样

 Distinct : 5155-55
 SUM x:    -2
 SUM y:     1
 ----------------------------
 Distinct: 4568-98
 SUM x:    -3
 SUM y:     1
 ----------------------------
 Distinct: 3302-48
 SUM x:    -1
 SUM y:     1
 ----------------------------
 GREAT JOB!

除非form_no没有-1或1,否则它的效果很好
输入示例:

form_no  |  type
---------------
5155-55  |  -1
5155-55  |  -1
5155-55  |   2
5155-55  |   1
         |
4568-98  |  -1
4568-98  |  -1
4568-98  |  -1
4568-98  |   2
4568-98  |   1 

3302-48  |   1
3302-48  |   2


请注意,form_no 3302-48没有-1。那么输出就像这样

 Distinct: 5155-55
 SUM x:    -2
 SUM y:     1
 ----------------------------
 Distinct: 4568-98
 SUM x:    -3
 SUM y:     1
 ----------------------------
 GREAT JOB!

除了我希望它像这样

 Distinct: 5155-55
 SUM x:    -2
 SUM y:     1
 ----------------------------
 Distinct: 4568-98
 SUM x:    -3
 SUM y:     1
 ----------------------------
 Distinct: 3302-48
 SUM x:     0
 SUM y:     1
 ----------------------------
 ERROR MESSAGE

有谁能告诉我如何去做这件事。我不确定游标是否可以接受空值(即当没有1或-1时,则没有任何内容可以加载到游标中)。

1 个答案:

答案 0 :(得分:2)

实际上不需要PL / SQL处理,只需使用SQL即可实现所需的结果:

SQL> select col
  2       , val
  3    from ( select form_no as "Distinct:"
  4                , to_char(sum(case when type1 = -1 then type1 else 0 end)) as sumx
  5                , to_char(sum(case when type1 = 1  then type1 else 0 end)) as sumy
  6            from t1
  7           group by form_no
  8         )
  9  unpivot(
 10     val for col in ("Distinct:", sumx, sumy)
 11   )
 12  ;

结果:

COL       VAL
--------- ----------
Distinct: 5155-55
SUMX      -2
SUMY      1

Distinct: 4568-98
SUMX      -3
SUMY      1

Distinct: 3302-48
SUMX      0
SUMY      1

9 rows selected 

请注意。 {11}版本引入了Unpivot运算符,并在上述查询中用于数据格式化。如果您的Oracle版本高于11g,您可以根据需要使用PL / SQL进行子查询(内联视图)和格式输出。

SQLFiddle Demo