欢迎您的来访!有源码,好建站(www.codehy.com)源码海洋源码网为您提供快速建站平台.
当前位置: 首页 > 行业资讯 > 数据库 >

达梦数据库SQL控制结构和循环结构的基础使用方法-非常有用,建议收藏

时间:2024-11-06 18:06来源:未知 作者:admin 点击:
控制结构 1,If语句 语句结构: if condition1 then statements1; elseif condition2 then statements2; ...... [else else_statements;]---可省略 end if; 示例:输入一个员工号,修改该员工的工资,如果该员工为10号部门,工资增加100; 若为20号部门,

 控制结构

 
1,If语句
 
语句结构:
 
if condition1 then statements1;
 
elseif condition2 then statements2;
 
......
 
[else else_statements;]---可省略
 
end if;
 
示例:输入一个员工号,修改该员工的工资,如果该员工为10号部门,工资增加100;
 
若为20号部门,工资增加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语句
 
语句结构:
 
case
 
when condition1 then statements1;
 
when condition2 then statements2;
 
......
 
when conditionn then statementsn;
 
[else else_statements;]---可省略
 
end case;
 
示例:输入员工号,修改该员工工资,如果该员工工资低于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;
 
循环结构
 
1,基本循环语句
 
语句结构:
 
loop
 
sequence_of_statement;
 
exit [when condition];
 
end loop;
 
示例:利用循环向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循环
 
语句结构:
 
while condition loop
 
sequence_of_statement;
 
end loop;
 
示例:利用循环向temp_table表中插入50条记录
 
DECLARE
 
v_counter int :=1;
 
begin
 
while v_counter<=50 loop
 
insert into temp_table values(v_counter,'while循环插入第'||v_counter||'条记录');
 
v_counter:=v_counter+1;
 
end loop;
 
end;
 
3,for循环
 
语句结构:
 
for loop_counter in [范围]
 
loop
 
sequence_of_statement;
 
end loop;
 
示例:利用循环向temp_table表中插入50条记录
 
declare
 
v_counter int;
 
BEGIN
 
for v_counter in 1..50
 
loop
 
insert into temp_table values(v_counter,'for循环插入第'||v_counter||'条记录');
 
end loop;
 
end;
 
达梦社区:https://eco.dameng.com
 
(责任编辑:admin)本文地址:http://www.codehy.com/info/shujuku/2024/1106/25743.html

推荐资讯