Oracle练习-PL/SQL编程基础

1、创建一个表stu,该表只有一个字段sno 类型是number(2),编写一个块,向stu表中添加数字1到10,但不包括4和7。

create table stu (
	sno number(2));

declare
	i int:=1;
begin
	while i<11
loop
	if i!=4 and i!=7 then
	insert into stu(sno) values(i);
	end if;
	i:=i+1;
end loop;
end;

在这里插入图片描述

2、为Scott用户下的emp表增加一个列stars,类型为VARCHAR2(100)。创建一个PL/SQL块,通过输入任意员工编号(&e_no),根据员工的工资计算他能获得的星号’*’数量,每100元奖励一个星号,按四舍五入处理(使用函数round)。并根据员工所获得的星号数量n,形成由n个星号组成的字符串,写入emp表的stars列。

alter table emp add starts VARCHAR2(100);

在这里插入图片描述

declare
	
	e_no emp.empno%type;//用来存放编号
	e_star emp.starts%type:='***************************************************************************************';
	e_mon emp.sal%type;//用来存放星星的个数
	
begin
	e_no:=&e_no;
	select round(sal/100) into e_mon from emp where empno=e_no;//得到星星的个数,round取整函数
if e_mon>0 then//如果为0下面会出错
	update emp set starts=substr(e_star,1,e_mon) where empno=e_no;//修改starts的值,substr用来切割字符串,从1开始切割e_no个字符
end if;
	
	
end;

在这里插入图片描述

发布了57 篇原创文章 · 获赞 5 · 访问量 2792

猜你喜欢

转载自blog.csdn.net/qq_43520913/article/details/105708616