zoukankan      html  css  js  c++  java
  • 使用游标——使用游标更新或删除数据


    通过使用显式游标,不仅可以一行一行的处理select语句的结果,而且也可以更新或删除当前游标行的数据。注意,如果通过游标更新活是删除数据,那么在定义游标时必须要带有for update 子句。

    例子:显示雇员名,工资,部门号,并给部门30的所有雇员增加200元工资:


    declare 
    cursor emp_cursor is select * from emp for update;
    begin
    for emp_record in emp_cursor loop
    if emp_record.deptno=30 then
    update emp set sal=sal+200 where current of emp_cursor;
    dbms_output.put_line('姓名: '||emp_record.ename||' ,部门号 '||emp_record.deptno||
    ',原工资: '||emp_record.sal||',新工资: '||(emp_record.sal+200));
    else
    dbms_output.put_line('姓名: '||emp_record.ename||', 部门号:'||emp_record.deptno||
                         ',工资: '||emp_record.sal);
    end if;
    end loop;
    end;
    /

    anonymous block completed
    姓名: SMITH, 部门号:20,工资: 880
    姓名: ALLEN ,部门号 30,原工资: 1600,新工资: 1800
    姓名: WARD ,部门号 30,原工资: 1250,新工资: 1450
    姓名: JONES, 部门号:20,工资: 3272.5
    姓名: MARTIN ,部门号 30,原工资: 1250,新工资: 1450
    姓名: BLAKE ,部门号 30,原工资: 2850,新工资: 3050
    姓名: CLARK, 部门号:10,工资: 2450
    姓名: SCOTT, 部门号:20,工资: 2000
    姓名: KING, 部门号:10,工资: 5000
    姓名: TURNER ,部门号 30,原工资: 1500,新工资: 1700
    姓名: ADAMS, 部门号:20,工资: 1210
    姓名: JAMES ,部门号 30,原工资: 950,新工资: 1150
    姓名: FORD, 部门号:20,工资: 3300
    姓名: MILLER, 部门号:10,工资: 2100


    -------------------------------------------

    作者:赵杰迪

    -------------------------------------------

  • 相关阅读:
    redis集群规范
    mongodb的基本使用
    redis进阶
    redis基本使用
    selenium的基本使用
    C++入门
    C语言入门
    MATLAB中矩阵reshape的顺序规律
    Tensorflow:ImportError: DLL load failed: 找不到指定的模块 Failed to load the native TensorFlow runtime
    差分定位和精密定位
  • 原文地址:https://www.cnblogs.com/zhaojiedi1992/p/oracle11g_sql_0014.html
Copyright © 2011-2022 走看看