plsql学习笔记1

plsql简介

  1. pl/sql编程语言
  2. pl/sql编程语言是对sql语言的扩展,使得sql语言更具有过程化编程的特性
  3. pl/sql编程语言比一般的过程化语言更加灵活高效
  4. pl/sql编程语言主要用来编写存储过程函数等

定义和输出

declare 
    i number(2) :=15;
begin
    dbms_output.put_line(i);
end;

结果:
    15
declare 
    i number(2) :=15;
    s varchar2(10) := '小明';
begin
    dbms_output.put_line(i);
    dbms_output.put_line(s);
end;

结果
    15
    小明
declare 
    i number(2) :=15;
    s varchar2(10) := '小明';
    eb E_BOOK.name%type;    --引用型变量
begin
    dbms_output.put_line(i);
    dbms_output.put_line(s);
    select name into eb from E_BOOK where id=1;
    dbms_output.put_line(eb);
end;

结果
    15
    小明
    文学类书1
declare 
    i number(2) :=15;
    s varchar2(10) := '小明';
    eb E_BOOK.name%type;    --引用型遍历
    bookrow E_BOOK%rowtype; --记录型变量
begin
    dbms_output.put_line(i);
    dbms_output.put_line(s);
    select name into eb from E_BOOK where id=1;
    dbms_output.put_line(eb);
    select * into bookrow from E_BOOK where id=1;
    dbms_output.put_line('id为:' ||bookrow.id ||'  name为:' || bookrow.name);
end;

结果
    15
    小明
    文学类书1
    id为:1  name为:文学类书1

if判断

题目:

  1. 输入小于18的数字,输出未成年
  2. 输入大于18小于40的数字,输出中年人
  3. 输入大于40的数字,输出老年人
declare
    i number(3) := 19;
begin
    if i<18 then
        dbms_output.put_line('未成年');
    elsif i<40 then
        dbms_output.put_line('中年人');
    else
        dbms_output.put_line('老年人');
    end if;

end;

结果
    中年人

循环

用三种方式输出1到10

while循环

declare
    i number(2) := 1;
begin
    while i<11 loop
            dbms_output.put_line(i);
            i := i+1;
    end loop;
end;

结果:
1
2
3
4
5
6
7
8
9
10

exit循环(常用)

declare
    i number(2) := 1;
begin
    loop
        exit when i>10;
        dbms_output.put_line(i);
            i := i+1;
    end loop;
end;

结果:
1
2
3
4
5
6
7
8
9
10

for循环

declare
    
begin
    for i in 1..10 loop
        dbms_output.put_line(i);
    end loop;
end;

结果:
1
2
3
4
5
6
7
8
9
10

游标

游标:可以存放多个对象,多行记录(类似域java的集合)

输出表中所有行的name

declare
    cursor c is select * from e_book;
    --定义了一个名为c的游标  并把表中的所有数据存入游标中
    bookrow e_book%rowtype;
begin
    open c;
        loop 
            fetch c into bookrow;
            exit when c%notfound;
            dbms_output.put_line(bookrow.name);
        end loop;
    close c;
end;

结果:
文学类书1
文学类书2
文学类书3
社会科学书1
社会科学书2
社会科学书3
小说书1
小说书2
小说书3

给指定书增加价格

...

存储过程

存储过程:存储过程就是提前已经编译好的一段pl/sql语言,放置在数据库,可以直接被调用。这一段pl/sql一般都是固定步骤的业务

--给指定书的价格加100块钱

create  or replace procedure p1(bid e_book.id%type)
is
begin
        update e_book set price = price+100 where id=bid;
        commit;
end;

--测试
declare

begin
    p1(1);
end;

存储函数(理解)

存储过程和储存函数的取别

一般来讲,过程和函数的区别在于函数可以有一个返回值,而过程没有返回值,但过程和函数都可以通过out指定一个或多个输出参数,我们可以利用out参数,在过程和函数中实现返回多个值


--通过储存函数实现计算指定书籍的10本的价格
--存储过程和存储函数的参数都不能带长度
--存储函数的返回值类型不能带长度
create or replace function x(bid e_book.id%type) return number  --不能带长度
is 
    s number(10);
begin
select price*10 into s from e_book where id=bid;
return s;
end;

--测试
--存储函数在调用的时候,返回值需要接收
declare
    s number(10);
begin
    s := x(1);
        dbms_output.put_line(s);
end;

out类型参数如何使用

--out类型参数如何使用
--使用存储过程来算10*价格

create or replace procedure z(bid e_book.id%type, p out number)
is 
    s number(10);
begin
    select price*10 into s  from e_book where id=bid;
    p := s;
end;

--测试
declare 
    pp number(10);
begin
    z(1,pp);
    dbms_output.put_line(pp);
end;

结果
22000

in和out类型参数的取别是什么?

凡是设计到into查询语句复制或者:=赋值操作的参数,那必须使用out来修饰,其他用in

猜你喜欢

转载自www.cnblogs.com/sm1128/p/11450566.html
今日推荐