Oracle存储过程(1)

(一)存储过程的基本语法

  1  CREATE OR REPLACE PROCEDURE 存储过程名

   IS

   BEGIN

  4  NULL;

   END;

(二)打印第一个Hello word

       1.存储过程

       create or replace procedure sayhelloword
      as
      --------说明部分
      begin
              dbms_output.put_line("Hello word");
      end;
      /

        打开控制台->切换路径d: >打开oracle数据库:sqlplus scott/[email protected]:1521/orcl > 清屏 host cls > 打开控制台输出开关 :set serveroutput  on 

      2.调用存储过程

         1.exec  sayhelloworld();
        2.begin
                       sayhelloworld();
                       sayhelloworld();
            end;
             /

      3.调用一个带参数的存储过程

         1.create  or replace procedure raisesalary(eno in number)
            as
           --定义一个变量保存涨前的薪水
           psal emp.sal%type;
           begin
           --得到涨之前的薪水
           select  sal into psal  from emp  where empno = eno;
           --给员工涨100
            update  emp set sal= sal+100 where empno = eno;
           --一般不在存储过程中commit或rollback
           --打印
          dbms_output.put_line('涨前:'||psal||'   涨后:'||(psal+100));
        end;
           /
           
          

  3.存储函数

          Function 为命名的存储程序  ,可带参数  返回计算值
          函数和过程的结构类似,但必须有一个return字句 ,用于返回函数值
         
         创建存储函数的语法
         create or replace  FUNCTION 函数名(参数列表)
         return  函数值类型
         AS
         PLSQL子程序体
        
           --存储函数:查询员工的年收入
            create or replace  function  queryempincome(eno  in number)
                   return  number
            as
            --定义变量保存员工的薪水和奖金
            psal  emp.sal%type
            pcommn  emp.comm%type;
            begin
                      select sal,comm into psal,pcomm from emp where empno=eno;
            --直接返回收入
            return psal*12+pcomm;
            end                                                             

     4.调试存储过程 

先用sys账户登录 密码
as sysdba
再对scott进行授权 grant DEBUG CONNECT SESSION ,DEBUG ANY PROCEDURE to scott;
        
         

猜你喜欢

转载自blog.csdn.net/sinat_34259781/article/details/78499110