zoukankan      html  css  js  c++  java
  • Oracle 存储过程入门(一)

    一,基本入门介绍

    公司系统需要用到oracle,可是还没在项目用过oracle,好吧,从基本学起。有问题的地方,欢迎指导啊。

    看创建存储过程的基本定义。注意,带有[]的都是可选的,可有可无的。只是语法能通过,当然根据自己需要处理。

    还是从简单例子开始学习,

    CREATE [OR REPLACE] PROCEDURE procedure_name
        [ (parameter [,parameter]) ]
    
    IS
        [declaration_section]
    
    BEGIN
        executable_section
    
    [EXCEPTION
        exception_section]
    
    END [procedure_name];
    View Code

    先初始化数据。我用的是Toad工具,下面的代码是在SQL Editor 中执行的。

    create table students
    ( 
      ID int,
      userName varchar(100),
      userPass varchar(100),
      userAge  int
    )
    
    insert into students values(1,'jack','jjjaa',23);
    insert into students values(2,'rose','jjjaa',21);
    insert into students values(3,'lucy','jjjaa',22);
    insert into students values(4,'Tony','jjjaa',24);
    commit;
    View Code

    当然,新建存储过程是需要在Procedure Editor中编写,但是执行存储过程又需要在SQL Editor中去执行,Procedure中是不可以执行(like exec的语句)的。

    这里我们新建一个存储过程,对于某个用户添加年龄,哈哈,当然这个是没什么意义的,学习,从简单入手。在实际开发中,语法,原理是一样的。

    create or replace procedure  SP_Update_Age
    (
     uName in varchar,--note,here don't have length ,sql have lenth ,not in oracle.
     Age in int
    )
    as
    begin
        update students set UserAge = UserAge + Age where userName = uName;
        commit;
    end SP_Update_Age;  
    View Code

    在执行存储过程之前,我们先查看原来的数据。

    select * from students
    
    
    /*********************
    
    ID    USERNAME    USERPASS    USERAGE
    
    1    jack            jjjaa        23
    2    rose            jjjaa        21
    3    lucy            jjjaa        22
    4    Tony            jjjaa        24
    
    **********************/
    View Code

    然后我们在SQL Editor中执行如下存储过程。注意看是怎么调用的:

    exec SP_UPDATE_AGE('jack',1);
    View Code

    执行之后,查看数据,

    select * from students;
    
    /********************
    
    ID    USERNAME    USERPASS    USERAGE
    
    1    jack    jjjaa    24  --noted,have changed 
    2    rose    jjjaa    21
    3    lucy    jjjaa    22
    4    Tony    jjjaa    24
    
    *********************/
    View Code

    二,基本语法介绍

    可以看出,基本的功能实现,调用完成。

    下面,来看看基本语法:

    1,变量赋值

     变量名 := 值;

    2,判断语句。

    if

    比较式

    then

    begin

    end;

    end

    if

    结合起来写个简单例子:

    create or replace procedure Test(x in out number)
    is
    begin
         if x<0 then
             begin
                 x:= 0 - x;
            end;
         elsif x > 0 then     --noted here elsif 
             begin
                 x:= x ;
            end;
         else
            x:=    0;
         end if;
    end Test;
    View Code

    Test:

    set serveroutput on;  --没这句话,看不到dmbs_output信息。
    declare
           num number;
    begin
        num:= -1;
        test(num);
        dbms_output.put_line( 'num = ' || num );
    end;
    /******************************
    num = 1
    PL/SQL procedure successfully completed.
    *******************************/
    View Code

    3,For循环,

    For  in ..loop;

    set serveroutput on;
    DECLARE
       x NUMBER := 100;
    BEGIN
       FOR i IN 1..10 LOOP  --noted here 
          IF MOD(i,2) = 0 THEN     -- i is even
              dbms_output.put_line( 'i: '||i||' is even ' );
          ELSE
              dbms_output.put_line('i: '|| i||' is odd' );
          END IF;
          x := x + 100;
          dbms_output.put_line('x value: '|| x);
       END LOOP;
       COMMIT;
    END;
    
    /*************************
    i: 1 is odd
    x value: 200
    i: 2 is even 
    x value: 300
    i: 3 is odd
    x value: 400
    i: 4 is even 
    x value: 500
    i: 5 is odd
    x value: 600
    i: 6 is even 
    x value: 700
    i: 7 is odd
    x value: 800
    i: 8 is even 
    x value: 900
    i: 9 is odd
    x value: 1000
    i: 10 is even 
    x value: 1100
    PL/SQL procedure successfully completed.
    
    
    *************************/
    View Code

    后面再说遍历什么游标啊,数组啊。先从简单的 开始。

    4,While 循环。

    create or replace Procedure Test2(i in out number)
    as
    begin
         while i < 10 loop
                begin
                       i:= i+1;
               end;
              end loop;
    end Test2;
    View Code

    来测试下。

    set serveroutput on;
    declare
           num number;
    begin
        num:= 1;
        test2(num);
        dbms_output.put_line( 'num = ' || num );
    end;
    
    
    /*********************
    
    num = 10
    PL/SQL procedure successfully completed.
    
    ***********************/
    View Code

    第一篇就先写到这里,对Oracle的存储过程有个简单的认识。

  • 相关阅读:
    领域驱动设计(DDD)实现之路
    《实现领域驱动设计》译者序
    一次领域驱动设计(DDD)的实际应用
    Gradle学习系列之十——自定义Plugin(本系列完)
    Gradle学习系列之九——自定义Task类型
    Gradle学习系列之八——构建多个Project
    Gradle学习系列之七——依赖管理
    Gradle学习系列之六——使用Java Plugin
    Gradle学习系列之五——自定义Property
    Gradle学习系列之四——增量式构建
  • 原文地址:https://www.cnblogs.com/lideng/p/3427822.html
Copyright © 2011-2022 走看看