oracle触发器判断条件分别执行执行相应操作的问题?
我想做个触发器,判断一个插入的学生学号中班级号码部分(从学号的1到8个字符),在班级表中是否存在,如果存在将插入学生的班级名称赋值为刚才找到的班级号码对应的,班级名称。c...
我想做个触发器,判断一个插入的学生学号中班级号码部分(从学号的1到8个字符),在班级表中是否存在,如果存在将插入学生的班级名称赋值为刚才找到的班级号码对应的,班级名称。
create table class
( classno char(8),
classname varchar(50)
constraint cla2 not null,
constraint cla1 primary key(clano));
create table student
( stuno char(10),
stuname char(20)
constraint stu1 not null,
stugendar char(3)
constraint stu2 check(stugendar in('f','m')),
stubirth Date,
stuclaname char(50),
constraint studentkey primary key(stuno));
下面的触发器实在不知道怎么修正了,感谢哪位朋友帮改下!可行的话,另送积分。
create or replace trigger name_stuclaname
after insert on student
for each row
declare
tempclaname varchar(10);
tempclano varchar(10);
begin
execute immediate
tempclano=substr(:new.stuno,1,8);
if tempclano in(select claname from class) then
execute
select claname into tempclaname from class where clano=:tempclano;
update set new.stuclaname=:tempclaname;
else
rollback;
end; 展开
create table class
( classno char(8),
classname varchar(50)
constraint cla2 not null,
constraint cla1 primary key(clano));
create table student
( stuno char(10),
stuname char(20)
constraint stu1 not null,
stugendar char(3)
constraint stu2 check(stugendar in('f','m')),
stubirth Date,
stuclaname char(50),
constraint studentkey primary key(stuno));
下面的触发器实在不知道怎么修正了,感谢哪位朋友帮改下!可行的话,另送积分。
create or replace trigger name_stuclaname
after insert on student
for each row
declare
tempclaname varchar(10);
tempclano varchar(10);
begin
execute immediate
tempclano=substr(:new.stuno,1,8);
if tempclano in(select claname from class) then
execute
select claname into tempclaname from class where clano=:tempclano;
update set new.stuclaname=:tempclaname;
else
rollback;
end; 展开
1个回答
展开全部
1、你的建表文中【classno】还是【primary key(clano)】?
【classname 】还是【claname】我按照【clano】和【claname】理解了。
2、修改如下
CREATE OR REPLACE TRIGGER NAME_STUCLANAME
BEFORE INSERT ON STUDENT
FOR EACH ROW
DECLARE
TEMPCLANAME VARCHAR(10);
BEGIN
TEMPCLANAME := NULL;
SELECT CLANAME INTO TEMPCLANAME FROM CLASS WHERE CLANO =SUBSTR(:NEW.STUNO,1,8);
IF TEMPCLANAME IS NOT NULL THEN
:NEW.STUCLANAME:=TEMPCLANAME;
ELSE
RAISE_APPLICATION_ERROR(-20001, '您插入的[班级编号]在[班级表]中不存在');
END IF;
END;
/
---
以上,希望对你有所帮助。
【classname 】还是【claname】我按照【clano】和【claname】理解了。
2、修改如下
CREATE OR REPLACE TRIGGER NAME_STUCLANAME
BEFORE INSERT ON STUDENT
FOR EACH ROW
DECLARE
TEMPCLANAME VARCHAR(10);
BEGIN
TEMPCLANAME := NULL;
SELECT CLANAME INTO TEMPCLANAME FROM CLASS WHERE CLANO =SUBSTR(:NEW.STUNO,1,8);
IF TEMPCLANAME IS NOT NULL THEN
:NEW.STUCLANAME:=TEMPCLANAME;
ELSE
RAISE_APPLICATION_ERROR(-20001, '您插入的[班级编号]在[班级表]中不存在');
END IF;
END;
/
---
以上,希望对你有所帮助。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询