Oracle数据库,PL/SQL数据块练习题

1 .写一个PL/SQL 块,实现任意两个员工的工资互换。
提示:可以定义两个变量,并从键盘上输入两个员工的编号,如果两个编号相同,则抛出一个自定义异常,否则进行工资的互换。

declare
id1 number := '&员工a编号:';
id2 number := '&员工b编号:';
maney1 number;
maney2 number;
begin
    select sal into maney1 from emp where empno=id1;
    select sal into maney2 from emp where empno=id2;
    dbms_output.put_line('调换之前的:'||maney1|| ':' ||maney2);
    if id1=id2 then
       raise_application_error(-20202,'我就问你怎么换!');
    else
       update emp set sal=maney1 where empno=id2;
       update emp set sal=maney2 where empno=id1;
       dbms_output.put_line('调换之后的:'||maney2 || ':' || maney1);
    end if;
end;

2.写一个PL/SQL 块,将某个以指定字母开头的姓名员工的工资赋给变量,然后输出。
提示:从键盘上输入一个字符串变量的值,如果该字符串的长度超过1,则引发一个自定义异常,否则用该字符对员工表的姓名字段进行模糊查询。如输入’S’,则将所有姓名以S开头的员工的平均工资赋到某个变量中。然后输出

declare
name_sal number;
name varchar(32):='&name';
begin
select avg(sal) into name_sal from emp where ename like name||'%';
if length(name)>1 then
   raise_application_error(-20200,'你太长了');
else
   dbms_output.put_line(name_sal);
end if;
end;

3 .写一个PL/SQL 块,输出 NxN 乘法表。
提示:从键盘上输入一个数字N,如输入6,则输出乘法表直到6乘6;如输入9,则输出乘法表直到9乘9。如果输入的是0或负数,则引发一个自定义异常。注意输出的格式要尽可能工整。

declare
m number;
begin
m:='&m';
if m < 1 then
raise_application_error(-20202,'我就问你怎么写!');
else
  for i in 1..m loop
      for j in 1..i loop
          dbms_output.put(i||'*'||j||'='||(i*j)||' ');
      end loop;
    dbms_output.put_line(' ');
  end loop;
end if;
end;
发布了70 篇原创文章 · 获赞 114 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43330884/article/details/103826720