当前位置:文档之家› oracle 上机考试试题与答案

oracle 上机考试试题与答案

1、写一个pl/sql程序块:直接使用数据类型定义两个变量v_empno 和v_ename,从scott模式下的emp表中检索某个员工的编号empno 和姓名ename,存储到v_empno和v_ename,并输出此员工的编号和姓名。

set serveroutput on
declare
type merchandise is record(
v_empno number(30),
v_ename varchar2(30));
record_merchandise merchandise;
begin
select empno,ename
into record_merchandise
from emp
where empno='7369';
dbms_output.put_line(record_merchandise.v_empno);
dbms_output.put_line(record_merchandise.v_ename);
end;
2、写一个pl/sql程序块:根据scott模式下的emp表中的部门编号deptno字段的值,为姓名为SCOTT的雇员修改工资;若他所在部门号为10,则工资加100;若部门号为20,则工资加300;否则工资加400。

set serveroutput on
declare
v_deptno emp.deptno%type;
addsal emp.sal%type;
sal number;
begin
select deptno into v_deptno from emp where ename='SCOTT';
if v_deptno='10' then
addsal:=100;
elsif v_deptno='20' then
addsal:=300;
else
addsal:=400;
end if;
update emp set sal=sal+addsal where ename='SCOTT';
dbms_output.put_line(sal);
end;
3、写一个pl/sql程序块:定义一个游标类型type_cursor,然后使用type_cursor定义变量ref_cur;根据scott模式下的emp表和dept表,使用游标变量ref_cur检索员工姓名和工作信息,并输出员工姓名和工作信息;使用游标变量ref_cur检索部门编号和部门名称信息,并输出部门编号和部门名称信息。

set serveroutput on
declare
type type_cursor is ref cursor;
ref_cur type_cursor;
mer_rec emp%rowtype;
ner_rec dept%rowtype;
begin
open ref_cur for select ename,job from emp; loop
fetch ref_cur into mer_rec;
exit when ref_cur%notfound;
dbms_output.put(mer_rec.ename ||' '); dbms_output.put(mer_rec.job );
end loop;
open ref_cur for select deptno,dname from dept;
loop
fetch ref_cur into ner_rec;
exit when ref_cur%notfound;
dbms_output.put(ner_rec.deptno||' ');
dbms_output.put(ner_rec.dname );
end loop;
close ref_cur;
end;
4、写一个pl/sql存储过程:根据scott模式下的emp表,写一个带参数的存储过程proc(deptno in number,sun_sal out number),输入部门编号,输出该部门的总工资信息。

并写一个pl/sql程序块,测试该存储过程。

create or replace procedure searchmerch
( v_deptno in number,
sun_sal out number) is
begin
select 12*(sal+nvl(comm,0))
into sun_sal
from emp
where deptno=v_deptno;
exception
when no_data_found then
sun_sal:='0';
end;
5、写一个pl/sql程序块:根据scott模式下的emp表和dept表,输出每个部门的编号和部门名称,以及该部门下所有的雇员和雇员工资,及其该部门的总人数。

输出效果如下:
部门编号:-- 部门名称:--
雇员姓名:-- 雇员工资:--
该部门总人数:--
declare
CURSOR c_dept IS SELECT deptno,dname FROM dept ORDER BY
deptno;
CURSOR c_emp (p_dept VarCHAR2) IS
SELECT ename,sal FROM emp WHERE deptno=p_dept ORDER BY ename;
n number;
BEGIN
FOR r_dept IN c_dept LOOP
DBMS_OUTPUT.PUT_LINE('部门编号:'|| r_dept.deptno||'--部门名称:'||r_dept.dname);
n:=0;
FOR r_emp IN c_emp(r_dept.deptno) LOOP
DBMS_OUTPUT.PUT_LINE('雇员姓名: '||r_emp.ename || ' 雇员工资:'||r_emp.sal);
n:=n+1;
END LOOP;
DBMS_OUTPUT.PUT_LINE(r_dept.dname||'部门的总人数:'|| n); END LOOP;
END;
6.创建一个语句级触发器CHECK_TIME,限定对表EMP的修改时间为周一至周五的早8点至晚5点。

create or replace trigger CHECK_TIME
before update or insert or delete
on emp
begin
if (to_char(sysdate,'DY') in ('sat','sun'))
or to_char(sysdate,'HH24')<'08'
or to_char(sysdate,'HH24')>='17' then
raise_application_error(-20500,'只能在工作时间对表操作!'); end if;
end;。

相关主题