if判断:
关于简单的if逻辑判断,简单的说下;
语法 : if 表达式 then --业务逻辑-- end if;
例子:修改记录,如果没有找到就输出'你好';
declare
name emp.sal%type;
v_sal emp%rowtype;
begin
name := 2454;
update emp set ename = 'wang' where sal = 2454;
if sql%notfound then
//`如果找不到;
dbms_output.put_line('你好');
end if;
//结束if
end;
if-else判断:
语法: if(条件) then...elsif(条件) then ...else...
注:elsif是没有els(e)的,一定注意;
declare
name emp.sal%type;
v_sal emp%rowtype;
begin
name := 2450;
select * into v_sal from emp where sal = name;
if(v_sal.sal<0) then
dbms_output.put_line('你好');
elsif (v_sal.sal<1000) then
dbms_output.put_line('bu好');
else
dbms_output.put_line('很好');
end if;
end;
循环:
for循环:
语法 : for 变量名 in n..n(如:1..10) loop .... end loop;
begin
--正序 1到10
for k in 1..10 loop
dbms_output.put_line(k);
end loop;
--倒序 10到1
for k in reverse 1..10 loop
dbms_output.put_line(k);
end loop;
end;
while循环:
语法:while(表达式) loop .... end loop;
游标: 用来做遍历的东西,永不回头;
游标属性: 游标名%found 查询到数据返回true;
游标名%notfound 没有查询到数据返回true;
游标名%rowcount 多少条记录;
游标名%isopen 是否开启游标;
语法: oper 游标名;
fetch 游标名 into 变量名;
close 游标名;
例子:
declare
cursor c is select * from emp;
v_sal emp%rowtype;
begin
open c;
fetch c into v_sal;
while (c%found) loop
dbms_output.put_line(v_sal.empno);
fetch c into v_sal;
end loop;
close c;
end;
for循环方式-最简单,最方便的方式:
declare
cursor c is select * from emp;
v_sal emp%rowtype;
begin
for v_sal in c loop
dbms_output.put_line(v_sal.empno);
end loop;
end;
带参数的游标:用于作为查询条件的游标,作用N大;
语法: oper 游标名;
fetch 游标名(变量) into 变量名;
close 游标名;
例子:
declare
cursor c(deptno1 emp.deptno%type)
is
select * from emp where deptno = deptno1;
v_sal emp%rowtype;
begin
for v_sal in c(10) loop
dbms_output.put_line(v_sal.empno);
end loop;
end;