考试总分为100分,共8题,时间为1小时。
 表结构说明:
 create table employee(
 id number(10) not null, -- 员工工号 
 salary number(10,2) default 0 not null, -- 薪水
 name varchar2(24) not null -- 姓名
 );1.创建序列seq_employee,该序列每次取的时候它会自动增加,从1开始计数,不设最大值,并且一直累加,不循环。(10分)
 2.写一个PL/SQL块,插入表user.employee中100条数据。插入该表中字段id用序列seq_employee实现,薪水和姓名字段可以任意填写。(15分)
 3.写一个语句块,在语句块中定义一个显式游标,按id升序排列,打印表employee中前十条数据。(15分)
 4.创建存储过程p_employee,输入员工薪水范围,返回员工工号、姓名、薪水结果集,结果集按员工薪水升序排列。(15分)
 5.创建函数f_employee实现更新员工薪水的功能,将薪水低于2000且姓wang的员工薪水加5%,其他不变,更新成功则返回0,否则返回1。(15分)
 6.写一个匿名语句块,用于执行函数f_employee,并打印执行该函数的结果。(8分)
 7.创建存储过程p_create_emp,用于判断表employee是否存在,如果存在则删除该表。(15分)
 8.写一个匿名语句块,用于执行存储过程p_create_emp。(7分)答案如下:
 1 ---参考答案create sequence seq_employee
 increment by 1
 start with 1
 nomaxvalue
 nocycle2 -----参考答案
declare 
 a number:=1;
 begin
 loop
 insert into employee values(seq_employee.nextval,a,'tom');
 commit;
 a:=a+1;
 exit when a>100;
 end loop;
 end;3 ------答案
declare
 vid number;
 vsalary number;
 vname varchar2(30);
 a number :=1;
 cursor cur_employee is select id,salary,name from employee order by id;
 begin
 open cur_employee;
 loop 
 fetch cur_employee into vid,vsalary,vname;
 dbms_output.put_line(vid||' '||vsalary||' '||vname);
 a:=a+1;
 exit when a>10;
 end loop;close cur_employee;
 end;下面的是同学写的:
 declare
 cursor mycur is select * from employee where rownum<=10 order by id;
 record employee%rowtype;
 begin
 open mycur;
 if mycur%isopen then
 loop
 fetch mycur into record;
 dbms_output.put_line(record.id);
 exit when mycur%notfound;
 end loop;
 end if;end;4 ------参考答案
create or replace procedure p_employee(vsalary number,vsalary2 number)
 as
 v_id number;
 v_salary number;
 v_name varchar2(30);
 cursor cur_employee is select id,salary,name from employee 
                        where salary between &vsalary and &vsalary2 order by salary;
 begin
 open cur_employee;
 loop
 fetch cur_employee into v_id,v_salary,v_name;
 dbms_output.put_line(v_id||' '||v_salary||' '||v_name);
 exit when cur_employee %notfound;
 end loop;
 end;5 -----答案
create or replace function f_employee return number
     as
     begin
     update employee set salary=salary+salary*0.05 where salary<2000 and name like 'wang%';
     if sql%rowcount=0 then
       return 1;
     else
       return 0;
     end if;
 end;6 ---参考答案
declare 
 a number;
 begin
 a:=f_employee();
 dbms_output.put_line(a);
 end;7 ----参考答案
create or replace procedure p_create_emp
 is
 v_count number;
 begin
 select count(*) into v_count from user_tables where table_name='EMPLOYEE';
 if v_count=0 then
 return;
 else
 execute immediate 'drop table employee';
 end if;
 end; 下面的是网上别人编写的参考答案
答案如下:
 SQL> create table employee(
 2 id number(10) not null, -- 员工工号
 3 salary number(10,2) default 0 not null, -- 薪水
 4 name varchar2(24) not null -- 姓名
 5 );表已创建。
---第一题答案:
 SQL> Create sequence seq_employee increment by 1 start with 1 nomaxvalue nocycle;序列已创建。
---第二题答案:
 SQL> declare i number;
 2 begin
 3 for i in 1 .. 100
 4 loop
 5 insert into employee
 6 values(seq_employee.nextval,1950+i,'王明'||to_char(i));
 7 commit;
 8 end loop;
 9 end;
 10 /PL/SQL 过程已成功完成。
 SQL> select * from employee where rownum<11;ID SALARY NAME
 ---------- ---------- ------------------------
 1 1951 王明1
 2 1952 王明2
 3 1953 王明3
 4 1954 王明4
 5 1955 王明5
 6 1956 王明6
 7 1957 王明7
 8 1958 王明8
 9 1959 王明9
 10 1960 王明10已选择10行。
