ORACLE STUDY NOTES 04

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ORACLE STUDY NOTES 04相关的知识,希望对你有一定的参考价值。

?

【JSU】LJDragon‘s Oracle course notes In the first semester, junior year

?

PL/SQL游标(declare .. begin .. end)

--游标:指向查询结果集的指针,指针指向哪一行,提取的就是哪一行的数据

--PLSQL的游标默认指向结果集的第1行

--显示游标的四大步骤

1.定义游标 cursor 游标变量名 is 查询语句;

2.打开游标 open 游标变量名;(可以重复打开)

3.提取游标 fetch 游标变量名 into 变量名;

4.关闭游标 close 游标变量名;

--游标的四大属性

1.游标变量名%FOUND 布尔型属性,当最近一次读记录时成功返回,则值为TRUE;

2.游标变量名%NOTFOUND 和%found相反

3.游标变量名%ISOPEN 布尔型属性,当游标已打开时返回 TRUE;

4.游标变量名%ROWCOUNT 数字型属性,返回已从游标中读取的记录数

?

--查询输出10号部门的所有员工(编号,姓名,工资)

  1. ???????cursor emp_cursor is --1.定义游标
  2. ???????select empno,ename,sal from emp where deptno = 10;
  3. ???????v_empno emp.empno%type;
  4. ???????v_ename emp.ename%type;
  5. ???????v_sal emp.sal%type;
  6. ???????open emp_cursor; --2.打开游标
  7. ???????fetch emp_cursor into v_empno,v_ename,v_sal; --3.提取游标
  8. ????????dbms_output.put_line(emp_cursor%ROWCOUNT);
  9. ???????fetch emp_cursor into v_empno,v_ename,v_sal;
  10. ???????fetch emp_cursor into v_empno,v_ename,v_sal;
  11. ???????dbms_output.put_line(emp_cursor%ROWCOUNT);
  12. ?
  13. ????????if emp_cursor%notfound then
  14. ??????????dbms_output.put_line(‘true‘);
  15. ???????else
  16. ?????????dbms_output.put_line(‘false‘);
  17. ???????end if;
  18. ???????dbms_output.put_line(v_empno||‘,‘||v_ename||‘,‘||v_sal);
  19. ???????close emp_cursor; --4.关闭游标
  20. end;

--方法2:loop循环

  1. ???????cursor emp_cursor is
  2. ???????select empno,ename,sal from emp where deptno = 10;
  3. ???????v_empno emp.empno%type; --参照表的列的数据类型 表名.列名%type
  4. ???????v_ename emp.ename%type;
  5. ???????v_sal emp.sal%type;
  6. ???????open emp_cursor;
  7. ??????loop
  8. ????????????fetch emp_cursor into v_empno,v_ename,v_sal;
  9. ????????????exit when emp_cursor%notfound;
  10. ????????????dbms_output.put_line(v_empno||‘,‘||v_ename||‘,‘||v_sal);
  11. ??????end loop;
  12. ??????close emp_cursor;
  13. end;

--方法3:--while loop循环

  1. ???????cursor emp_cursor is
  2. ???????select empno,ename,sal from emp where deptno = 10;
  3. ???????v_empno emp.empno%type;
  4. ???????v_ename emp.ename%type;
  5. ???????v_sal emp.sal%type;
  6. ???????open emp_cursor;
  7. ???????fetch emp_cursor into v_empno,v_ename,v_sal;
  8. ???????while emp_cursor%found loop
  9. ????????????dbms_output.put_line(v_empno||‘,‘||v_ename||‘,‘||v_sal);
  10. ????????????fetch emp_cursor into v_empno,v_ename,v_sal;
  11. ???????end loop;
  12. ??????close emp_cursor;
  13. end;

