讲函数之前,先介绍一下程序结构

3.程序结构

  新建一个测试窗口,举一个小例子

declare
-- 声明变量,包括游标
begin
-- 执行部分
dbms_output.put_line('hello world!'); --异常处理
end;

  变量声明时,类型可以用字段类型,也可以直接引用表的字段类型 

  举个小例子:

declare
-- 声明变量,包括游标
pagename varchar2(10);
lang1 langmap.lang1%type;
begin
-- 执行部分 select l.pagename, l.lang1
into pagename, lang1
from langmap l
where l.pagename = 'repair'
and l.msgid = '';
dbms_output.put_line('配置项目:' || pagename || ' 状态:' || lang1); --异常处理
end;

  或者直接引用表的行类型

  举个小例子:

declare
-- 声明变量,包括游标
v_langmap langmap%rowtype;
begin
-- 执行部分 select *
into v_langmap
from langmap l
where l.pagename = 'repair'
and l.msgid = '';
dbms_output.put_line('配置项目:' || v_langmap.pagename || ' 状态:' ||
v_langmap.lang1); --异常处理
end;

4.判断

begin
if 条件 then 内容 elsif 条件 then 内容 else 内容 end if;
end;

5.循环

begin
loop
exit when 条件 --语法部分 end loop;
end;

6.游标

  不带参

declare
cursor c_notice is select t.noticeno,t.title from tenantnotice t;
v_no tenantnotice.noticeno%type;
v_title tenantnotice.title%type;
begin
--打开游标
open c_notice; --遍历游标
loop
fetch c_notice into v_no,v_title;
exit when c_notice%notfound;
dbms_output.put_line('编号:'|| v_no||',标题:'|| v_title);
end loop; --关闭游标
close c_notice;
end;

  带参

declare
cursor c_notice(v_noticeno tenantnotice.noticeno%type)
is select t.noticeno,t.title from tenantnotice t where t.noticeno=v_noticeno;
v_no tenantnotice.noticeno%type;
v_title tenantnotice.title%type;
begin
--打开游标
open c_notice(''); --遍历游标
loop
fetch c_notice into v_no,v_title;
exit when c_notice%notfound;
dbms_output.put_line('编号:'|| v_no||',标题:'|| v_title);
end loop; --关闭游标
close c_notice;
end;

  select  into 是隐式游标,只能返回一行(存储过程输出值)

7.存储过程

  

  存储过程没有declare,变量直接在is(或者as,相同的效果)和begin之间声明就行

  1)无参无返回值

create or replace procedure kxy_Proc_hello is
--这里可以声明变量
begin
dbms_output.put_line('hello world');
end kxy_Proc_hello;

  可在测试脚本中直接调用

begin
kxy_Proc_hello;
end;

  也可以在命令行中调用

exec kxy_Proc_hello;

  命令行有时候看不到结果,先执行

set serveroutput on

  2)带参无返回值

create or replace procedure kxy_Proc_GetTitle(i_no in tenantnotice.noticeno%type)
is
v_title tenantnotice.title%type;
begin
select t.title into v_title from tenantnotice t where t.noticeno=i_no;
dbms_output.put_line(v_title);
end;

  3)无参有返回值

  4)带参有返回值

create or replace procedure kxy_Proc_GetTitle(i_no in tenantnotice.noticeno%type,o_title out tenantnotice.title%type)
is
begin
select t.title into o_title from tenantnotice t where t.noticeno=i_no;
end;

  调用

declare
v_title tenantnotice.title%type;
begin
kxy_proc_gettitle('', v_title);
-- kxy_proc_gettitle('',o_title => v_title);--这样写也可以
dbms_output.put_line(v_title);
end;

8.函数

  函数必须有返回值,可在sql语句中直接调用,可以不传参,也可以使用 in/out参数 输入输出

  1)无参

create or replace function kxy_Fun_hello
return varchar2
is
--这里可以声明变量
msg varchar2():= 'hello world';
begin
return msg;
end; select kxy_Fun_hello() from dual;

