DECLARE CURSOR emp_cursor IS SELECT empno,ename,job FROM emp; v_empno emp.empno%TYPE; v_name emp.ename%TYPE; v_job emp.job%TYPE; BEGIN OPEN emp_cursor; LOOP FETCH emp_cursor INTO v_empno,v_name,v_job; DBMS_OUTPUT.PUT_LINE('員工號爲:'||v_empno||'姓名是'||v_name||'職位:'||v_job); EXIT WHEN emp_cursor%NOTFOUND; END LOOP; CLOSE emp_cursor; END;
DECLARE CURSOR emp_cursor IS SELECT empno,ename,job FROM emp; v_empno emp.empno%TYPE; v_name emp.ename%TYPE; v_job emp.job%TYPE; BEGIN OPEN emp_cursor; LOOP FETCH emp_cursor INTO v_empno,v_name,v_job; EXIT WHEN emp_cursor%NOTFOUND; END LOOP; IF emp_cursor%ISOPEN THEN DBMS_OUTPUT.PUT_LINE('遊標已打開'); DBMS_OUTPUT.PUT_LINE('讀取了'||emp_cursor%ROWCOUNT||'行'); ELSE DBMS_OUTPUT.PUT_LINE('遊標沒有打開'); END IF; CLOSE emp_cursor; END;
CREATE TABLE emp_new AS SELECT * FROM emp;
BEGIN UPDATE emp_new SET sal = sal+500 WHERE empno=&empno; IF SQL%FOUND THEN DBMS_OUTPUT.PUT_LINE('成功修改'); COMMIT; ELSE DBMS_OUTPUT.PUT_LINE('修改失敗'); ROLLBACK; END IF; END;
DECLARE CURSOR emp_cursor IS SELECT empno,ename,job FROM emp; BEGIN FOR emp_record IN emp_cursor LOOP DBMS_OUTPUT.PUT_LINE('員工號:'||emp_record.empno||'員工姓名'||emp_record.ename||'員工職位'||emp_record.job); END LOOP; END;
BEGIN FOR emp_record IN (SELECT empno,ename,job FROM emp) LOOP DBMS_OUTPUT.PUT_LINE('員工號:'||emp_record.empno||'員工姓名'||emp_record.ename||'員工職位'||emp_record.job); END LOOP; END;
DECLARE CURSOR emp_cursor(dno NUMBER)IS SELECT empno,ename,job FROM emp WHERE deptno=dno; BEGIN FOR emp_record IN emp_cursor(&dno) LOOP DBMS_OUTPUT.PUT_LINE('員工號'||emp_record.empno||'姓名'||emp_record.ename||'職位'||emp_record.job); END LOOP; END;
CREATE TABLE emp_new AS SELECT * FROM emp;
DECLARE CURSOR empnew_cursor IS SELECT ename,job FROM emp_new FOR UPDATE; BEGIN FOR empnew_record IN empnew_cursor LOOP DBMS_OUTPUT.PUT_LINE('姓名'||empnew_record.ename||'職位'||empnew_record.job); IF empnew_record.job='PRESIDENT' THEN UPDATE emp_new SET sal=sal+1000 WHERE CURRENT OF empnew_cursor; ELSIF empnew_record.job='MANAGER' THEN UPDATE emp_new SET sal=sal+500 WHERE CURRENT OF empnew_cursor; END IF; END LOOP; COMMIT; END;
SELECT * FROM EMP WHERE job in('PRESIDENT','MANAGER'); SELECT * FROM EMP_NEW WHERE job in('PRESIDENT','MANAGER');