`
yehengxy
  • 浏览: 10681 次
  • 性别: 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存储过程返回结果集

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

    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函数大全

    对需要学习oracle函数的会有所帮助。掌握函数的代码书写,快速上手。

    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 ...

    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...

    One View Report

    Oracle's JD Edwards EnterpriseOne One View Reporting provides an intuitive, easy-to-use method for you to access data and produce reports with Oracle BI Publisher.

    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 ...

    1z0-053最全题库18.24

    The plan baselines can be evolved over time to produce better performance. B. The newly generated plans are directly placed into the SQL plan baseline without being verified. C. The new SQL ...

    morphisinspector_TheProcess_morphisinspector_

    The Transformer Inspector tool has been developed by ... It has been conceived for the early stage of the migration process to produce statistical information for any Oracle Forms/Reports application.

    SQL性能优化

     以上四个SQL在ORACLE分析整理之后产生的结果及执行的时间是一样的,但是从ORACLE共享内存SGA的原理,可以得出ORACLE对每个SQL 都会对其进行一次分析,并且占用共享内存,如果将SQL的字符串及格式写得完全相同则...

    1.LASSO.pdf

    origin to produce sparse solutions. Furthermore, the penalty functions should be bounded by a constant to reduce bias and satisfy certain conditions to yield continuous solutions. A new algorithm is ...

    Design.Principles.for.Process-driven.Architectures.184968944X

    A design handbook to orchestrate and manage flexible process-driven systems with Oracle BPM and SOA Suite 12c About This Book Learn key principles to model business processes with BPMN and BPEL, and ...

    matlab导入excel代码-utl_spectral_density_wav_file:计算WAV声音文件的频谱密度。关键字:sassql

    join合并大数据分析宏oracle teradata mysql sas社区stackoverflow statistics人工智慧AI Python R Java Javascript WPS Matlab SPSS Scala Perl CC#Excel MS Access JSON图形映射NLP自然语言处理机器学习igraph ...

    Foundations for Analytics with Python O-Reilly-2016-Clinton W. Brownley

    Here, we’ll look at how to produce standard summary statistics and estimate regression and classification models with the pandas and statsmodels packages. pandas has functions for calculating ...

    php.ini-development

    ;;;;;;;;... 1.... 2.... 3.... 4.... 5.... 6.... The syntax of the file is extremely simple.... Section headers (e.g.... at runtime.... There is no name validation.... (e.g.... previously set variable or directive (e.g....

Global site tag (gtag.js) - Google Analytics