聲明游標:?declare cursor? cursor_name(游標名)??is? select_statement(查詢語句);
打開游標:? open?cursor_name;
讀取游標中數(shù)據(jù):fetch?cursor_name?into?record變量?
關閉游標:?close?cursor_name;
案列1:
declare
??--定義游標
??cursorcur_xsjbxx?is
????select*?from? stuinfo?order by stuid;
??--定義記錄變量??
??ls_curinfo?cur_xsjbxx%rowtype;
begin
??opencur_xsjbxx;--打開游標
??loop
????FETCH cur_xsjbxx
??????INTO ls_curinfo;--獲取記錄值
????EXIT?WHEN cur_xsjbxx %NOTFOUND;
????dbms_output.put_line('學號:'||?ls_curinfo.stuid?||?',姓名:'||? ls_curinfo.STUNAME);
??end loop;
??close cur_xsjbxx;--關閉游標
end;
案列2:
CREATE OR REPLACE TRIGGER EMP_INFO
AFTER INSERT OR UPDATE OR DELETE ON EMP
DECLARE
CURSOR CUR_EMP IS
? SELECT DEPTNO, COUNT(EMPNO) AS TOTAL_EMP, SUM(SAL) AS TOTAL_SAL FROM EMP GROUP BY DEPTNO;
BEGIN
DELETE DEPT_SAL; --觸發(fā)時首先刪除映射表信息
FOR V_EMP IN CUR_EMP LOOP
? --DBMS_OUTPUT.PUT_LINE(V_EMP.DEPTNO || V_EMP.TOTAL_EMP || V_EMP.TOTAL_SAL);
? --插入數(shù)據(jù)
? INSERT INTO DEPT_SAL
? VALUES
? (V_EMP.DEPTNO, V_EMP.TOTAL_EMP, V_EMP.TOTAL_SAL);
END LOOP;
END;