Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 在循环游标中找不到数据_Loops_Plsql_Cursor - Fatal编程技术网

Loops 在循环游标中找不到数据

Loops 在循环游标中找不到数据,loops,plsql,cursor,Loops,Plsql,Cursor,在PLSQL中运行以下代码时,出现“未找到数据”错误 declare v_firm_id number; amount number; begin OPEN MT_CURSOR FOR SELECT firm_id FROM t_firm; LOOP FETCH MT_CURSOR INTO v_firm_id; EXIT WHEN MT_CURSOR%NOTFOUND; Select sum(TRN

在PLSQL中运行以下代码时,出现“未找到数据”错误

 declare

    v_firm_id number;
    amount number;

    begin

    OPEN MT_CURSOR FOR
    SELECT firm_id
    FROM t_firm;

    LOOP

    FETCH MT_CURSOR INTO v_firm_id;

    EXIT WHEN MT_CURSOR%NOTFOUND;

    Select sum(TRN_AMOUNT) into amount 
    from t_sales
    where FIRM_ID = v_firm_id;

    update t_firm
    set matching_amount = amount
    where firm_id = v_firm_id; 

    END LOOP;


 end;

这个密码有问题吗?有人能告诉我更正的地方吗?谢谢

问题似乎在于游标返回的其中一家公司的T_SALES表中没有数据。要处理这个问题,您需要为找不到的数据包含一个处理程序 例外情况,因此如果没有客户的销售交易记录,则总销售金额设置为零:

declare
  v_firm_id number;
  amount number;

begin
  OPEN MT_CURSOR FOR
    SELECT firm_id
    FROM t_firm;

  LOOP
    FETCH MT_CURSOR INTO v_firm_id;

    EXIT WHEN MT_CURSOR%NOTFOUND;

    BEGIN
      Select sum(TRN_AMOUNT) into amount 
        from t_sales
        where FIRM_ID = v_firm_id;
    EXCEPTION
      WHEN NO_DATA_FOUND THEN
        AMOUNT := 0;
    END;

    update t_firm
      set matching_amount = amount
      where firm_id = v_firm_id; 

  END LOOP;
end;
分享和享受

嗨,如果我单独运行,我可以从该查询中获得总和(trn_amount)。我 尝试按如下方式打印金额:dbms\u output.put\u line('amount:' ||金额);我得到的输出如下:(如果没有事务,则是正确的) 销售额显示为0,但似乎为空)金额:(空白)

我猜t_公司和t_销售之间不存在基于公司id的匹配

一个小提示,将其替换为SQL语句:

CREATE TABLE t_firm  ( matching_amount NUMBER, firm_id NUMBER );

CREATE TABLE t_sales ( trn_amount NUMBER, firm_id NUMBER );

INSERT INTO  t_firm  VALUES (100, 1);

INSERT INTO  t_sales VALUES (200, 1);

UPDATE  t_firm f
SET     f.matching_amount =
        (
            SELECT sum(s.trn_amount)
            FROM t_sales s
            WHERE f.firm_id = s.firm_id
        )
;

SELECT matching_amount FROM t_firm WHERE firm_id = 1;
-- 200
嗨,如果我单独运行,我可以从该查询中获得总和(trn_amount)。我尝试按如下方式打印金额:dbms_output.put_line('amount:'| | amount);我得到的输出如下:(如果没有交易销售,它将显示0,但似乎是空的)金额:(空白)更改
选择sum(TRN\u amount)为amount
选择NVL(sum(TRN\u amount),0)为amount