控制結(jié)構(gòu)
1,If語(yǔ)句
語(yǔ)句結(jié)構(gòu):
if condition1 then statements1;
elseif condition2 then statements2;
......
[else else_statements;]---可省略
end if;
示例:輸入一個(gè)員工號(hào),修改該員工的工資,如果該員工為10號(hào)部門(mén),工資增加100;
若為20號(hào)部門(mén),工資增加150;否則增加200
create procedure add_sal (empno in int) as
declare
v_empno number(4);
v_deptno number(5);
v_increment number(4);
BEGIN
v_empno:=empno;
select deptno into v_deptno from emp where empno=v_empno;
if v_deptno=10 then v_increment:=100;
elseif v_deptno=20 then v_increment:=150;
else v_increment:=200;
end if;
update emp set sal=sal+v_increment where empno=v_empno;
end;
2,case語(yǔ)句
語(yǔ)句結(jié)構(gòu):
case
when condition1 then statements1;
when condition2 then statements2;
......
when conditionn then statementsn;
[else else_statements;]---可省略
end case;
示例:輸入員工號(hào),修改該員工工資,如果該員工工資低于1000,工資增加200,
如果工資在1000-2000之間,則增加150;否則增加50
create procedure add_sal_1 (empno in number(4)) as
declare
v_empno number(4);
v_sal number(7,2);
v_increment number(7,2);
BEGIN
v_empno:=empno;
select sal into v_sal from EMP where EMPNO=v_empno;
case
when v_sal<1000 then v_increment:=200;
when v_sal between 1000 and 2000 then v_increment:=150;
else v_increment:=50;
end case;
update emp set sal=sal+v_increment where empno=v_empno;
end;
循環(huán)結(jié)構(gòu)
1,基本循環(huán)語(yǔ)句
語(yǔ)句結(jié)構(gòu):
loop
sequence_of_statement;
exit [when condition];
end loop;
示例:利用循環(huán)向temp_table表中插入50條記錄
--建表
create table temp_table(num int,info char(10));
--插入50條記錄
DECLARE
v_counter INT:=1;
begin
loop
insert into temp_table values(v_counter,'插入第'||v_counter||'條記錄');
v_counter:=v_counter+1;
exit when v_counter>50;
end loop;
end;
2,while循環(huán)
語(yǔ)句結(jié)構(gòu):
while condition loop
sequence_of_statement;
end loop;
示例:利用循環(huán)向temp_table表中插入50條記錄
DECLARE
v_counter int :=1;
begin
while v_counter<=50 loop
insert into temp_table values(v_counter,'while循環(huán)插入第'||v_counter||'條記錄');
v_counter:=v_counter+1;
end loop;
end;
3,for循環(huán)
語(yǔ)句結(jié)構(gòu):
for loop_counter in [范圍]
loop
sequence_of_statement;
end loop;
示例:利用循環(huán)向temp_table表中插入50條記錄
declare
v_counter int;
BEGIN
for v_counter in 1..50
loop
insert into temp_table values(v_counter,'for循環(huán)插入第'||v_counter||'條記錄');
end loop;
end;
達(dá)夢(mèng)社區(qū):https://eco.dameng.com