• 【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;
    /

  • 相关阅读:
    015.现场.快用Scala(4月)
    014.科普.有生产力的Sql语句
    JQuery速记
    草稿
    使用jquery.layout.js构建页眉/页脚/左侧导航/中间展示内容的网页结构
    实践自己的WebSite______流水
    如何生成带注释的DLL文件
    MVC如何在解决方案下创建文件夹
    如何利用子视图
    【草稿】JS中如何操作时间
  • 原文地址:https://www.cnblogs.com/greenZ/p/8721850.html
Copyright © 2020-2023  润新知