SET SERVEROUTPUT ON;
DECLARE
V_ENAME EMP.ENAME%TYPE;
BEGIN
SELECT ENAME INTO V_ENAME FROM EMP WHERE EMPNO = &GNO;
DBMS_OUTPUT.PUT_LINE('名字:' || V_ENAME);
END;
/
SET SERVEROUTPUT ON;
DECLARE
V_ENAME EMP.ENAME%TYPE;
BEGIN
SELECT ENAME INTO V_ENAME FROM EMP WHERE EMPNO = &GNO;
DBMS_OUTPUT.PUT_LINE('名字:' || V_ENAME);
EXCEPTION
WHEN no_data_found THEN
DBMS_OUTPUT.PUT_LINE('编号未找到!');
END;
/
SET SERVEROUTPUT ON;
CREATE OR REPLACE PROCEDURE SP_PRO6(SPNO NUMBER) IS
V_SAL EMP.SAL%TYPE;
BEGIN
SELECT SAL INTO V_SAL FROM EMP WHERE EMPNO = SPNO;
CASE
WHEN V_SAL < 1000 THEN
UPDATE EMP SET SAL = SAL + 100 WHERE EMPNO = SPNO;
WHEN V_SAL < 2000 THEN
UPDATE EMP SET SAL = SAL + 200 WHERE EMPNO = SPNO;
END CASE;
EXCEPTION
WHEN CASE_NOT_FOUND THEN
DBMS_OUTPUT.PUT_LINE('case语句没有与' || V_SAL || '相匹配的条件');
END;
/
--调用存储过程
SQL> EXEC SP_PRO6(7369);
DECLARE
CURSOR EMP_CURSOR IS
SELECT ENAME, SAL FROM EMP;
BEGIN
OPEN EMP_CURSOR; --声明时游标已打开,所以没必要再次打开
FOR EMP_RECORD1 IN EMP_CURSOR LOOP
DBMS_OUTPUT.PUT_LINE(EMP_RECORD1.ENAME);
END LOOP;
EXCEPTION
WHEN CURSOR_ALREADY_OPEN THEN
DBMS_OUTPUT.PUT_LINE('游标已经打开');
END;
/
DECLARE
CURSOR EMP_CURSOR IS
SELECT ENAME, SAL FROM EMP;
EMP_RECORD EMP_CURSOR%ROWTYPE;
BEGIN
--open emp_cursor; --打开游标
FETCH EMP_CURSOR INTO EMP_RECORD;
DBMS_OUTPUT.PUT_LINE(EMP_RECORD.ENAME);
CLOSE EMP_CURSOR;
EXCEPTION
WHEN INVALID_CURSOR THEN
DBMS_OUTPUT.PUT_LINE('请检测游标是否打开');
END;
/
SET serveroutput ON;
DECLARE
V_SAL EMP.SAL%TYPE;
BEGIN
SELECT SAL INTO V_SAL FROM EMP WHERE ENAME = 'ljq';
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('不存在该员工');
END;
/
--自定义例外
CREATE OR REPLACE PROCEDURE EX_TEST(SPNO NUMBER) IS
--定义一个例外
MYEX EXCEPTION;
BEGIN
--更新用户sal
UPDATE EMP SET SAL = SAL + 1000 WHERE EMPNO = SPNO;
--sql%notfound 这是表示没有update
--raise myex;触发myex
IF SQL%NOTFOUND THEN RAISE MYEX;
END IF;
EXCEPTION
WHEN MYEX THEN DBMS_OUTPUT.PUT_LINE('没有更新任何用户');
END;
/