zoukankan      html  css  js  c++  java
  • oracle cursor


    -------- begining of the structure of test --------
    /*
    userId    userName
    1    John1
    2    John2
    3    John3
    4    John4
    5    John5
    */

    ---------------- part 1 ----------------
    declare
        cursor v_cursor is select * from test;
        v_record test%rowtype;

    begin
        open v_cursor;
       
        fetch v_cursor into v_record;
        while v_cursor%FOUND LOOP
            dbms_output.put_line(v_record.userid || ',' ||v_record.username);
            fetch v_cursor into v_record;
        end loop;
       
        close v_cursor;
    end;

    -- result
    /*
    1,John1
    2,John2
    3,John3
    4,John4
    5,John5
    */

    ---------------- part 2 ----------------
    declare
        cursor v_cursor(userId number, userName varchar2) is select userid, userName from test where userId = 1;
        v_userId test.userid%type;
        v_userName test.username%type;
       
    begin
        -- 这里temp是随便输入的一个字串,
        -- 可能是由于上面游标是针对字段定义的.
        open v_cursor(1,'temp');
       
        loop
            fetch v_cursor into v_userId, v_userName;
            exit when v_cursor%NOTFOUND;
            dbms_output.put_line(v_userId || '-' || v_userName);              
        end loop;
       
        close v_cursor;
    end;

    ---------------- part 3 ----------------
    --由于不需要open cursor,因为最高效。
    declare
        cursor v_cursor(userName varchar2) is select userName from test where userId = 1; 
        
    begin  
        for v_temp_cursor in v_cursor('temp') loop
            dbms_output.put_line(v_temp_cursor.username);
        end loop;
    end;

    ---------------- ISOPEN ----------------
    declare
        cursor v_cursor(username varchar2) is select userName from test where userId = 1;
        v_userName test.username%type;
       
    begin
        if v_cursor%ISOPEN then
           dbms_output.put_line('cursor is opened');
        else
           open v_cursor('temp');
        end if;
       
        fetch v_cursor into v_userName;
        close v_cursor;
       
        dbms_output.put_line(v_userName);
    END;

    ---------------- ROWCOUNT ----------------
    declare   
        cursor v_cursor(username varchar2) is select userName from test where userId > 1;
        v_userName test.username%type;

    begin
        open v_cursor('temp');
       
        loop
            fetch v_cursor into v_userName;
            exit when v_cursor%NOTFOUND OR v_cursor%NOTFOUND IS null;
            dbms_output.put_line('record number is '||v_cursor%ROWCOUNT);
        end loop;
       
        close v_cursor;
    END;

    ---------------- 更新数据 ----------------
    declare
        cursor v_cursor is select username from test for update;
        v_userName test.username%type;

    begin
        open v_cursor;
       
        fetch v_cursor into v_userName;
        while v_cursor%FOUND Loop
            update test set username =  v_userName || userId where current of v_cursor;
            fetch v_cursor into v_userName;
        end loop;
       
        close v_cursor;
    end;

    ---------------- 隐式游标 ----------------
    begin
        for v_cursor in(select username from test) loop
            dbms_output.put_line(v_cursor.username);
        END LOOP;
    end;

  • 相关阅读:
    TinyEditor:简洁且易用的html所见即所得编辑器
    arguments.callee 调用自身
    java.io.IOException: 设备未就绪
    关于vcastr3播放器自动播放的问题
    javascript中IE浏览器不支持NEW DATE()带参数的解决方法
    Oracle常用查看表结构命令
    java通过文件头内容判断文件类型
    RS开发日期提示控件默认为昨天
    Cognos更新问题之利用Transform实现Cube增量更新
    SqlServer中从字符串中获取项目指标方法charindex月substring结合
  • 原文地址:https://www.cnblogs.com/aspsmile/p/1278499.html
Copyright © 2011-2022 走看看