定义变量
declare
i number(2) := 10;
name varchar2(32) := '张三';
username tb_sys_user.username%type; ---引用型变量
row tb_sys_user%rowtype;---记录型变量
begin
dbms_output.put_line(i);
dbms_output.put_line(name);
select username into username from tb_sys_user where userno = '99999';
dbms_output.put_line(username);
select * into row from tb_sys_user where userno = '99999';
dbms_output.put_line(row.username || '-----' || row.userno);
end;
IF语句
declare
i number(10) := ⅈ
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;
循环语句
DECLARE
i number(2) := 1;
BEGIN
while i < 11 loop
dbms_output.put_line(i);
i := i + 1;
end loop;
END;
DECLARE
i number(2) := 1;
BEGIN
for i in 1 .. 10 loop
dbms_output.put_line(i);
end loop;
END;
DECLARE
i number(2) := 1;
BEGIN
loop
exit when i>10;
dbms_output.put_line(i);
i := i + 1;
end loop;
END;
游标
DECLARE
CURSOR cl is
select * from tb_sys_user;
tbrow tb_sys_user%rowtype;
BEGIN
open cl;
loop
fetch cl
into tbrow;
exit when cl%notfound;
dbms_output.put_line(tbrow.username);
end loop;
close cl;
END;
储存过程
存储过程就是提前编译好的pl/sql语言。
语法
create or replace PROCEDURE 过程名[(参数名 in/out 参数类型)]
AS
BEGIN
PLSQL子程序体;
END;
储存函数
语法
create or replace function 函数名(Name in type,Name in type,...) return 数据类型 is
结果变量 数据类型;
BEGIN
return(结果变量);
END;
存储过程和存储函数的区别
1、语法区别:关键字不一样
2、存储函数比存储过程多了两个return,本质区别,存储函数有返回值,存储过程没有返回值。
如果存储过程想实现返回值的业务,我们就必须使用out类型的参数,即便是存储过程使用了out类型的参数,其本质也不是真的有了返回值,而是在存储过程累不给了out类型的参数赋值,在执行完毕后,我们直接拿输出类型参数的值。