ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

plsql存储过程练习题

plsql存储过程练习题 # Oracle PL/SQL 练习题10道中等难度约束要求1. 允许存储过程、自定义函数、IF判断、CASE、FOR/WHILE循环2. 禁止触发器、异常处理块(EXCEPTION)3. 环境基于经典emp、dept表题目可直接在SCOTT用户下运行不需要自建业务表 说明函数必须有返回值存储过程无返回值可使用IN/OUT参数不许写EXCEPTION部分。## 题目1存储过程‑IF判断编写存储过程 p_check_sal 传入员工编号p_empno。查询该员工工资- 工资大于3000输出员工XXX工资偏高- 工资1500~3000输出员工XXX工资正常- 小于1500输出员工XXX工资偏低要求使用DBMS_OUTPUT打印结果。create or replace procedure sp_97(V_empno number)isV_sal number;V_ename emp.ename%type;beginselect sal , ename into V_sal,V_ename from emp where empno V_empno;if v_sal3000 thendbms_output.put_line(员工||V_ename||工资偏高);elsif V_sal between 1500 and 3000 thendbms_output.put_line(员工||V_ename||工资正常);elsif v_sal 1500 thendbms_output.put_line(员工||V_ename||工资偏低);end if ;end;call sp_97(7566);## 题目2函数‑IF编写函数f_get_job_level接收岗位p_job返回岗位等级数字- PRESIDENT → 1- MANAGER →2- ANALYST →3- 其余岗位返回4。create or replace function f_get_job_level(p_job varchar2 )return numberisbeginif p_jobPRESIDENT thenreturn 1;elsif p_jobMANAGER thenreturn 2;elsif p_jobANALYST thenreturn 3;elsereturn 4;end if;end;## 题目3存储过程‑WHILE循环编写存储过程p_print_num传入数字p_n使用**WHILE循环**打印1~p_n之间所有偶数。create or replace procedure sp_97 (p_n number)isn number:1;beginwhile np_nloopif mod(n,2)0 thendbms_output.put_line(n);end if;end loop;end;## 题目4函数‑FOR循环编写函数f_sum_even接收入参p_max使用FOR循环计算1~p_max所有偶数之和返回总和。create or replace function fu_97(p_max number)return numberisn number:0;beginfor i in 1..p_max loopif mod(i,2)0 thenn: n i;end if;end loop;return n;end;select fu_97(56) from dual## 题目5存储过程‑IF 查询 OUT参数创建存储过程p_dept_stats入参部门编号p_deptno两个OUT参数o_emp_count(部门人数)、o_avg_sal(部门平均工资)。逻辑如果部门人数大于5则把平均工资上浮10%赋值给o_avg_sal否则保持原平均工资。create or replace procedure sp_97(p_deptno in number,o_emp_count out number,o_avg_sal out number)isbeginselect avg(sal),count(1) into o_avg_sal,o_emp_count from emp where deptnop_deptno;if o_emp_count5 theno_avg_sal: o_avg_sal*1.1;end if;end;declarea number;b number;beginsp_97(10, a , b );dbms_output.put_line(a||b);end;## 题目6函数‑CASE判断编写函数f_sal_tax传入工资p_sal使用 if 表达式计算模拟个税并返回- sal1000扣税0- 1000sal2000扣5%- 2000sal3500扣10%- sal3500扣15%返回扣税金额。if## 题目7存储过程‑FOR循环 IF嵌套存储过程p_sal_update_loop传入部门号p_deptno。遍历该部门全部员工FOR循环游标for- 如果岗位是MANAGER工资增加200- 如果岗位是CLERK工资增加100其他岗位工资不变。执行update更新表。 提示使用FOR rec IN(select empno,job,sal from emp where deptnop_deptno)LOOP禁止显式声明cursor。create or replace procedure sp_97(p_deptno number)isbeginfor i in (select empno,job,sal from emp where deptnop_deptno)loopif i.jobMANAGER thenupdate emp set salsal200 where empnoi.empno;elsif i.jobCLERK thenupdate emp set salsal100 where empnoi.empno;end if;end loop;end;## 题目8函数‑循环判断编写函数f_count_high_sal入参部门编号p_deptno统计该部门工资大于2500的员工人数返回统计数量。使用FOR循环遍历不允许直接count聚合一步返回结果必须循环逐个判断计数。create or replace function fu_97(p_deptno number)return numberisn number:0;beginfor i in (select * from emp where deptnop_deptno) loopif i.sal2500 thenn :n1;end if;end loop;return n;end;## 题目9存储过程‑多条件IFOUT输出字符串存储过程p_emp_info输入员工编号p_empno输出OUT字符串o_result。拼接信息姓名:xxx岗位:xxx附加规则- 入职早于1982年追加[老员工]- 工资2800追加[高薪]。create or replace procedure sp_97(p_empno in number,o_result out varchar2)isV_ename emp.ename%type;V_job emp.job%type;V_sal number;V_hiredate date;beginselect ename,job,sal,hiredateinto V_ename,V_job,V_sal,V_hiredatefrom empwhere empnop_empno;if v_sal2800 theno_result:姓名:||v_ename||岗位:||V_job||高新;end if ;if to_char(V_hiredate,yyyy)1982 theno_result:姓名:||v_ename||岗位:||V_job||老员工;end if;end;## 题目10综合函数调用存储过程IF循环1. 复用第6题函数f_sal_tax2. 创建存储过程p_show_tax_list(p_deptno number)使用FOR循环遍历该部门所有员工调用f_sal_tax得到每个人扣税DBMS_OUTPUT打印姓名:xxx工资:xxx扣税:xxx。---f_sal_taxcreate or replace procedure sp_97(p_deptno number)isbeginfor i in (select * from emp where deptnop_deptno)loopdbms_output.put_line(i.ename||i.sal||f_sal_tax(i.sal));end loop;end;------------------------------------------------------------------------------------------------------------------------------------------------------
返回列表