-------第三题答案:
 SQL> declare
 2 cursor c is select id,salary,name from(select * from employee order by id) where rownum<11;
 3 v_record c%rowtype;
 4 begin
 5 open c;
 6 loop
 7 fetch c into v_record;
 8 exit when c%notfound;
 9 dbms_output.put_line(to_char(v_record.id)||','||to_char(v_record.salary)||','||v_record.name);
 10 end loop;
 11 close c;
 12 end;
 13 /
 1,1951,王明1
 2,1952,王明2
 3,1953,王明3
 4,1954,王明4
 5,1955,王明5
 6,1956,王明6
 7,1957,王明7
 8,1958,王明8
 9,1959,王明9
 10,1960,王明10PL/SQL 过程已成功完成。
 -------第四题答案
 SQL> create or replace procedure p_employee
 2 (iminsalary in number,
 3 imaxsalary in number)
 4 is
 5 begin
 6 for x in(select id,salary,name from(select * from employee where salary between iminsalary and imaxsalary) order by salary)
 7 loop
 8 dbms_output.put_line(to_char(x.id)||to_char(x.salary)||x.name);
 9 end loop;
 10 end;
 11 /过程已创建。
SQL> exec p_employee(2000,2007);
 502000王明50
 512001王明51
 522002王明52
 532003王明53
 542004王明54
 552005王明55
 562006王明56
 572007王明57PL/SQL 过程已成功完成。
---------第五题答案
 SQL> create or replace function f_employee return number
 is
 begin
 update employee set salary=salary+salary*0.05 where salary<2000 and name like '王%';
 commit;
 if sql%rowcount=0 then
 return 1;
 else
 return 0;
 end if;
 end;
 /函数已创建。
-----第六题答案
 SQL> declare a number;
 2 begin
 3 a:=f_employee();
 4 dbms_output.put_line(to_char(a));
 5 end;
 6 /
 0PL/SQL 过程已成功完成。
SQL> select * from employee where salary<2000 and name like '王%';
未选定行
 SQL> select * from employee where rownum<50;ID SALARY NAME
 ---------- ---------- ------------------------
 1 2048.55 王明1
 2 2049.6 王明2
 3 2050.65 王明3
 4 2051.7 王明4
 5 2052.75 王明5
 6 2053.8 王明6
 7 2054.85 王明7
 8 2055.9 王明8
 9 2056.95 王明9
 10 2058 王明10
 11 2059.05 王明11ID SALARY NAME
 ---------- ---------- ------------------------
 12 2060.1 王明12
 13 2061.15 王明13
 14 2062.2 王明14
 15 2063.25 王明15
 16 2064.3 王明16
 17 2065.35 王明17
 18 2066.4 王明18
 19 2067.45 王明19
 20 2068.5 王明20
 21 2069.55 王明21
 22 2070.6 王明22ID SALARY NAME
 ---------- ---------- ------------------------
 23 2071.65 王明23
 24 2072.7 王明24
 25 2073.75 王明25
 26 2074.8 王明26
 27 2075.85 王明27
 28 2076.9 王明28
 29 2077.95 王明29
 30 2079 王明30
 31 2080.05 王明31
 32 2081.1 王明32
 33 2082.15 王明33ID SALARY NAME
 ---------- ---------- ------------------------
 34 2083.2 王明34
 35 2084.25 王明35
 36 2085.3 王明36
 37 2086.35 王明37
 38 2087.4 王明38
 39 2088.45 王明39
 40 2089.5 王明40
 41 2090.55 王明41
 42 2091.6 王明42
 43 2092.65 王明43
 44 2093.7 王明44ID SALARY NAME
 ---------- ---------- ------------------------
 45 2094.75 王明45
 46 2095.8 王明46
 47 2096.85 王明47
 48 2097.9 王明48
 49 2098.95 王明49已选择49行。
-----第七题答案
 SQL> create or replace procedure p_create_emp
 2 is
 3 v_count number;
 4 begin
 5 select count(*) into v_count from user_tables where table_name='EMPLOYEE';
 6 if v_count=0 then
 7 return;
 8 else
 9 execute immediate 'drop table employee';
 10 end if;
 11 end;
 12 /过程已创建。
 ---------第八题答案
 SQL> exec p_create_emp;PL/SQL 过程已成功完成。
SQL> select * from employee;
 select * from employee
 *
 ERROR 位于第 1 行:
 ORA-00942: 表或视图不存在