创建 instead of 触发器遇到的问题

suxun 2008-06-14 05:09:13
我先创建了emp表
create table emp (emp_id number(5) primary key, emp_name varchar2(20),
emp_salary number(4));
将emp表的emp_id属性设置为关键字

然后创建dept表
create table dept (dept_id number(3),dept_name varchar2(20),emp_id number(5),
constraint fk_empid foreign key(emp_id) references emp(emp_id));

创建视图
create or replace view dept_emp as select system.emp.EMP_ID,
system.emp.EMP_NAME, system.dept.DEPT_NAME, system.emp.EMP_SALARY
from system.dept, system.emp where system.emp.EMP_ID = system.dept.EMP_ID;

创建instead of 触发器
create or replace trigger tr_instead_of_dept_emp
instead of insert on dept_emp
for each row
declare
v_temp int;
begin
select count(*) into v_temp from dept
where DEPT_ID = :new.DEPT_ID;
if v_temp = 0 then
insert into dept(DEPT_ID,DEPT_NAME,EMP_ID)
values(:new.DEPT_ID,:new.DEPT_NAME,:new.EMP_ID);
end if;
select count(*) into v_temp emp
where EMP_ID = :new.EMP_ID;
if v_temp = 0 then
insert into emp(EMP_ID,EMP_NAME,EMP_SALARY)
values(:new.EMP_ID,:new.EMP_NAME,EMP_SALARY);
end if;
end;
编译运行后出现:NEW.DEPT_ID赋值错误。

请问那个大侠知道问题出现在什么地方,请给予指教。谢谢。。。
...全文
210 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
suxun 2008-06-16
  • 打赏
  • 举报
回复

create table system.emp (emp_id number(5) primary key, emp_name varchar2(20),
emp_salary number(4));

create table system.dept (dept_id number(3),dept_name varchar2(20),emp_id number(5),
constraint fk_empid foreign key(emp_id) references system.emp(emp_id));


create or replace view dept_emp as select system.emp.EMP_ID, system.emp.EMP_NAME,
system.dept.DEPT_NAME,system.emp.EMP_SALARY from system.dept,system.emp where
system.emp.EMP_ID = system.dept.EMP_ID;


create or replace trigger tr_instead_of_dept_emp
instead of insert on system.dept_emp
for each row
declare
v_temp int;
begin
select count(*) into v_temp from system.dept
where system.dept.dept_id = :new.dept_id;
if v_temp = 0 then
insert into system.dept (dept_id,dept_name,emp_id)
values(:new.dept_id,:new.dept_name,:new.emp_id);
end if;
select count(*) into v_temp from system.emp
where system.emp.emp_id = :new.emp_id;
if v_temp = 0 then
insert into system.emp (emp_id,emp_name,emp_salary)
values(:new.emp_id,:new.emp_name,:new.emp_salary);
end if;
end;

insert into dept_emp values(1,'stephen','technology',8000);

提示无法修改与非键值保存对应的列

上面的顺序反了。。。
suxun 2008-06-16
  • 打赏
  • 举报
回复
create or replace view dept_emp as select system.emp.EMP_ID, system.emp.EMP_NAME,
system.dept.DEPT_NAME,system.emp.EMP_SALARY from system.dept,system.emp where
system.emp.EMP_ID = system.dept.EMP_ID;
create table system.emp (emp_id number(5) primary key, emp_name varchar2(20),
emp_salary number(4));
create table system.dept (dept_id number(3),dept_name varchar2(20),emp_id number(5),
constraint fk_empid foreign key(emp_id) references system.emp(emp_id));

select * from dept_emp;


create or replace trigger tr_instead_of_dept_emp
instead of insert on system.dept_emp
for each row
declare
v_temp int;
begin
select count(*) into v_temp from system.dept
where system.dept.dept_id = :new.dept_id;
if v_temp = 0 then
insert into system.dept (dept_id,dept_name,emp_id)
values(:new.dept_id,:new.dept_name,:new.emp_id);
end if;
select count(*) into v_temp from system.emp
where system.emp.emp_id = :new.emp_id;
if v_temp = 0 then
insert into system.emp (emp_id,emp_name,emp_salary)
values(:new.emp_id,:new.emp_name,:new.emp_salary);
end if;
end;

insert into dept_emp values(1,'stephen','technology',8000);

提示无法修改与非键值保存对应的列

这是什么原因呢?
wffffc 2008-06-14
  • 打赏
  • 举报
回复

insert into dept(DEPT_ID,DEPT_NAME,EMP_ID)
values(:new.DEPT_ID,:new.DEPT_NAME,:new.EMP_ID);这里有问题,你的视图里面就没有DEPT_ID这个字段
select count(*) into v_temp from dept
where DEPT_ID = :new.DEPT_ID;同上


select count(*) into v_temp emp这里少了from


编译后用show error:
警告: 创建的触发器带有编译错误。

SQL> show error
TRIGGER TR_INSTEAD_OF_DEPT_EMP 出现错误:

LINE/COL ERROR
-------- -----------------------------------------------------------------
5/17 PLS-00049: 错误的赋值变量 'NEW.DEPT_ID'
8/8 PLS-00049: 错误的赋值变量 'NEW.DEPT_ID'
10/1 PL/SQL: SQL Statement ignored
10/29 PL/SQL: ORA-00923: 未找到要求的 FROM 关键字
fxianxian 2008-06-14
  • 打赏
  • 举报
回复
错误一:
constraint fk_empid foreign key(emp_id) references emp(emp_id)
错误二:
values(:new.EMP_ID,:new.EMP_NAME,EMP_SALARY);
另外还有
SYSTEM应该是DBA的权限,你有没有权限望system.emp和system.dept里面插数据啊?

17,382

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