AI编程社区 oracle cursor

oracle cursor

--游标可用于处理多行--显示声明游标declarecursorcur_empisselect*fromemployees;--声明游标v_empemployees%rowtype;v_star_nonumber;v_starvarchar2(100);beginopencur_emp;--打开游标loop...


  
  1. --游标可用于处理多行 
  2. --显示声明游标 
  3. declare 
  4.  cursor cur_emp is select  * from employees;--声明游标 
  5.  v_emp employees%rowtype; 
  6.  v_star_no number; 
  7.  v_star  varchar2(100) ; 
  8. begin 
  9.    open cur_emp;--打开游标 
  10.    loop 
  11.       fetch cur_emp into v_emp;--获取值 
  12.       v_star_no := round(v_emp.salary/1000); 
  13.       v_star := null
  14.       for i in 1..v_star_no 
  15.       loop 
  16.           v_star := v_star||'*'
  17.        end loop; 
  18.        dbms_output.put_line(v_emp.last_name||' is '||v_star||'( salary is: '||v_emp.salary||')'); 
  19.        exit when cur_emp%notfound; 
  20.      end loop; 
  21. close cur_emp; 
  22. end
  23.  
  24. --游标for循环(隐式打开游标,获取游标,关闭游标) 
  25. --形式一 
  26. declare 
  27.  cursor cur_emp is select  * from employees; 
  28.   v_star_no number; 
  29.   v_star  varchar2(100) ; 
  30. begin 
  31.    for rec in cur_emp 
  32.     loop 
  33.      v_star_no := round(rec.salary/1000);--星号的个数 
  34.       v_star := null;--初始化 
  35.       for i in 1..v_star_no 
  36.       loop 
  37.           v_star := v_star||'*'
  38.        end loop; 
  39.        dbms_output.put_line(rec.last_name||' is '||v_star||'( salary is: '||rec.salary||')');    
  40.        exit when cur_emp%notfound;    
  41.      end loop; 
  42. end;   
  43.  
  44. --形式二 
  45. declare 
  46.    v_star_no number; 
  47.   v_star  varchar2(100) ; 
  48. begin 
  49.    for rec in (select  * from employees) 
  50.       loop 
  51.      v_star_no := round(rec.salary/1000); 
  52.       v_star := null
  53.       for i in 1..v_star_no 
  54.       loop 
  55.           v_star := v_star||'*'
  56.        end loop; 
  57.        dbms_output.put_line(rec.last_name||' is '||v_star||'( salary is: '||rec.salary||')');  
  58.     exit when cur_emp%notfound;      
  59.      end loop; 
  60. end;    
  61.  
  62. --游标的属性 
  63. --SQL%ROWCOUNT  :返回受影响的行数 
  64. --SQL%FOUND     :返回值为布尔值,如果一行或多行受sql语句影响,返回true否则为false 
  65. --SQL%NOTFOUND  :返回值为布尔值,如果行数受sql语句影响,返回true否则为false 
  66. create table t1 as select * from employees;   
  67. /       
  68. VARIABLE rows_deleted VARCHAR2(30) 
  69. DECLARE 
  70. v_employee_id t1.employee_id%TYPE := 176; 
  71. BEGIN 
  72. DELETE FROM t1 
  73. WHERE 
  74. employee_id = v_employee_id; 
  75. :rows_deleted := (SQL%ROWCOUNT ||' row deleted.'); 
  76. END
  77. PRINT rows_deleted 
  78.  
  79. rows_deleted 绑定变量 
  80.     绑定变量是一种减少应用程序在分析查询时将使用的栓锁(也就是锁)数目的可靠方法。 
  81.     软分析比硬分析使用的CPU时间少得多,绑定变量是获得软分析的方法。 
  82.     SQL语句中使用串字面值而不是绑定变量使系统冒SQL注入的风险。 
  83.     如果用户输入某些预料不到的字符,如输入引号,SQL语句中的串字面值可能导致失败。 
  84.     不论使用何种环境,不管是PL/SQL、JAVA和JDBC或某些其他语言,使用绑定变量即使不比不使用绑定变量快,至少也是一样快,而且代码容易编写。 
  85.  
  86. --查询工资大于部门平均工资的员工(运用关联子查询) 
  87. SET SERVEROUTPUT ON 
  88. BEGIN 
  89.  FOR  rec IN (SELECT  e.last_name,e.salary,e.department_id,b.avgsal  
  90.               FROM  employees e, 
  91.               (SELECT department_id, round(avg(salary),2) avgsal 
  92.                FROM employees group by department_id 
  93.               ) b 
  94.               WHERE e.department_id=b.department_id and e.salary>b.avgsal 
  95.               ) 
  96.                 LOOP 
  97.                 dbms_output.put_line(rec.last_name||' works in '||rec.department_id||' and earns more than average salary '|| 
  98.                                     rec.avgsal||' the salary is '||rec.salary); 
  99.                 END LOOP; 
  100.                 END

 

转载于:https://blog.51cto.com/toratto/1133240

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐

  • 浏览量 56
  • 收藏 0
  • 0

所有评论(0)

查看更多评论 
已为社区贡献3条内容