zoukankan      html  css  js  c++  java
  • 【oracle】补充 cursor 基本例子

    1118-02补充 cursor 基本例子

    查看 共享的命令:
    cmd
    fsmgmt.msc

    /*
    cursor
    1.参数列表,is <select_statement> 是否含有 变量
    2.接收游标值
    2.1 标量类型变量
    2.2 记录类型变量
    2.3 for循环中的循环计数变量
    3.游标属性
    3.1 显式游标,ref cursor 语法: 游标名%游标属性
    3.2 隐式游标 语法: SQL%游标属性
    */

    set serveroutput on;
    /*cursor,无参,接收游标值:1.标量类型变量*/
    declare
    cursor v_cur is
    select serial_no,type_code,brand_code,brand_name from t_sales;
    vi_serial_no integer;
    vv_type_code varchar2(30);
    vv_brand_code varchar2(30);
    vv_brand_name varchar2(30);
    begin
    open v_cur;
    loop
    fetch v_cur into vi_serial_no,vv_type_code,vv_brand_code,vv_brand_name;
    exit when v_cur%notfount;
    dbms_output.put_line( vi_serial_no||','||vv_type_code||','||vv_brand_code||','||vv_brand_name );
    end loop;
    close v_cur;
    exception
    dbms_output.put_line(sqlcode||sqlerrm);
    end;
    /

    set serveroutput on;
    /*cursor,无参,接收游标值:2.记录类型变量*/
    declare
    cursor v_cur is
    select serial_no,type_code,brand_code,brand_name from t_sales;
    type t_record is record (
    serial_no integer,
    type_code varchar2(30),
    brand_code varchar2(30),
    brand_name varchar2(30)
    );
    v_record t_record; --记录类型 的显式定义
    --v_record t_sales%rowtype; --记录类型 的隐式定义(table)
    -- v_record v_t_sales%rowtype; --记录类型 的隐式定义(view)
    --v_record v_cur%rowtype; --记录类型 的游标定义
    begin
    open v_cur;
    loop
    fetch v_cur into v_record;
    -- fetch v_cur into v_record.serial_no,v_record.type_code,v_record.brand_code,v_record.brand_name;
    exit when v_cur%notfount;
    dbms_output.put_line( vi_serial_no||','||vv_type_code||','||vv_brand_code||','||vv_brand_name );
    end loop;
    close v_cur;
    exception
    dbms_output.put_line(sqlcode||sqlerrm);
    end;
    /

    set serveroutput on;
    /*cursor,无参,接收游标值:3.for循环中的循环计数标量*/
    declare
    cursor v_cur is
    select serial_no,type_code,brand_code,brand_name from t_sales;
    begin
    for i_row in v_cur loop
    dbms_output.put_line( i_row.serial_no||','||i_row.type_code||','||i_row.brand_code||','||i_row.brand_name );
    end loop;
    exception
    dbms_output.put_line(sqlcode||sqlerrm);
    end;
    /

    set serveroutput on;
    /*cursor,有参,无变量*/
    declare
    cursor v_cur(vp_serial_no integer default 5) is
    select serial_no,type_code,brand_code,brand_name from t_sales where serial_no <= vp_serial_no;
    begin
    for i_row in v_cur(3) loop
    dbms_output.put_line( i_row.serial_no||','||i_row.type_code||','||i_row.brand_code||','||i_row.brand_name );
    end loop;
    exception
    dbms_output.put_line(sqlcode||sqlerrm);
    end;
    /

    set serveroutput on;
    /*cursor,有参,有变量*/
    declare
    vv_type_code varchar2(30);
    cursor v_cur(vp_serial_no integer default 5) is
    select serial_no,type_code,brand_code,brand_name from t_sales where serial_no <= vp_serial_no and type_code = upper(vv_type_code);
    v_record v_cur%rowtype;
    begin
    vv_type_code := 'T01'; --open前 赋值
    --open v_cur; --参数列表 有默认值的游标,open时可以不带参数列表
    open v_cur(3);
    loop
    fetch v_cur into v_record;
    exit when v_cur%notfount;
    dbms_output.put_line( v_record.serial_no||','||v_record.type_code||','||v_record.brand_code||','||v_record.brand_name );
    end loop;
    close v_cur;
    exception
    dbms_output.put_line(sqlcode||sqlerrm);
    end;
    /


    set serveroutput on;
    /*游标属性:1.显式游标,ref cusor*/
    declare
    cursor v_cur is
    select serial_no,type_code,brand_code,brand_name from t_sales;
    v_record v_cur%rowtype;
    begin
    if not (v_cur%isopen) then
    open v_cur;
    end if;

    fetch v_cur into v_record;
    while v_cur%found loop
    dbms_output.put_line( v_cur%rowcount||':'||v_record.serial_no||','||v_record.type_code||','||v_record.brand_code||','||v_record.brand_name );
    fetch v_cur into v_record;
    end loop;
    exception
    dbms_output.put_line(sqlcode||sqlerrm);
    end;
    /

    set serveroutput on;
    /*游标属性: 2.隐式游标*/
    declare
    type t_record is record (
    serial_no integer,
    type_code varchar2(30),
    brand_code varchar2(30),
    brand_name varchar2(30)
    );
    v_record t_record;
    begin
    select serial_no,type_code,brand_code,brand_name into v_record from t_sales where serial_no = 1;
    dbms_output.put_line(to_char(SQL%rowcount));
    update t_sales set brand_name = brand_name||'_01';
    dbms_output.put_line(to_char(SQL%rowcount));
    --commit;
    rollback;
    dbms_output.put_line(to_char(SQL%rowcount));
    exception
    dbms_output.put_line(sqlcode||sqlerrm);
    end;
    /


    /* ref cursor
    定义:
    1.select语句
    2.sql字符串
    */
    set serveroutput on;
    /*ref cursor,指定游标语句:1.select语句*/
    declare
    type t_ref_cursor is ref cursor;
    v_ref_cur t_ref_cursor;
    vi_serial_no integer := 5;
    v_record t_sales%rowtype;
    begin
    open v_ref_cur for
    select serial_no,type_code,brand_code,brand_name into v_record from t_sales where serial_no <= vi_serial_no;
    loop
    fetch v_ref_cur into v_record;
    exit when v_reg_cur%notfound;
    dbms_output.put_line(v_record.serial_no||','||v_record.type_code||','||v_record.brand_code||','||v_record.brand_name );
    end loop;
    exception
    dbms_output.put_line(sqlcode||sqlerrm);
    end;
    /

    set serveroutput on;
    /*ref cursor,指定游标语句:2.sql字符串*/
    declare
    type t_ref_cursor is ref cursor;
    v_ref_cur t_ref_cursor;
    vi_serial_no integer := 5;
    v_record t_sales%rowtype;
    vv_sql varchar2(500);
    begin
    vi_serial_no := 3;
    /*使用 普通变量
    vv_sql := ' select serial_no,type_code,brand_code,brand_name into v_record from t_sales where serial_no <= '||vi_serial_no ;
    open v_ref_cur for vv_sql;
    */
    /*使用 绑定变量*/
    vv_sql := ' select serial_no,type_code,brand_code,brand_name into v_record from t_sales where serial_no <= :serial_no' ;
    open v_ref_cur for vv_sql using vi_serial_no; --open时指定绑定变量的值
    loop
    fetch v_ref_cur into v_record;
    exit when v_reg_cur%notfound;
    dbms_output.put_line(v_record.serial_no||','||v_record.type_code||','||v_record.brand_code||','||v_record.brand_name );
    end loop;
    exception
    dbms_output.put_line(sqlcode||sqlerrm);
    end;
    /

  • 相关阅读:
    Idea创建Hibernate bean类和.xml文件
    Hibernate 一对多,多对多,多对一检索策略
    Hibernate 性能优化一对一关联映射
    HashSet 与TreeSet和LinkedHashSet的区别
    使用LinkedList模拟一个堆栈或者队列数据结构
    设计模式------工厂模式和抽象工厂模式
    悟透JavaScript
    JS产生随机数的几个用法!
    防盗链
    nigin配置安全:三个案例看Nginx配置安全(转)
  • 原文地址:https://www.cnblogs.com/greenZ/p/8721850.html
Copyright © 2011-2022 走看看