我有一个具有代码和索引属性的表。
我正在尝试创建一个触发器来检查代码的值。如果插入的代码有重复,则触发索引加1。示例:
code|c_index
111 | 1
112 | 1
113 | 1
111 | 2
114 | 1
112 | 2
111 | 3
这是我的代码,但它不起作用:
create trigger trg_update
after insert on trial
for each row
declare v_index;
begin
select max(nvl(trial.c_index, 0)) into v_index
from trial;
if new.code = code then
set new.c_index = v_index
else
set new.c_index = 1
end if;
end;
...................................
我试图做一个新的更好的,但仍然没有工作:
create trigger trg_update
after insert on trial
for each row
declare v_index number;
begin
if :new.code = :old.code then
select max(nvl(c_index, 0)) into v_index
from trial
where code = :new.code;
set :new.c_index = v_index + 1
else
set :new.c_index = 1
end if;
end;
上面的代码有什么问题,问题的解决方案是什么?
..................................................
更新:
运行此代码后:
create trigger trg_update
AFTER insert on trial
for each ROW
DECLARE v_index NUMBER := -1; -- "-1" is put in place so to be below the minimum value of the column
DECLARE v_cnt NUMBER := 0;
BEGIN
SELECT MAX(c_index), COUNT(*)
INTO :v_index, v_cnt
FROM trial
WHERE code = :new.code;
IF v_index <> -1 AND v_cnt > 1 THEN
--Only one update here, for the newly inserted row explicitly
UPDATE trial
SET c_index = c_index +1
WHERE code = :new.code
AND c_index = v_index
AND ROWNUM = 1;
END IF;
END;
显示的一些问题:
1- 错误(2,1):PLS-00103:遇到符号“DECLARE” 2- 错误(7,8):PLS-00049:错误的绑定变量 'V_INDEX' 3- 错误(9,15):PLS-00049 :错误的绑定变量'NEW.CODE'
这是尝试修复错误 2 和 3 后的新代码:
create or replace trigger trg_update
AFTER insert on trial
for each ROW
declare vindex NUMBER := -1; -- "-1" is put in place so to be below the minimum value of the column
declare vcnt NUMBER := 0;
BEGIN
SELECT MAX(c_index), COUNT(*)
INTO vindex, vcnt
FROM trial
WHERE code = :new.code;
IF vindex <> -1 AND vcnt > 1 THEN
--Only one update here, for the newly inserted row explicitly
UPDATE trial
SET c_index = c_index +1
WHERE code = :new.code AND c_index = vindex AND ROWNUM = 1;
END IF;
END;
但是,仍然显示第一个错误。
错误(2,1):PLS-00103:在预期以下情况之一时遇到符号“DECLARE”:开始函数杂注过程子类型类型当前游标删除之前存在符号“开始”替换“声明”以继续。
除了这个错误:
错误(19,4):PLS-00103:在预期以下情况之一时遇到符号“文件结尾”:(开始案例声明结束异常退出 goto if loop mod null pragma raise return select update while with <<继续关闭当前删除获取锁插入打开回滚保存点设置sql执行提交forall合并管道清除
我该如何解决这些错误?