前几天面试题,大家可以看看,我把自己做的答案附上,所有代码都编译成功,由于不是正式开发
环境,所以过程和函数中没有用到exception,平常我写代码的时候都有用的哈。呵呵
考试总分为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分)
6.写一个匿名语句块,用于执行函数f_employee,并打印执行该函数的结果。(8分)
7.创建存储过程p_create_emp,用于判断表employee是否存在,如果存在则删除该表。(15分)
8.写一个匿名语句块,用于执行存储过程p_create_emp。(7分)
答案如下:
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行。
3.写一个语句块,在语句块中定义一个显式游标,按id升序排列,打印表employee中前十条数据。(15分)
-------第三题答案:
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;