首页 > 开发 > 综合 > 正文

几十个实用的PL/SQL(4)

2024-07-21 02:08:01
字体:
来源:转载
供稿:网友



第四阶段

q.编写一过程以接受用户输入的三个部门编号并显示其中两个部门编号的部门名称。

a.

create or replace procedure deptname(no1 dept.deptno%type,no2 dept.deptno%type,no3 dept.deptno%type) as

       vflag number;

       vdeptno1 dept.deptno%type;

       vdeptno2 dept.deptno%type;

       vdname1 dept.dname%type;

       vdname2 dept.dname%type;

 

begin

       vflag:=to_number(to_char(sysdate,'ss'));

       if (vflag>=1 and vflag<=10) or (vflag>=50 and vflag<60) then

              select deptno,dname into vdeptno1,vdname1 from dept where deptno=no1;

              select deptno,dname into vdeptno2,vdname2 from dept where deptno=no2;

       elsif (vflag>=11 and vflag<=20) or (vflag>=40 and vflag<50) then

              select deptno,dname into vdeptno1,vdname1 from dept where deptno=no1;

              select deptno,dname into vdeptno2,vdname2 from dept where deptno=no3;

       else

              select deptno,dname into vdeptno1,vdname1 from dept where deptno=no2;

              select deptno,dname into vdeptno2,vdname2 from dept where deptno=no3;

       end if;

       dbms_output.put_line('部门编号:'||vdeptno1 ||'  '||'部门名称:' ||vdname1);

       dbms_output.put_line('部门编号:'||vdeptno2 ||'  '||'部门名称:' ||vdname2);

      

end;

/

execute deptname(10,20,30);

 

q.编写一过程以显示所指定雇员名的雇员部门名和位置。

a.

create or replace procedure deptmesg(pename emp.ename%type,pdname out dept.dname%type,ploc out dept.loc%type) as

begin

       select dname,loc into pdname,ploc

       from emp,dept

       where emp.deptno=dept.deptno and emp.ename=pename;

end;

/

variable vdname varchar2(14)

variable vloc varchar2(13)

 

execute deptmesg('smith',:vdname,:vloc);

print vdname vloc;

 

q.编写一个给特殊雇员加薪10%的过程,这之后,检查如果已经雇佣该雇员超过60个月,则给他额外加薪3000.

a.

create or replace procedure raise_sal(no in number) as

       vhiredate date;

       vsal emp.sal%type;

begin

       select hiredate,sal into vhiredate,vsal from emp where empno=no;

       if months_between(sysdate,vhiredate)>60 then

              vsal:=nvl(vsal,0)*1.1+3000;

       else

              vsal:=nvl(vsal,0)*1.1;

       end if;

       update emp set sal=vsal where empno=no;

end;

/

variable no number

begin

       :no:=7369;

end;

/

execute raise_sal(:no)

select empno,ename,sal,comm,hiredate from emp where empno=:no;

 

q.编写一个函数以检查所指定雇员的薪水是否有效范围内。不同职位的薪水范围为:

 designation          raise

 clerk                   1500-2500

 salesman             2501-3500

 analyst                3501-4500

 others                 4501 and above.

 如果薪水在此范围内,则显示消息“salary is ok”,否则,更新薪水为该范围内的最水值。

a.

create or replace function sal_level(no emp.empno%type) return char as

       vjob emp.job%type;

       vsal emp.sal%type;

       vmesg char(50);

begin

       select job,sal into vjob,vsal from emp where empno=no;

       if vjob='clerk' then

              if vsal>=1500 and vsal<=2500 then

                     vmesg:='salary is ok.';

              else

                     vsal:=1500;

                     vmesg:='have updated your salary to '||to_char(vsal);

              end if;

       elsif vjob='salesman' then

              if vsal>=2501 and vsal<=3500 then

                     vmesg:='salary is ok.';

              else

                     vsal:=2501;

                     vmesg:='have updated your salary to '||to_char(vsal);

              end if;

       elsif vjob='analyst' then

              if vsal>=3501 and vsal<=4500 then

                     vmesg:='salary is ok.';

              else

                     vsal:=3501;

                     vmesg:='have updated your salary to '||to_char(vsal);

              end if;

       else

              if vsal>=4501 then

                     vmesg:='salary is ok.';

              else

                     vsal:=4501;

                     vmesg:='have updated your salary to '||to_char(vsal);

              end if;

       end if;

       update emp set sal=vsal where empno=no;

       return vmesg;

end;

/

declare

       vmesg char(50);

       vempno emp.empno%type;

begin

       vempno:=&empno;

       vmesg:=sal_level(vempno);

       dbms_output.put_line(vmesg);

end;

/

--select empno,ename,sal,comm,hiredate from emp where empno=:no;

 

q.编写一个函数以显示该雇员在此组织中的工作天数。

a.

create or replace function hire_day(no emp.empno%type) return number as

       vhiredate emp.hiredate%type;

       vday number;

 

begin

       select hiredate into vhiredate from emp where empno=no;

       vday:=ceil(sysdate-vhiredate);

       return vday;

end;

/

declare

       vday number;

       vempno emp.empno%type;

begin

       vempno:=&empno;

       vday:=hire_day(vempno);

       dbms_output.put_line(vday);

end;

/

 

--select empno,ename,sal,comm,hiredate from emp where empno=:no;
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表