一、实验题目
PLSQL程序设计基础
二、实验目的
熟悉PL/SQL基本语法,掌握游标、异常、控制语句的使用。
三、实验内容
1、编写“显示指定教师信息”的匿名块。
--DECLARE
-- V_TEACHER_ID TEACHER.TEACHER_ID%TYPE:='&要查询的教师编号';
-- MYEXCEPTION EXCEPTION;
-- CURSOR TEACHER_CUR IS
-- SELECT * FROM TEACHER WHERE TEACHER_ID=V_TEACHER_ID;
--BEGIN
-- IF TRIM(V_TEACHER_ID) IS NULL THEN
-- RAISE MYEXCEPTION;
-- END IF;
-- FOR TEACHER_REC IN TEACHER_CUR LOOP
-- dbms_output.put_line('教师编号|姓名|性别|出生日期|学历|职称|所在系部');
-- DBMS_OUTPUT.PUT_LINE(TEACHER_REC.TEACHER_ID||' '||TEACHER_REC.TNAME||' '||TEACHER_REC.TSEX||' '||TEACHER_REC.TBIRTH||' '||TEACHER_REC.EDUC||' '||TEACHER_REC.PROFESS||' '||TEACHER_REC.DEPT_ID);
-- END LOOP;
-- EXCEPTION
-- WHEN MYEXCEPTION THEN DBMS_OUTPUT.PUT_LINE('输入的教师编号为空!');
--END;
2、编写“添加教师信息”的匿名块。
DECLARE
V_TEACHERID TEACHER.TEACHER_ID%TYPE:='&教师编号';
V_TNAME TEACHER.TNAME%TYPE:='&教师姓名';
V_TSEX TEACHER.TSEX%TYPE:='&教师性别';
V_TBIRTH TEACHER.TBIRTH%TYPE:='&出生日期';
V_TEDUC TEACHER.EDUC%TYPE:='&教师学历';
V_PROFESS TEACHER.PROFESS%TYPE:='&职称';
V_DEPTID TEACHER.DEPT_ID%TYPE:='&系部代码';
V_NUM NUMBER;
NULL_EX EXCEPTION;
NE_EX EXCEPTION;
BEGIN
IF TRIM(V_TEACHERID) IS NULL OR TRIM(V_TNAME) IS NULL OR TRIM(V_TSEX) IS NULL OR TRIM(V_TBIRTH) IS NULL OR TRIM(V_TEDUC)
IS NULL OR TRIM(V_PROFESS) IS NULL OR TRIM(V_DEPTID) IS NULL THEN
RAISE NULL_EX;
END IF;
SELECT COUNT(DEPT_ID) INTO V_NUM FROM DEPT WHERE DEPT_ID=V_DEPTID;
IF V_NUM= 0 THEN
RAISE NE_EX;
END IF;
IF V_TSEX!='男' AND V_TSEX!='女' THEN
DBMS_OUTPUT.PUT_LINE('性别只能为男或女!');
END IF;
INSERT INTO TEACHER VALUES(V_TEACHERID,V_TNAME,V_TSEX,TO_DATE(V_TBIRTH),V_TEDUC,V_PROFESS,V_DEPTID);
DBMS_OUTPUT.PUT_LINE('教师信息添加成功!');
COMMIT;
EXCEPTION
WHEN NULL_EX THEN DBMS_OUTPUT.PUT_LINE('教师编码输入为空!');
WHEN NE_EX THEN DBMS_OUTPUT.PUT_LINE('部门表中没有此部门记录!');
WHEN DUP_VAL_ON_INDEX THEN
DBMS_OUTPUT.PUT_LINE('教师已存在!');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('未知异常!');
END;
来源:https://blog.csdn.net/qq_41007733/article/details/102750147