zoukankan      html  css  js  c++  java
  • Oracle存储过程基础内容

    基本语法

    1.1 无参无变量

    create or replace procedure firstPro

    as

    begin

    dbms_output.put_line('hello word, my name is stored procedure');

    end;

    里面只有一个输出语句

    调用

    begin

    firstPro();

    end;

    1.2 无参有变量

    create or replace procedure myDemo02

    as

    name varchar(10);

    age int;

    begin

    name:='xiaoming';

    age:=18;

    dbms_output.put_line('name='||name||', age='||age);

    end;

    as中声明变量,begin中赋值,输出时通过||符号达到连接字符串的功能

    1.3 有参入门

    create or replace procedure myDemo03(name in varchar,age in int)

    as

    begin

    dbms_output.put_line('name='||name||', age='||age);

    end;

    调用时再赋值

    begin

      myDemo03('xiaoming',18);

    end;

    1.4 形参实参

    create or replace procedure myDemo04(name in varchar,age in int)

    as

    begin

      dbms_output.put_line('name='||name||', age='||age);

    end;

     调用

    declare

     name varchar(10);

     age int;

    begin

      name:='xiaoming';

      age:=18;

      myDemo04(name=>name,age=>18);

    end;

    调用的时候如果需要变量,就可以在declare中声明,然后执行的时候把变量值通过=>从右向左赋值给参数,要用都要用,此时不能myDemo04(name=>name,18)

    1.5 in,out参数

    create or replace procedure myDemo05(name out varchar,age in int)

    as

    begin

    dbms_output.put_line('age='||age);

    select 'xiaoming' into name from 名字表;

    end;

     in就是调用方法时赋值,是来自方法外的值,out是要自己查询出的值,在调用的时候就已经有了,而已赋值给其他变量

    declare

     name varchar(10);

     age int;

    begin

      myDemo05(name=>name,age=>10);

      dbms_output.put_line('name='||name);

    end;

    这个nameout,人家在内部查询出来,调用的时候就有值了,然后赋值给接收变量,下面还能输出,in只能是调用时赋值.

    增删改查

    2.1 操作

    create or replace procedure mydemo07(ids in int, username in varchar,userpass in varchar, userage in int)
    as
    begin
    -- insert into students(id,username,userpass,userage) values(ids,username,userpass,userage);--
    -- delete from students where id=ids;--
    -- update students set userage=100 where id=ids;--
    commit;

    end;

    都是入参,接收后对表进行增删改操作

    调用

    begin
    mydemo07(10,'a','b','17');
    end;

    2.2 查询

    create or replace procedure mydemo08(ids in int, age out int)
    as
    begin
      select userage into age from students where id=ids; --
    commit;
    end;

    根据传入的id,查询出年龄输出

    declare
    ids int;
    age int;
    begin
    ids:=1;
    myDemo08(ids=>ids,age=>age);
    dbms_output.put_line('age='||age);
    end;

    循环

    3.1 for循环

    create or replace procedure mydemo09

    as

    begin

      for stu in (select * from students) loop

         if (stu.id<5) then

            dbms_output.put_line(stu.id);

          end if;

      end loop;

    commit;

    end;

     调用

    begin

      mydemo09();

    end;

    3.2 while循环

    create or replace procedure test_while_loop

    as

    n_count number := 0;

    begin

      while n_count < 10 loop

        dbms_output.put_line(n_count);

        n_count := n_count + 1;

      end loop;

    end;

     在声明的时候也可赋值

    begin

      test_while_loop();

    end;

  • 相关阅读:
    js 时差转换 getTimezoneOffset()
    js 验证对象是否为数组
    mac下的一些mysql操作
    mac下mysql 1045 (28000): Access denied for user 'root'@'localhost' (using password:
    canvas 画布 文字描边
    background-size: contain 与cover的区别,以及ie78的兼容写法
    tortoisegit 常见错误disconnected no supported authentication methods available(server sent: publickey)
    windows 下git 的配置安装与使用
    Spring中Bean的生命周期
    HBase--DependentColumnFilter(参考例过滤器 )详解
  • 原文地址:https://www.cnblogs.com/fengnan/p/9815761.html
Copyright © 2011-2022 走看看