Q132284591 发表于 2018-9-14 10:48:17

Oracle 游标使用

-- 声明游标;CURSOR cursor_name IS select_statement  --For 循环游标
  --(1)定义游标
  --(2)定义游标变量
  --(3)使用for循环来使用这个游标
  declare
  --类型定义
  cursor c_job
  is
  select empno,ename,job,sal
  from emp
  where job='MANAGER';
  --定义一个游标变量v_cinfo c_emp%ROWTYPE ,该类型为游标c_emp中的一行数据类型
  c_row c_job%rowtype;
  begin
  for c_row in c_job loop
  dbms_output.put_line(c_row.empno||'-'||c_row.ename||'-'||c_row.job||'-'||c_row.sal);
  end loop;
  end;
  --Fetch游标
  --使用的时候必须要明确的打开和关闭
  declare
  --类型定义
  cursor c_job
  is
  select empno,ename,job,sal
  from emp
  where job='MANAGER';
  --定义一个游标变量
  c_row c_job%rowtype;
  begin
  open c_job;
  loop
  --提取一行数据到c_row
  fetch c_job into c_row;
  --判读是否提取到值,没取到值就退出
  --取到值c_job%notfound 是false
  --取不到值c_job%notfound 是true
  exitwhen c_job%notfound;
  dbms_output.put_line(c_row.empno||'-'||c_row.ename||'-'||c_row.job||'-'||c_row.sal);
  end loop;
  --关闭游标
  close c_job;
  end;
  --1:任意执行一个update操作,用隐式游标sql的属性%found,%notfound,%rowcount,%isopen观察update语句的执行情况。
  begin
  update emp set ENAME='ALEARK'WHERE EMPNO=7469;
  if sql%isopen then
  dbms_output.put_line('Openging');
  else
  dbms_output.put_line('closing');
  endif;
  if sql%found then
  dbms_output.put_line('游标指向了有效行');--判断游标是否指向有效行
  else
  dbms_output.put_line('Sorry');
  endif;
  if sql%notfound then
  dbms_output.put_line('Also Sorry');
  else
  dbms_output.put_line('Haha');
  endif;
  dbms_output.put_line(sql%rowcount);
  exception
  when no_data_found then
  dbms_output.put_line('Sorry No data');
  when too_many_rows then
  dbms_output.put_line('Too Many rows');
  end;
  declare
  empNumber emp.EMPNO%TYPE;
  empName emp.ENAME%TYPE;
  begin
  if sql%isopen then
  dbms_output.put_line('Cursor is opinging');
  else
  dbms_output.put_line('Cursor is Close');
  endif;
  if sql%notfound then
  dbms_output.put_line('No Value');
  else
  dbms_output.put_line(empNumber);
  endif;
  dbms_output.put_line(sql%rowcount);
  dbms_output.put_line('-------------');
  select EMPNO,ENAME intoempNumber,empName from emp where EMPNO=7499;
  dbms_output.put_line(sql%rowcount);
  if sql%isopen then
  dbms_output.put_line('Cursor is opinging');
  else
  dbms_output.put_line('Cursor is Closing');
  endif;
  if sql%notfound then
  dbms_output.put_line('No Value');
  else
  dbms_output.put_line(empNumber);
  endif;
  exception
  when no_data_found then
  dbms_output.put_line('No Value');
  when too_many_rows then
  dbms_output.put_line('too many rows');
  end;
  --2,使用游标和loop循环来显示所有部门的名称
  --游标声明
  declare
  cursor csr_dept
  is
  --select语句
  select DNAME
  from Depth;
  --指定行指针,这句话应该是指定和csr_dept行类型相同的变量
  row_dept csr_dept%rowtype;
  begin
  --for循环
  for row_dept in csr_dept loop
  dbms_output.put_line('部门名称:'||row_dept.DNAME);
  end loop;
  end;
  --3,使用游标和while循环来显示所有部门的的地理位置(用%found属性)
  declare
  --游标声明
  cursor csr_TestWhile
  is
  --select语句
  selectLOC
  from Depth;
  --指定行指针
  row_loc csr_TestWhile%rowtype;
  begin
  --打开游标
  open csr_TestWhile;
  --给第一行喂数据
  fetch csr_TestWhile into row_loc;
  --测试是否有数据,并执行循环
  while csr_TestWhile%found loop
  dbms_output.put_line('部门地点:'||row_loc.LOC);
  --给下一行喂数据
  fetch csr_TestWhile into row_loc;
  end loop;
  close csr_TestWhile;
  end;
  select*from emp
  --4,接收用户输入的部门编号,用for循环和游标,打印出此部门的所有雇员的所有信息(使用循环游标)
  --CURSOR cursor_name[(parameter[,parameter],...)] IS select_statement;
  --定义参数的语法如下:Parameter_name data_type[{:=|DEFAULT} value]
  declare
  CURSOR
  c_dept(p_deptNo number)
  is
  select*from emp where emp.depno=p_deptNo;
  r_emp emp%rowtype;
  begin
  for r_emp in c_dept(20) loop
  dbms_output.put_line('员工号:'||r_emp.EMPNO||'员工名:'||r_emp.ENAME||'工资:'||r_emp.SAL);
  end loop;
  end;
  select*from emp
  --5:向游标传递一个工种,显示此工种的所有雇员的所有信息(使用参数游标)
  declare
  cursor
  c_job(p_job nvarchar2)
  is
  select*from emp where JOB=p_job;
  r_job emp%rowtype;
  begin
  for r_job in c_job('CLERK') loop
  dbms_output.put_line('员工号'||r_job.EMPNO||''||'员工姓名'||r_job.ENAME);
  end loop;
  end;
  SELECT*FROM EMP
  --6:用更新游标来为雇员加佣金:(用if实现,创建一个与emp表一摸一样的emp1表,对emp1表进行修改操作),并将更新前后的数据输出出来
  --http://zheng12tian.iteye.com/blog/815770
  createtable emp1 asselect*from emp;
  declare
  cursor
  csr_Update
  is
  select*fromemp1 forupdateOF SAL;
  empInfo csr_Update%rowtype;
  saleInfoemp1.SAL%TYPE;
  begin
  FOR empInfo IN csr_Update LOOP
  IF empInfo.SALr_testAvg.DEP_AVG then
  salInfo:=r_testAvg.SAL-50;
  endif;
  update emp1 set SAL=salInfo wherecurrentof crs_testAvg;
  end loop;
  end;


页: [1]
查看完整版本: Oracle 游标使用