zoukankan      html  css  js  c++  java
  • Oracle存储过程游标for循环怎么写

    一、不带参数的游标for循环

    首先编写存储过程的整体结构,如下:

    create or replace procedure test_proc is
    
      v_date date; --变量定义
    
    begin
    
      select sysdate into v_date from dual;
    
    end test_proc;

    定义游标:

    create or replace procedure test_proc is
    
      v_date date; --定义变量
    
      cursor cur is select * from ldcode; --定义游标
    
    begin
    
      select sysdate into v_date from dual;
    
    end test_proc;

    编写for循环:

    create or replace procedure test_proc is
    
      v_date date; --定义变量
    
      cursor cur is select * from ldcode where rownum<10; --定义游标
    
    begin
    
      select sysdate into v_date from dual;
    
      --游标for循环开始
    
      for temp in cur loop --temp为临时变量名,自己任意起
    
        Dbms_Output.put_line(temp.Code); --输出某个字段,使用"变量名.列名"即可。
    
      end loop;
    
      --游标for循环结束
    
    end test_proc;

    测试运行,点击【DBMS Output】标签页查看结果如下图:

    二、带参数的游标for循环

    定义带参数的游标:

    cursor cur(v_codetype ldcode.Codetype%TYPE) is
    
        select * from ldcode where codetype = v_codetype; --定义游标

    定义游标格式:

    cursor 游标名称(变量定义) is 查询语句;

    注意:

    where条件中的变量名v_codetype要与游标定义cur(v_codetype ldcode.Codetype%TYPE)中的一致。

    编写for循环部分:

     --游标for循环开始

      for temp in cur('llmedfeetype') loop
    
        --temp为临时变量名,自己任意起
    
        --cur('llmedfeetype')为"游标名称(传入的变量)"
    
        Dbms_Output.put_line(temp.Code); --输出某个字段,使用"变量名.列名"即可。
    
      end loop;

      --游标for循环结束

    测试运行,点击【DBMS Output】标签页查看结果如下图:

  • 相关阅读:
    为什么要使用智能指针?
    C++如何定义一个函数指针
    Python三个处理excel表格的库
    Python的一个mysql实例
    Python利用xlutils统计excel表格数据
    PHP连接数据库的方式
    利用xlutils第三方库复制excel模板
    Python自动化办公第三方库xlwt
    Python之excel第三方库xlrd和xlwt
    Python生成器
  • 原文地址:https://www.cnblogs.com/xwb583312435/p/9054973.html
Copyright © 2011-2022 走看看