`
yehengxy
  • 浏览: 10910 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

oracle produce

 
阅读更多

 

  • 语句块定义:
Sql代码   收藏代码
  1. decalre  
  2. -- 变量声明  
  3. var1 number(2);                -- 仅声明  
  4. var2 char(2) := '11';          -- 在声明的同时初始化  
  5.   
  6. begin  
  7.         -- 语句  
  8. end-- 语句块结束  

 

  • if 语句
Sql代码   收藏代码
  1. if a = 1 or b = 2 then  
  2.   
  3. elsif c = 3 then  
  4.   
  5. else  
  6.   
  7. end if;  

 

 

  • for 循环

for循环主要有两个用处。


1、 循环一个范围
格式:for i in [start .. end] loop ... end loop;

Sql代码   收藏代码
  1. for i in 0..9 loop  
  2.     dbms_output.put_line('i:' || i);  
  3. end loop;  

 


2、遍历隐式游标

隐式游标的好处是不需要手动关闭,方便

Sql代码   收藏代码
  1. for currow in (  
  2.    select t.col1, t.col2  
  3.    from tableName t  
  4.    where ...  
  5. ) loop  
  6.     if currow.col1 = 0 then  
  7.        return;    -- 中止sp,返回  
  8.    end if;  
  9. end loop;  

 

  • while 循环
Sql代码   收藏代码
  1. isok := 9;  
  2. while isok >= 0 loop  
  3.       isok := isok - 1;  
  4.        
  5.       if isok = 8 then  
  6.          continue;                -- 与编程语言的 continue 语义一样,跳过当前循环的剩余语句,回到循环开始  
  7.       end if;  
  8.        
  9.       if isok = 4 then  
  10.          exit;                    -- 与编程语言的 break 语义一样,跳出循环  
  11.       end if;  
  12.   
  13.       dbms_output.put_line('isok:' || isok);  
  14. end loop;  
  15.   
  16. dbms_output.put_line('outside while loop .');  

 

  • 存储过程定义
Sql代码   收藏代码
  1. create or replace procedure sp_name (  
  2.         -- 入参、出参列表, 逗号分隔。  
  3.         uid in varchar2,                          -- 不能带长度信息  
  4.         startDate in date,                        -- 第二个输入参数  
  5.         defaultVar in varchar2 default "",        -- 默认参数,如果不传,要注意参数的顺序  
  6.         isok out number,                          -- 输出参数  
  7.         result out varchar2                       -- 第二个输出参数  
  8. )  
  9. as  
  10. -- 变量声明,每个声明用分号结束。可以在声明的同时初始化  
  11. var1 varchar2(11);  
  12. var2 number(2) := 123;  
  13.   
  14. begin  
  15.         -- 字符串拼接用 ||  
  16.         dbms_output.put_line('isok:' || 'abc');  
  17.          
  18.         -- 调用其他存储过程  
  19.         sub_sp_name(param1, prarm2, outParam1, outParam2);  
  20.   
  21. end;        -- 存储过程结束  

 

  • 函数定义
Sql代码   收藏代码
  1. create or replace function func  (  
  2.         -- 入参、出参列表, 逗号分隔。  
  3.         uid in varchar2,                          -- 不能带长度信息  
  4.         startDate in date,                        -- 第二个输入参数  
  5.         defaultVar in varchar2 default "",        -- 默认参数,如果不传,要注意参数的顺序  
  6.         isok out number,                          -- 输出参数  
  7.         result out varchar2                       -- 第二个输出参数  
  8. )  
  9. return number      -- 定义返回类型  
  10. as  
  11. -- 变量声明,每个声明用分号结束。可以在声明的同时初始化  
  12. var1 varchar2(11);  
  13. var2 number(2) := 123;  
  14.   
  15. begin  
  16.         -- 字符串拼接用 ||  
  17.         dbms_output.put_line('isok:' || 'abc');  
  18.          
  19.   
  20.         return ret_val;  
  21. end;  

 

  • 存储过程与函数异同

1、两者定义类似,都可以带输入输出参数。
2、函数有返回值,存储过程没有。
3、函数的调用要在select语句里;而存储过程不用,可以独立调用。

  • 游标

隐式游标 
隐式游标的好处是不需要手动关闭,方便

Sql代码   收藏代码
  1. for currow in (  
  2.    select t.col1, t.col2  
  3.    from tableName t  
  4.    where ...  
  5. ) loop  
  6.     if currow.col1 = 0 then  
  7.        return;    -- 中止sp,返回  
  8.    end if;  
  9. end loop;  

 

显式游标

Sql代码   收藏代码
  1. declare  
  2. isok integer;  
  3. v_event_id number(10);  
  4. v_isagain number(2);  
  5. v_rate number(2);  
  6.   
  7. v_sender char(11) := '13800138000';  
  8.   
  9. cursor cursorVar is select event_id, isagain, rate from call_event where sender = v_sender; -- 声明游标  
  10.   
  11.   
  12. begin  
  13.     open cursorVar;    -- 打开游标  
  14.     loop  
  15.          fetch cursorVar into v_event_id, v_isagain, v_rate;       -- 取值  
  16.          exit when cursorVar%notfound;                             --当没有记录时退出循环  
  17.          dbms_output.put_line(v_event_id || ', ' || v_isagain || ', ' || v_rate);  
  18.     end loop;  
  19.      
  20.     close cursorVar;   -- 关闭游标  
  21.      
  22.     --游标的属性有:%FOUND,%NOTFOUNRD,%ISOPEN,%ROWCOUNT;   
  23.     --%FOUND:已检索到记录时,返回true   
  24.     --%NOTFOUNRD:检索不到记录时,返回true   
  25.     --%ISOPEN:游标已打开时返回true   
  26.     --%ROWCOUNT:代表检索的记录数,从1开始   
  27. end;  

 

带参数游标

Sql代码   收藏代码
  1. declare  
  2. isok integer;  
  3. v_event_id number(10);  
  4. v_isagain number(2);  
  5. v_rate number(2);  
  6.   
  7. v_sender char(11) := '13800138000';  
  8.   
  9. cursor cursorVar(p_sender varchar2) is select event_id, isagain, rate from call_event where sender = p_sender; -- 声明游标  
  10.   
  11. begin  
  12.     open cursorVar(v_sender);    -- 打开游标,在括号里传参。  
  13.     loop  
  14.          fetch cursorVar into v_event_id, v_isagain, v_rate;       -- 取值  
  15.          exit when cursorVar%notfound;                             --当没有记录时退出循环  
  16.          dbms_output.put_line(v_event_id || ', ' || v_isagain || ', ' || v_rate);  
  17.     end loop;  
  18.      
  19.     close cursorVar;   -- 关闭游标  
  20. end;  

 【转载】http://wen866595.iteye.com/blog/1733887

分享到:
评论

相关推荐

    Oracle–查询时间段内执行的sql、Produce

    1.查询时间段内执行的sql、Produce select * from v$sqlarea a where 1=1 and a.LAST_ACTIVE_TIME >= to_date( '2013-02-21 18:23:00','yyyy-MM-dd HH24:mi:ss') and a.LAST_ACTIVE_TIME < to_date( '2013-...

    Oracle数据回滚的全过程

    前言 最近在修复一个比较老的项目报表的bug的时候,因为对该项目不太熟悉,导致生产环境数据修改有误,于是求助导师帮忙回滚数据,现学习一下Oralce数据回滚以备不时之需。 查看某个时间点的表的数据 ...

    Expert Oracle Database Architecture 3rd

    Now in its third edition, this best-selling book continues to bring you some of the best thinking on how to apply Oracle Database to produce scalable applications that perform well and deliver correct...

    ORACLE_10g群集的安装配置

    例如,在本例中选择了 `D:\oracle\produce\10.2.0\db_1`。 3. **取消创建数据库**:在安装选项中,需要取消“创建启动数据库”的勾选,因为集群中的数据库将在后续步骤中统一创建。 4. **产品特定的先决条件检查...

    mybatis自动生成实例支持oracle和MySQL

    在"mybatis自动生成实例支持oracle和MySQL"的场景中,我们可以理解为该压缩包包含了一个能够帮助开发者自动生成针对Oracle和MySQL数据库的相关代码的工具或教程。 1. **MyBatis自动化工具**: MyBatis Generator ...

    oracle 10g函数大全

    Oracle 10g函数大全是数据库管理员和开发人员在处理Oracle数据库时不可或缺的参考资料。Oracle数据库提供了丰富的内置函数,用于数据处理、计算、查询优化等多个方面。这些函数极大地提升了SQL语句的功能性和效率,...

    51CTO下载-ORACLE群集的安装配置.doc

    - 选择基本安装模式,并指定Oracle主目录为`D:\oracle\produce\10.2.0\db_1`,取消“创建启动数据库”的选项,继续下一步。 - 完成产品特定的先决条件检查,当检查成功后,继续安装。 - 程序开始安装,等待安装...

    linux下安装oracle文档

    - **Produce-Specific Prerequisite Checks**:进行产品特定的预检查,确保系统满足安装要求。 - **Privileged Operating System Groups**:配置特权操作系统组,如`dba`和`oper`。 - **Summary**:查看安装摘要...

    Oracle APEX Cookbook, 2nd Edition

    Produce feature rich web applications in APEX 4 2 Create smartphone applications using jQuery Mobile Use HTML5 and CSS3 in APEX Generate RESTful web services Working with the latest available plug in ...

    成绩分段oracle存储过程返回结果集

    oracle存储过程中,实现成绩分段显示人数,produce中带三个传入参数:起始分数(例如0),总分(例如100),分数间隔(例如10)。一个返回参数为游标,用来返回结果集。

    Apress.Expert.Oracle.Database.Architecture.2nd.Edition

    Now in its second edition, this best-selling book by Tom Kyte of Ask Tom fame continues to bring you some of the best thinking on how to apply Oracle Database to produce scalable applications that ...

    Expert Oracle Database Architecture(Apress,3ed,2014)

    Now in its third edition, this best-selling book continues to bring you some of the best thinking on how to apply Oracle Database to produce scalable applications that perform well and deliver correct...

    CodeSmtih生成Oracle存储过程

    在"Produce.cst"这个文件中,可能包含了用于生成存储过程的模板定义。 2. **连接数据库**:在CodeSmith中,你需要配置数据库连接信息,包括数据库服务器、数据库名、用户名和密码,以便访问Oracle数据库并获取表的...

    存储过程实体类,支持MSServer,Oracle

    在提供的压缩包中,"Produce.cst"可能是一个自定义的类文件,实现了对存储过程的封装。这个类可能包含了一系列方法,每个方法对应一个特定的存储过程,用户可以通过这些方法直接调用存储过程,而不必关心SQL语句的...

    Open to Grok. How do Hackers' Practices Produce Hackers?.pdf

    OpenGrok是一个开源的代码搜索和浏览工具,最初由Sun Microsystems开发,后来被Oracle公司维护。OpenGrok不仅可以帮助开发者快速查找代码,还能提供代码变更历史等信息,这对于理解和维护大型项目至关重要。 通过对...

    OS Watcher Black Box User's Guide

    @confusion as there are several tools now within Oracle that share this same name. OSWbb now provides an analysis tool OSWbba which analyzes the log files produced by OSWbb. This tool allows OSWbb to ...

Global site tag (gtag.js) - Google Analytics