我想在触发器上找不到数据时抛出一个错误,所以我使用了以下触发器。当没有第一次找到数据时工作,此后数据prsents它不工作且没有结果发生。任何想法这个问题
create or replace
trigger testtrigger
after INSERT OR UPDATE
on T1
FOR EACH ROW
BEGIN
SELECT t_power
INTO t_power_trg
FROM t3
WHERE UPPER(t3.NAME) LIKE ('%' || UPPER(:NEW.INFO) || '%');
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20001,'-------------Selected model is not present (info(2))-------------');
SELECT refer_power
INTO refer_power_trg
FROM t3
WHERE UPPER(t3.NAME) LIKE ('%' || UPPER(:NEW.TAR2) || '%');
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20001,'-------------Selected reference is not present (info(2))-------------');
UPDATE t_master
SET
Master_pwr=t_power_trg where firstname='Z'
END IF;
END
答案 0 :(得分:2)
您的代码中存在一些SYNTAX问题,请查看以下代码。希望它能满足您的要求。
CREATE OR REPLACE TRIGGER testtrigger
AFTER INSERT OR UPDATE ON T1
FOR EACH ROW
DECLARE
t_power_trg T3.T_POWER%TYPE;
BEGIN
SELECT t_power
INTO t_power_trg
FROM t3
WHERE UPPER(t3.NAME) LIKE ('%' || UPPER(:NEW.INFO) || '%');
UPDATE t_master
SET Master_pwr=t_power_trg
where firstname='Z';
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20001,'-------------Selected model is not present (info(2))-------------');
END;
答案 1 :(得分:1)
这是一个函数,您可以在循环之前添加一个步骤来捕获您想要查看的案例数
像这样:if ((select count(*)
FROM t3
WHERE UPPER(t3.NAME) LIKE ('%' || UPPER(:NEW.INFO) || '%')) < 1
) then
SET @error = '-------------Selected model is not present (info(2))-------------';
end if;