14-pl/sql编程语言

基本概念

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

声明方法

--赋值操作可以使用:=也可以使用into查询语句赋值
declare
    i number(2) := 10;
    s varchar2(10) := '小明';
    ena emp.ename%type;---引用型变量
    emprow emp%rowtype;---记录型变量
begin
    dbms_output.put_line(i);
    dbms_output.put_line(s);
    select ename into ena from emp where empno = 7788;
    dbms_output.put_line(ena);
    select * into emprow from emp where empno = 7788;
    dbms_output.put_line(emprow.ename || '的工作为:' || emprow.job);
end;

pl/sql中的if判断

---输入小于18的数字,输出未成年
---输入大于18小于40的数字,输出中年人
---输入大于40的数字,输出老年人
declare
  i number(3) := ⅈ
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;

pl/sql中loop循环😜

while循环

--用三种方式输出1到10是个数字
declare
 i number(2):=1
begin
 while i<11
loop
 dbms_output.put_line(i)
 i:=i+1;
end loop;
end;

exit循环

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

for循环

declare 
begin
	for i in 1..10 
	loop
	dbms_output.put_line(i)
	end loop;
end;
  • 游标:可以存放多个对象,多行记录
  • 输出emp表中所有员工的姓名
declare
 cursor c1 is select * from emp;
 emprow emp%rowtype;
begin
 open c1;
    loop
        fetch c1 into emprow;
        exit when c1%notfound;
        dbms_output.put_line(emprow.ename);
    end loop;
 close c1;
end;

存储过程

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

给员工涨工资100块钱

create or replace procedure p1(eno emp.empno%type)
is 
begin
 update emp set sal=sal+100 where empno=eno;
 commit
 end;

---------------------------------------------
select * from emp where empno=7788;
--测试
declare
begin
p1(7788)
end;
----通过存储函数实现计算指定员工的年薪
----存储过程和存储函数的参数都不能带长度
----存储函数的返回值类型不能带长度

create or replace function f_yearsal(eno emp.empno%type) return number
is
  s number(10);     
begin
  select sal*12+nvl(comm, 0) into s from emp where empno = eno;
  return s;
end;
--测试f_yearsal
--存储函数在调用的时候,返回值需要接收
declare
  s number(10); 
begin
  s := f_yearsal(7788);
  dbms_output.put_line(s);
end;

out类型的参数如何使用

---使用存储过程来算年薪
create or replace procedure p_yearsal(eno emp.empno%type, yearsal out number)
is
   s number(10);
   c emp.comm%type;
begin
   select sal*12, nvl(comm, 0) into s, c from emp where empno = eno;
   yearsal := s+c;
end;

---测试p_yearsal
declare
  yearsal number(10);
begin
  p_yearsal(7788, yearsal);
  dbms_output.put_line(yearsal);
end;

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

---凡是涉及到into查询语句赋值或者:=赋值操作的参数,都必须使用out来修饰。

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

语法区别:
关键字不一样
存储函数比存储过程多了两个return

 - 本质区别:存储函数有返回值,而存储过程没有返回值
 如果存储过程想实现有返回值的业务,我们就必须使用out类型的参数
 即使是存储过程使用了out类型的参数,起本质也不是真的有了返回值。
 而是在存储过程内部给out类型参数赋值,在执行完毕后,我们直接拿到输出类型参数的值
 我们可以使用存储函数有返回值的特性,来自定义函数
 而存储过程不能用来自定义函数
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值