--改成参照类型(?)

  1. ???????cursor emp_cursor is
  2. ???????select * from emp where deptno = 10;
  3. ???????e emp%rowtype;
  4. ???????open emp_cursor;
  5. ??????loop
  6. ????????????fetch emp_cursor into e;
  7. ????????????exit when emp_cursor%notfound;
  8. ????????????dbms_output.put_line(e.empno||‘,‘||e.ename||‘,‘||e.sal);
  9. ??????end loop;
  10. ??????close emp_cursor;
  11. end;

--游标提取的值赋给表类型(动态数组)

  1. ???????cursor emp_cursor is
  2. ???????select * from emp;
  3. ???????type emp_table_type is table of emp%rowtype
  4. ???????index by binary_integer;
  5. ?
  6. ???????e emp_table_type;
  7. ??????open emp_cursor;
  8. ????????????fetch emp_cursor bulk collect into e limit 5;--限制取的条数
  9. ??????close emp_cursor;
  10. ?
  11. ??????for i in 1 .. e.count loop
  12. ??????????dbms_output.put_line(e(i).ename);
  13. ??????end loop;
  14. end;

--循环取出所有的记录

  1. ???????cursor emp_cursor is
  2. ???????select * from emp;
  3. ???????type emp_table_type is table of emp%rowtype
  4. ???????index by binary_integer;
  5. ?
  6. ???????e emp_table_type;
  7. ??????open emp_cursor;
  8. ??????loop
  9. ????????????fetch emp_cursor bulk collect into e limit 5;--限制取的条数
  10. ???????????????for i in 1 .. e.count loop
  11. ???????????????????dbms_output.put_line(e(i).ename);
  12. ???????????????end loop;
  13. ????????????exit when emp_cursor%notfound;
  14. ??????end loop;
  15. ??????close emp_cursor;
  16. end;

--带参数游标,根据部门编号查询员工信息

  1. ???????cursor emp_cursor(p_deptno emp.deptno%type) is
  2. ???????select * from emp where deptno = p_deptno;
  3. ???????e emp%rowtype;
  4. ???????open emp_cursor(10);
  5. ??????loop
  6. ????????????fetch emp_cursor into e;
  7. ????????????exit when emp_cursor%notfound;
  8. ????????????dbms_output.put_line(e.empno||‘,‘||e.ename||‘,‘||e.sal);
  9. ??????end loop;
  10. ??????close emp_cursor;
  11. ??????dbms_output.put_line(‘-----------------‘);
  12. ???????open emp_cursor(20);
  13. ??????loop
  14. ????????????fetch emp_cursor into e;
  15. ????????????exit when emp_cursor%notfound;
  16. ????????????dbms_output.put_line(e.empno||‘,‘||e.ename||‘,‘||e.sal);
  17. ??????end loop;
  18. ??????close emp_cursor;
  19. end;

--游标for循环

  1. ???????cursor emp_cursor is select * from emp ;
  2. ???????for e in emp_cursor loop
  3. ?????????dbms_output.put_line(e.ename);
  4. ???????end loop;
  5. end;

--最精简版

  1. ???????for e in ( select * from emp) loop
  2. ?????????dbms_output.put_line(e.ename);
  3. ???????end loop;
  4. end;

--根据部门表循环输出员工信息

--部门编号:10,部门名称

--员工编号:xxx ,姓名:xxx,职位:xxx,入职日期:xxx

--员工编号:xxx ,姓名:xxx,职位:xxx,入职日期:xxx

--员工编号:xxx ,姓名:xxx,职位:xxx,入职日期:xxx

--部门编号:20,部门名称

--员工编号:xxx ,姓名:xxx,职位:xxx,入职日期:xxx

--员工编号:xxx ,姓名:xxx,职位:xxx,入职日期:xxx

--员工编号:xxx ,姓名:xxx,职位:xxx,入职日期:xxx

