--plsql控制语句
--一、分支控制语句
--1 if-then-else语句
--当向员工表插入一条数据时,先查询下员工编号是否存在,如果不存在,则添加;
declare
v_count number(4);
v_empno number(4):=32;
begin
select count(1) into v_count from emp where emp=v_empno;
if v_count<1 then
insert into emp(id,c_name,empno) values(sys_guid(),'lisi',v_empno);
end if;
commit;
exception
when others then
dbms_output.put_line(sqlcode||';'||sqlerrm);
end;
--现在修改为如果存在则更新,不存在则添加;
declare
v_count number(4);
v_empno number(4):=5;
begin
select count(1) into v_count from emp where empno=v_empno;
if v_count=0 then
insert into emp(id,empname,empno) values(sys_guid(),'lisi',v_empno);
elsif
update emp set empname='lisi' where empno=v_empno;
end if;
commit;
exception
when others then
dbms_output.put_line(sqlcode||';'||sqlerrm);
end;
-- 嵌套 在if else中嵌套 if 语句块,公司决定为部门编号为20的所有员工加薪,而且根据部门位置不同的,加薪不同
declare
v_empno emp.empno%type;
v_salary emp.deptno%type;
v_job emp.job%type;
begin
select empno,salary,job into v_empno,v_salary,v_job from emp where empno:=20;
if v_empno=20 then
if v_job='clerk' then
v_salary:=v_salary+(1+0.2);
elsif v_job='analyse' then
v_salary:=v_salary+(1+0.25);
end if;
elsif
dbms_output.put_line('v_empno编号不是'||v_empno);
end if;
commit;
exception
when others then
dbms_output.put_line(sqlcode||';'||sqlerrm);
end;
-- if then elsif 语句
declare
v_char varchar(3):='abc';
begin
if v_char='a' then
dbms_output.put_line('这是字符串'||v_char);
elsif v_char='b' then
dbms_output.put_line('这是字符串'||v_char);
elsif v_char='c' then
dbms_output.put_line('这是字符串'||v_char);
else
dbms_output.put_line('这是字符串---'||v_char);
end if;
end;
--case 语句
declare
v_job varchar2(32);
v_empno varchar2(4):=empno;
begin
select job into v_job from emp where empno=v_empno;
case v_job
when 'clerk' then
update emp set salary=salary*(1+0.2) where empno=v_empno;
when 'analyse' then
update emp set salary=salary*(1+0.25) where empno=v_empno;
else
dbms_output.put_line('你不具备加薪资格');
end case;
end;
--根据员工薪水去判断是职称;
declare
v_salary number(4);
v_empno number(13):=&empno;
begin
select salary into v_salary from emp where empno=v_empno;
case
when v_salary between 1000 and 1500
then
dbms_output.put_line('初级');
when v_salary between 1500 and 2-00
then
dbms_output.put_line('中级');
else
dbms_output.put_line('无职称');
end case;
end;
--loop 循环
begin
loop
dbms_output.put_line('---');
end loop;
end;
--使用exit退出循环
declare
v_counter integer:=0;
begin
loop
v_counter:=v_counter+1;
dbms_output.put_line('循环'||v_counter);
if v_counter=10 then
exit;
end if;
end loop;
dbms_output.put_line('循环退出');
end;
--使用exit when退出循环
declare
v_counter integer(10):=4;
begin
loop
v_counter:=v_counter+1;
dbms_output.put_line('循环第'||v_counter);
exit when v_counter=10;
end loop;
dbms_output.put_line('循环已退出!');
end;
--使用continue继续执行循环
declare
x number:=10;
begin
loop
x:=x+1;
if x<10 then
dbms_output.put_line('循环第'||x);
if x=3 then
continue;
end if;
end if;
end loop;
end;
--使用continue when重新开始循环
declare
x number:=10;
begin
loop
dbms_output.put_line('当前值为'||x);
x:=x+1;
continue when<3;
exit when x=5;
end loop;
dbms_output.put_line('循环结束!');
end;
--while loop循环
declare
v_counter integer:=0;
begin
while v_counter<5;
loop
dbms_output.put_line('索引值为'||v_counter);
v_counter:=v_counter+1;
end loop;
end;
--for -loop语句
declare
v_counter integer:=0;
begin
for i in 1..3
loop
v_counter:=v_counter+1;
dbms_output.put_line('当前值为'||i);
end loop;
dbms_output.put_line('已退出当前循环');
end;
--reverse 关键字
declare
v_counter integer:=5;
begin
for i reverse 1..3
loop
v_counter:=v_counter+1;
dbms_output.put_line('循环计数器为'||i);
end loop;
dbms_output.put_line('循环总计'||v_counter);
end;
--使用上下边界值
declare
v_index integer:=&index;
begin
for i in 1..v_index
loop
dbms_output.put_line('循环'||i);
end loop;
end;
--顺序控制语句
--goto语句和标签
declare
p varchar2(30);
n pls_integer:=37;
begin
for i in 2..round(sqrt(n))
loop
if n mod i =0 then
p:='不是素数';
goto print_now;
else
p:='是素数';
end if;
end loop;
<<print_now>>
dbms_output.put_line(p);
end;
--goto 语句模拟循环语句
declare
v_count integer:=0;
begin
<<outer>>
loop
v_count:=v_count+1;
dbms_output.put_line(v_count);
if v_count=4 then
goto outer;
end if;
end loop;
end;
--null语句
declare
v_counter integer:=0;
begin
if v_counter>5 then
dbms_output.put_line('此时的值'|v_counter);
else
null;
end if;
end;
--一、分支控制语句
--1 if-then-else语句
--当向员工表插入一条数据时,先查询下员工编号是否存在,如果不存在,则添加;
declare
v_count number(4);
v_empno number(4):=32;
begin
select count(1) into v_count from emp where emp=v_empno;
if v_count<1 then
insert into emp(id,c_name,empno) values(sys_guid(),'lisi',v_empno);
end if;
commit;
exception
when others then
dbms_output.put_line(sqlcode||';'||sqlerrm);
end;
--现在修改为如果存在则更新,不存在则添加;
declare
v_count number(4);
v_empno number(4):=5;
begin
select count(1) into v_count from emp where empno=v_empno;
if v_count=0 then
insert into emp(id,empname,empno) values(sys_guid(),'lisi',v_empno);
elsif
update emp set empname='lisi' where empno=v_empno;
end if;
commit;
exception
when others then
dbms_output.put_line(sqlcode||';'||sqlerrm);
end;
-- 嵌套 在if else中嵌套 if 语句块,公司决定为部门编号为20的所有员工加薪,而且根据部门位置不同的,加薪不同
declare
v_empno emp.empno%type;
v_salary emp.deptno%type;
v_job emp.job%type;
begin
select empno,salary,job into v_empno,v_salary,v_job from emp where empno:=20;
if v_empno=20 then
if v_job='clerk' then
v_salary:=v_salary+(1+0.2);
elsif v_job='analyse' then
v_salary:=v_salary+(1+0.25);
end if;
elsif
dbms_output.put_line('v_empno编号不是'||v_empno);
end if;
commit;
exception
when others then
dbms_output.put_line(sqlcode||';'||sqlerrm);
end;
-- if then elsif 语句
declare
v_char varchar(3):='abc';
begin
if v_char='a' then
dbms_output.put_line('这是字符串'||v_char);
elsif v_char='b' then
dbms_output.put_line('这是字符串'||v_char);
elsif v_char='c' then
dbms_output.put_line('这是字符串'||v_char);
else
dbms_output.put_line('这是字符串---'||v_char);
end if;
end;
--case 语句
declare
v_job varchar2(32);
v_empno varchar2(4):=empno;
begin
select job into v_job from emp where empno=v_empno;
case v_job
when 'clerk' then
update emp set salary=salary*(1+0.2) where empno=v_empno;
when 'analyse' then
update emp set salary=salary*(1+0.25) where empno=v_empno;
else
dbms_output.put_line('你不具备加薪资格');
end case;
end;
--根据员工薪水去判断是职称;
declare
v_salary number(4);
v_empno number(13):=&empno;
begin
select salary into v_salary from emp where empno=v_empno;
case
when v_salary between 1000 and 1500
then
dbms_output.put_line('初级');
when v_salary between 1500 and 2-00
then
dbms_output.put_line('中级');
else
dbms_output.put_line('无职称');
end case;
end;
--loop 循环
begin
loop
dbms_output.put_line('---');
end loop;
end;
--使用exit退出循环
declare
v_counter integer:=0;
begin
loop
v_counter:=v_counter+1;
dbms_output.put_line('循环'||v_counter);
if v_counter=10 then
exit;
end if;
end loop;
dbms_output.put_line('循环退出');
end;
--使用exit when退出循环
declare
v_counter integer(10):=4;
begin
loop
v_counter:=v_counter+1;
dbms_output.put_line('循环第'||v_counter);
exit when v_counter=10;
end loop;
dbms_output.put_line('循环已退出!');
end;
--使用continue继续执行循环
declare
x number:=10;
begin
loop
x:=x+1;
if x<10 then
dbms_output.put_line('循环第'||x);
if x=3 then
continue;
end if;
end if;
end loop;
end;
--使用continue when重新开始循环
declare
x number:=10;
begin
loop
dbms_output.put_line('当前值为'||x);
x:=x+1;
continue when<3;
exit when x=5;
end loop;
dbms_output.put_line('循环结束!');
end;
--while loop循环
declare
v_counter integer:=0;
begin
while v_counter<5;
loop
dbms_output.put_line('索引值为'||v_counter);
v_counter:=v_counter+1;
end loop;
end;
--for -loop语句
declare
v_counter integer:=0;
begin
for i in 1..3
loop
v_counter:=v_counter+1;
dbms_output.put_line('当前值为'||i);
end loop;
dbms_output.put_line('已退出当前循环');
end;
--reverse 关键字
declare
v_counter integer:=5;
begin
for i reverse 1..3
loop
v_counter:=v_counter+1;
dbms_output.put_line('循环计数器为'||i);
end loop;
dbms_output.put_line('循环总计'||v_counter);
end;
--使用上下边界值
declare
v_index integer:=&index;
begin
for i in 1..v_index
loop
dbms_output.put_line('循环'||i);
end loop;
end;
--顺序控制语句
--goto语句和标签
declare
p varchar2(30);
n pls_integer:=37;
begin
for i in 2..round(sqrt(n))
loop
if n mod i =0 then
p:='不是素数';
goto print_now;
else
p:='是素数';
end if;
end loop;
<<print_now>>
dbms_output.put_line(p);
end;
--goto 语句模拟循环语句
declare
v_count integer:=0;
begin
<<outer>>
loop
v_count:=v_count+1;
dbms_output.put_line(v_count);
if v_count=4 then
goto outer;
end if;
end loop;
end;
--null语句
declare
v_counter integer:=0;
begin
if v_counter>5 then
dbms_output.put_line('此时的值'|v_counter);
else
null;
end if;
end;