日期:2014-05-17 浏览次数:20829 次
-- 我认为应该前面加上
[color=#FF0000]declare[/color]
salary emp.sal%TYPE := 0; -- 声明变量salary,类型与emp.sal相同,初始值为0
mgr_num emp.mgr%TYPE; -- 声明变量mgr_num,类型与emp.mgr相同
last_name emp.ename%TYPE; -- 声明变量last_name,类型与emp.ename相同
starting_empno emp.empno%TYPE := 7499; -- 声明变量starting_empno,类型与emp.empno相同,初始值为7499
BEGIN -- 开始匿名过程
SELECT mgr
INTO mgr_num
FROM emp
WHERE empno = starting_empno; -- 将员工编号为7499的经理编号取出并赋值给mgr_num
WHILE salary <= 2500 LOOP
SELECT sal, mgr, ename
INTO salary, mgr_num, last_name
FROM emp
WHERE empno = mgr_num; -- 循环查找7499员工的上级领导,当其salary大于2500就退出循环,将找到的员工的sal,mgr,ename分别赋值给salary,mgr_num,last_name
END LOOP;
INSERT INTO temp
VALUES (NULL, salary, last_name); -- 将找到的纪录insert到temp表中。
COMMIT; -- 提交
EXCEPTION -- 处理异常
WHEN NO_DATA_FOUND THEN -- 当没找到任何纪录时向temp表insert一条关于'Not found'的纪录。
INSERT INTO temp
VALUES (NULL, NULL, 'Not found');
[color=#FF0000]end;[/color] -- 应该有end结束标志
/ -- 执行
DECLARE salary emp.sal%TYPE := 0; mgr_num emp.mgr%TYPE; last_name emp.ename%TYPE; starting_empno emp.empno%TYPE := 7499; BEGIN SELECT mgr INTO mgr_num FROM emp WHERE empno = starting_empno; --继续利用薪水和员工的上级工号进行迭代(这个循环是关键) WHILE salary <= 2500 LOOP--找到大于薪水2500的就退出while循环 SELECT sal, mgr, ename INTO salary, mgr_num, last_name FROM emp WHERE empno = mgr_num; END LOOP; --将找到的值放入temp表 INSERT INTO temp VALUES (NULL, salary, last_name); COMMIT; EXCEPTION --异常处理 WHEN NO_DATA_FOUND THEN INSERT INTO temp VALUES (NULL, NULL, 'Not found'); END; /