0

我已经创建了一个带有 select 语句的触发器,我想说如果这个 select 语句不返回任何行,那么在变量“辅助”中放置一个 0。我尝试使用 NVL(auxiliar,0) 但它不起作用。我怎样才能做到这一点?

SELECT NVL(salary,0) INTO auxiliar FROM BILL WHERE code=:NEW.code;

[更新]我的触发代码:

IF preCondicio THEN
        KMpendents:=coalesce(SELECT rev_pendent_km
            INTO KMpendents
            FROM REV_PENDENT
            WHERE rev_pendent_vehicle_codi=:NEW.lloguer_vehicle_codi,0);
        IF KMtotals+KMpendents>=15000 THEN
            SELECT venedor_codi 
            INTO venedorCodi 
            FROM venedor 
            WHERE venedor_alta=(
                SELECT MAX(venedor_alta) 
                FROM venedor 
                WHERE venedor_delegacio_codi=(
                    SELECT venedor_delegacio_codi
                    FROM venedor
                    WHERE venedor_codi=:NEW.lloguer_venedor_codi));
            INSERT INTO REVISIONS VALUES(:NEW.lloguer_vehicle_codi,:NEW.lloguer_dataf,KMtotals+KMpendents,venedorCodi);
            IF KMpendents!=0 THEN
                DELETE FROM REV_PENDENT 
                WHERE rev_pendent_vehicle_codi=:NEW.lloguer_vehicle_codi;
            END IF;
        ELSE
            IF KMpendents!=0 THEN
                UPDATE REV_PENDENT SET rev_pendent_km=KMtotals+KMpendents WHERE rev_pendent_vehicle_codi=:NEW.lloguer_vehicle_codi;
            ELSE INSERT INTO REV_PENDENT VALUES(:NEW.lloguer_vehicle_codi,KMtotals,:NEW.lloguer_dataf);
            END IF;
        END IF;
    END IF;

变量KMpendents相当于我在更新之前告诉的变量auxiliar 。但是 Oracle 向我显示了这些错误:

PLS-00103:在预期以下情况之一时遇到符号“,”:。( * @ % & - + ; / at for mod 余数 rem 和或有相交的组减去顺序 start union where connect || indicator multiset

4

2 回答 2

0
BEGIN
 SELECT NVL(salary,0) INTO auxiliar FROM BILL WHERE code=:NEW.code;
EXCEPTION
  WHEN NOTFOUND THEN
      auxiliar := 0;
END;

或者您可以使用SQL%NOTFOUNDtoo 检查语句是否返回no rows

CREATE or REPLACE TRIGGER trigger_name
BEFORE INSERT
   ON table_name
   [ FOR EACH ROW ]

DECLARE
   -- variable declarations

BEGIN
    ....
    ....
    BEGIN
     SELECT NVL(salary,0) INTO auxiliar FROM BILL WHERE code=:NEW.code;
    EXCEPTION
      WHEN NOTFOUND THEN
          auxiliar := 0;
    END;
    --Yes, this is possible and valid.

EXCEPTION
   WHEN ...
   -- exception handling

END;
于 2012-12-28T00:14:22.970 回答
0

如果 select 子句只包含聚合函数,那么总会返回至少一行。

SELECT coalesce(sum(salary),0)
INTO   auxiliar
FROM   BILL
WHERE  code=:NEW.code;
于 2012-12-28T11:35:20.893 回答