欢迎光临
我们一直在努力

几十个实用的PL/SQL(4)-数据库专栏,SQL Server

建站超值云服务器,限时71元/月

第四阶段

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;

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 几十个实用的PL/SQL(4)-数据库专栏,SQL Server
分享到: 更多 (0)

相关推荐

  • 暂无文章