Oracle 自定义函数、存储过程的更多相关文章

  1. Oracle自定义函数和存储过程示例,自定义函数与存储过程区别

    参考资料:http://www.newbooks.com.cn/info/60861.html oracle自定义函数学习和连接运算符(||) 贴一段中文文档示例,应该就可以开始工作了: --过程(P ...

  2. Mybatis下配置调用Oracle自定义函数返回的游标结果集

    在ibatis和Mybatis对存储过程和函数函数的调用的配置Xml是不一样的,以下是针对Mybatis 3.2的环境进行操作的. 第一步配置Mapper的xml内容 <mapper names ...

  3. 【转】Oracle 自定义函数语法与实例

    原文地址:https://blog.csdn.net/libertine1993/article/details/47264211 Oracle自定义函数的语法如下: create or replac ...

  4. Oracle自定义函数1

    用户定义函数是存储在数据库中的代码块,可以把值返回到调用程序.调用时如同系统函数一样,如max(value)函数,其中,value被称为参数.函数参数有3种类型. IN 参数类型:表示输入给函数的参数 ...

  5. Oracle自定义函数

    核心提示:函数用于返回特定数据.执行时得找一个变量接收函数的返回值; 语法如下: create or replace function function_name ( argu1 [mode1] da ...

  6. 基本开题的感觉是了-MySQL继续继续(自定义函数&存储过程)

    hi 感觉论文开题基本确定了,凯森 1.MySQL -----自定义函数----- ----基本 两个必要条件:参数和返回值(两者没有必然联系,参数不一定有,返回一定有) 函数体:合法的SQL语句:以 ...

  7. Oracle自定义函数&加密

    在sql中频繁使用的功能(逻辑.加密等)可以写成自定义函数进行封装,之后再调用即可. CREATE OR REPLACE FUNCTION "函数名" (参数名 参数类型 参数数据 ...

  8. oracle自定义函数:将使用点分隔符的编码转成层级码格式的编码

    维护一个旧的系统,表设计中只有编码,而没有其他排序相关的字段,然后根据编码排序出现了顺序错乱的问题. 详细地说,其编码设计是使用[.]分隔符的编码,比如1.1.1.1.1.1.1.1.1.2这样的格式 ...

  9. Oracle 自定义函数实现列转行效果

    在 Oracle 领域,我相信一说到列转行大部分人都会立马想到 WM_CONCAT 函数,我觉得主要是因为该函数比较实用.但事实上 WM_CONCAT 并非官方公开函数,使用会存在一定的风险:函数返回 ...

  10. sqlserver中自定义函数+存储过程实现批量删除

    由于项目的需要,很多模块都要实现批量删除的功能.为了方便模块的调用,把批量删除方法写成自定义函数.直接上代码. 自定义函数: ALTER FUNCTION [dbo].[func_SplitById] ...

随机推荐

  1. web开发-Django博客系统

    项目界面图片预览 项目代码github地址 项目完整流程 项目流程: 1 搞清楚需求(产品经理) (1) 基于用户认证组件和Ajax实现登录验证(图片验证码) (2) 基于forms组件和Ajax实现 ...

  2. Openwrt无线中继设置并访问外网

    Openwrt无线中继设置并访问外网 本篇博文参考来自:http://blog.csdn.net/pifangsione/article/details/13162023 配置目标 主路由器使用AP模 ...

  3. (九)Delete an Index

    Now let’s delete the index that we just created and then list all the indexes again: 现在让我们删除刚刚创建的索引, ...

  4. JDB与迭代

    要求 1 使用C(n,m)=C(n-1,m-1)+C(n-1,m)公式进行递归编程实现求组合数C(m,n)的功能 2 m,n 要通过命令行传入 3 提交测试运行截图(至少三张:正常如c(3,2).异常 ...

  5. Linux 修改本地时间 (centos为例)

    1.  tzselect [root@xxxx etc]# tzselect --- 选择时区命令 Please identify a location so that time zone rules ...

  6. Mantis中文网

    Mantis中文网 | Mantis安装.Mantis使用.Mantis中文http://www.mantis.org.cn/ Mantis Bug Tracker | Demohttp://www. ...

  7. mybatis中使用到的设计模式

    Mybatis中使用到了哪些设计模式呢?下面就简单的来介绍下: 1.构造者模式: 构造者模式是在mybatis初始化mapper映射文件的过程中,为<cache>节点创建Cache对象的方 ...

  8. Ubuntu 14.04 mame sound fix

    sudo vi '/etc/mame/mame.ini' samplerate 22050

  9. jap篇 之 JSTL标签库

    JSTL标签库: JSTL: JSP Standard Tag Library 作用:和[EL配合]使用,可以让用户[尽可能少的使用java源码]. 1,导入jar包 导入(复制粘贴到项目中的lib目 ...

  10. [模板] 后缀自动机&&后缀树

    后缀自动机 后缀自动机是一种确定性有限状态自动机, 它可以接收字符串\(s\)的所有后缀. 构造, 性质 翻译自毛子俄罗斯神仙的博客, 讲的很好 后缀自动机详解 - DZYO的博客 - CSDN博客 ...