zoukankan      html  css  js  c++  java
  • oracle学习笔记(十九) 子程序——存储过程

    子程序——存储过程

    我们可以使用子程序来封装一下我们需要的操作,子程序又有存储过程,函数和触发器。
    这里先学习存储过程~

    语法

    create [or replace] procedure $procedure_name$ [(参数列表)]
    	is/as --相当于declare
    	begin
    		[exception]--异常处理
    	end $procedure_name$;
    	
    删除过程:drop procedure $procedure_name$
    

    创建存储过程

    创建无参数存储过程

    create or replace procedure hello
    	is
    	begin
    		dbms_output.put_line('hello world');
    	end hello;
    	/ --执行	
    

    创建带参存储过程

    参数有三种模式,参数类型定义不用定义宽度

    模式 说明
    in(默认) 模式的参数, 可以是字面量或变量形式传值
    out 模式的参数, 必须以变量形式传递,变量不应该赋值,接收过程中返回的结果
    in out 模式的参数, 必须以变量形式传递
    --输出指定字符串(使用in)
    create or replace procedure print(text in varchar2)
    	is
    	begin
    		dbms_output.put_line(text);
    	end print;
    	/
    	
    --计算结果,返回给调用者(使用out)
    create or replace procedure sum(num1 in int,num2 in int,result out int)
    	is
    	begin
    		result := num1 +num2;
    	end sum;
    /
    
    declare
    	--变量不应该赋值
    	v_result int := 5;
    begin
    	sum(5,6,v_result);
    	--上面的语句还可以这样写,这样便于直观的知道参数对应哪一个
    	--sum(num1 =>5,num2=>6,result=>v_result);
    	dbms_output.put_line(v_result);
    end;
    /
    
    --交换两个数字(使用in out)
    create or replace procedure swap(a in out int,b in out int)
    is
    	v_temp int ;
    begin
    	v_temp := a;
    	a := b;
    	b := v_temp;
    end swap;
    /
    
    declare
    	a int :=5;
    	b int := 9;
    begin
    	swap(a,b);
    	dbms_output.put_line(a||','||b);
    end;
    /
    

    执行

    PL/SQL执行

    begin
    	--包名.过程名
    	--未定义包名,不需要写包名
    	$procedure_name$;
    end;
    /
    
    --如果想要其他用户访问当前用户的存储过程,当前用户下,授权给其他用户权限
    grant execute on $procedure_name$ to $user_name$;
    

    命令行执行

    exec/execute $procedure_name$[(参数)]
    
  • 相关阅读:
    如何把.ipynb文件转化为.py文件?
    本地浏览器连接服务器端jupyter notebook服务
    本地浏览器下远程连接jupter notebook服务器
    ubuntu下如何设置环境变量
    ubuntu环境变量的设置
    ssh免密登录设置方法
    主机之间ssh免密码登录
    ubuntu与windows互传文件的3种方法
    ubuntu16.04安装Sogou输入法详细步骤
    Ubuntu下安装Sogou输入法
  • 原文地址:https://www.cnblogs.com/stars-one/p/10951517.html
Copyright © 2011-2022 走看看