--游标for循环带参数写法

  1. ???????cursor dept_cursor is
  2. ???????select * from dept;
  3. ?
  4. ???????cursor emp_cursor(v_deptno emp.deptno%type) is
  5. ???????select * from emp where deptno = v_deptno;
  6. ???????for d in dept_cursor loop
  7. ??????????dbms_output.put_line(d.deptno||‘,‘||d.dname);
  8. ??????????for e in emp_cursor(d.deptno) loop
  9. ????????????dbms_output.put_line(‘ ‘||e.job||‘,‘||e.ename||‘,‘||e.hiredate);
  10. ????????????end loop;
  11. ???????end loop;
  12. end;

--精简写法

  1. ???????for d in (select * from dept) loop
  2. dbms_output.put_line(d.deptno||‘,‘||d.dname);
  3. ??????????for e in (select * from emp where deptno = d.deptno) loop
  4. dbms_output.put_line(‘ ‘||e.job||‘,‘||e.ename||‘,‘||e.hiredate);
  5. ??????????end loop;
  6. ???????end loop;
  7. end;

--隐式游标

隐式游标:固定名称SQL

游标四个属性

SQL%FOUND:如果操作有影响的行,就为TRUE,否则为FALSE

SQL%NOTFOUND:求反

SQL%ISOPEN:在隐式游标中,取值永远为FALSE

SQL%ROWCOUNT:操作影响的行数

1.在事务提交之前获取游标属性;

2.只取最近的一条DML操作状态;

  1. ???????update emp set sal = sal + 10 where deptno = 20;
  2. ???????--delete emp where 1=2;
  3. ???????if SQL%FOUND THEN
  4. ?????????dbms_output.put_line(‘TRUE‘);
  5. ???????ELSE
  6. ?????????dbms_output.put_line(‘FALSE‘);
  7. ???????END IF;
  8. ???????dbms_output.put_line(SQL%ROWCOUNT);
  9. ???????commit;
  10. end;

--游标更新(current of 游标变量名)

--更改所有员工工资<1000,加1000;

  1. ???????cursor emp_c is
  2. ???????select * from emp for update;
  3. ???????for e in emp_c loop
  4. ?????????if e.sal <3000 then
  5. ????????????update emp set sal = sal + 1000 where current of emp_c;
  6. ?????????end if;
  7. ???????end loop;
  8. ??????-- dbms_output.put_line(SQL%ROWCOUNT);
  9. end;

--动态游标

--根据用户的输入选择输出不同的表记录

--E,查询输出emp的姓名

--D,查询输出dept的部门名称

  1. ???????v_cmd CHAR(1) := ‘&input‘;
  2. ???????v_name VARCHAR2(50);
  3. ?
  4. ???????--声明自定义的游标变量类型
  5. ???????--TYPE c_type IS REF CURSOR;
  6. ???????--声明游标变量
  7. ????????--c c_type;
  8. ?
  9. ???????c SYS_REFCURSOR;--代替TYPE c_type IS REF CURSOR
  10. ???????IF v_cmd=‘ETHEN
  11. ??????????dbms_output.put_line(‘员工姓名‘);
  12. ??????????OPEN c FOR SELECT ename FROM emp;
  13. ???????ELSIF v_cmd=‘DTHEN
  14. ??????????dbms_output.put_line(‘部门名称‘);
  15. ??????????OPEN c FOR SELECT dname FROM dept;
  16. ???????ELSE
  17. ??????????dbms_output.put_line(‘输入无效‘);
  18. ??????????RETURN;
  19. ???????END IF;
  20. ?
  21. ???????LOOP
  22. ???????????FETCH c INTO v_name;
  23. ???????????EXIT WHEN c%NOTFOUND;
  24. ???????????dbms_output.put_line(v_name);
  25. ???????END LOOP;
  26. ?
  27. ???????CLOSE c;
  28. END;

【JSU】LJDragon‘s Oracle course over in 5th.

以上是关于ORACLE STUDY NOTES 04的主要内容,如果未能解决你的问题,请参考以下文章

[20-04-23][Self-study Notes 9]Java Array of Objects

[Python Study Notes]电池信息

[Python Study Notes]内存信息

Linux Study Notes

[Python Study Notes]cpu信息

[Python Study Notes]计算器