--作业
--1、 输入部门编号,按照下列加薪比例执行(用if-elsif 和case两种方法实现)。
--deptno raise(%)
--10 5%
--20 10%
--30 15%
--40 20%
--加薪比例以现有的sal为标准
--方法一
select deptno,ename,sal,
(case deptno
when 10 then sal * 0.05
when 20 then sal * 0.1
when 30 then sal * 0.15
when 40 then sal * 0.2
end) raise_sal from emp;
--方法二
declare
cursor c is select deptno, ename, sal from emp;
begin
for v_temp in c loop
dbms_output.put(v_temp.deptno || ' ' || v_temp.ename || ' ' || v_temp.sal || ' + ');
if v_temp.deptno = 10 then
dbms_output.put_line(v_temp.sal * 0.05 || ' = ' || (v_temp.sal + v_temp.sal * 0.05));
elsif v_temp.deptno = 20 then
dbms_output.put_line(v_temp.sal * 0.1 || ' = ' || (v_temp.sal + v_temp.sal * 0.1));
elsif v_temp.deptno = 30 then
dbms_output.put_line(v_temp.sal * 0.15 || ' = ' || (v_temp.sal + v_temp.sal * 0.15));
elsif v_temp.deptno = 40 then
dbms_output.put_line(v_temp.sal * 0.20 || ' = ' || (v_temp.sal + v_temp.sal * 0.20));
end if;
end loop;
end;
--2、接受2个数相除,并显示结果,如果除数为0,则显示错误提示。
select (case
when &除数 = 0 then '除数不能为零'
when &除数 <> 0 then '&被除数 / &除数' || ' = ' || to_char(&被除数 / &除数)
end) 除法结果 from dual;
--3、自己创建一张userinfo表,包含两个字段username,password,
--表中的记录信息取自emp表ename,empno字段,写一个PL/SQL程序,
--模拟登陆的过程,
--用户分别输入用户名和密码,对于登陆成功和失败分别给出提示信息.
--删表
drop table userinfo cascade constraint;
--建表
create table userinfo (
username varchar2(20),
password varchar2(20)
);
--插数据
declare
cursor c is select ename,empno from emp;
begin
for v_temp in c loop
insert into userinfo values(v_temp.ename,v_temp.empno);
end loop;
commit;
end;
--查询数据
select * from userinfo;
--登陆功能
create or replace procedure p_userinfo_login(v_t_us_na in userinfo.username%type,v_t_us_pa in userinfo.password%type) is
--v_us_na in userinfo.username%type,v_us_pa in userinfo.password%type
--v_us_na in varchar2(20),v_us_pa in varchar2(20)
v_us_na userinfo.username%type := v_t_us_na;
v_us_pa userinfo.password%type := v_t_us_pa;
cursor c is select * from userinfo;
begin
dbms_output.put_line('******登陆功能******');
for v_temp in c loop
if v_temp.username = v_us_na then
v_us_na := '密码错误';
if v_temp.password = v_us_pa then
v_us_pa := '登陆成功';
exit;
end if;
end if;
end loop;
if v_us_pa = '登陆成功' then
dbms_output.put_line(v_us_pa);
elsif v_us_na = '密码错误' then
dbms_output.put_line(v_us_na);
else
dbms_output.put_line('用户不存在');
end if;
end;
--执行方法
exec p_userinfo_login('SMITH','7369');
--4、用userinfo表,写一个PL/SQL程序,
--模拟注册的过程,
--用户分别输入用户名和密码,对于登陆成功和失败分别给出提示信息.
--注册功能
create or replace procedure p_userinfo_register(v_t_us_na in userinfo.username%type,v_t_us_pa in userinfo.password%type,v_t_us_paa in userinfo.password%type) is
v_us_na userinfo.username%type := v_t_us_na;
v_us_pa userinfo.password%type := v_t_us_pa;
v_us_paa userinfo.password%type := v_t_us_paa;
v_count number;
begin
dbms_output.put_line('******注册功能******');
select count(*) into v_count from userinfo where username = v_us_na;
if v_count = 0 then
if v_us_pa is not null and v_us_paa is not null then
--问题 null和null是不能比较的
if v_us_pa = v_us_paa then
insert into userinfo values(v_us_na,v_us_pa);
dbms_output.put_line('注册成功');
commit;
else
dbms_output.put_line('密码不一致');
end if;
else
dbms_output.put_line('密码不能为空');
end if;
else
dbms_output.put_line('用户名重复');
end if;
end;
--执行方法
exec p_userinfo_register('SMITH','7369','73691');
--查询数据
select * from userinfo;