PL/SQL语言基础

时间:2022-03-01 00:35:18

PL/SQL语言基础

进行PL/SQL编程前,要打开输出set serveroutput on

PL/SQL语言基础

1、创建一个匿名PL/SQL块,将下列字符输出到屏幕:“My PL/SQL Block Works”. 声明一个暂存员工号的变量v_empno,编写一个匿名块,查询smith员工的工号并输出显示。

PL/SQL语言基础

2、编写一个PL/SQL块,输出所有员工的姓名、员工号、工资和部门号。

PL/SQL语言基础

3、编写一个PL/SQL块,输出所有比本部门平均工资高的员工信息。

PL/SQL语言基础

4、编写一个PL/SQL块,输出所有员工及其部门领导的姓名、员工号及部门号。

PL/SQL语言基础

5、查询名为“Smith”的员工信息,并输出其员工号、姓名、工资、部门号。如果该员工不存在,则插入一条新记录,员工号为2010,员工名为“Smith”,工资为7500元,EMAIL为smith@sau.edu.cn,入职日期为“2018年10月10日”,职位编号为AD_VP,部门号为50。如果存在多个名为“Smith”的员工,则输出所有名为“Smith”的员工号、姓名、工资、入职日期、部门号、email。

PL/SQL语言基础

6、编写一个PL/SQL块,根据员工职位不同更新员工的工资。职位为AD_PRES、AD_VP、AD_ASST的员工工资增加1000元,职位为FI_MG,FI_ACCOUNT的员工工资增加800元,职位为AC_MGR,AC_ACCOUNT的员工工资增加700元,职位为SA_MAN,SA_REP的员工工资增加600元,职位为PU_MAN,PU_CLERK的员工工资增加500元,职位为ST_MAN,ST_CLERK,SH_CLERK的员工工资增加400元,职位为IT_PROG,MK_MAN,MK_REP的员工工资增加300元,其他职位的员工工资增加200元。

PL/SQL语言基础

7、编写一个PL/SQL块,修改员工号为201的员工工资为8000元,保证修改后的工资在职位允许的工资范围之内,否则取消操作,并说明原因。

PL/SQL语言基础

参考代码

DECLARE
CURSOR c_emp IS SELECT * FROM employees
where last_name='Smith';
v_empno c_emp%ROWTYPE;
BEGIN
OPEN c_emp;
DBMS_OUTPUT.PUT_LINE('My PL/SQL Block Works');
LOOP
FETCH c_emp INTO v_empno;
EXIT WHEN c_emp%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('the ID of simth is '||v_empno.employee_id);
END LOOP;
CLOSE c_emp;
END;
/ DECLARE
CURSOR c_emp IS SELECT * FROM employees;
v_empno c_emp%ROWTYPE;
BEGIN
OPEN c_emp;
LOOP
FETCH c_emp INTO v_empno;
EXIT WHEN c_emp%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(v_empno.first_name||' '||v_empno.last_name||' ID:'||v_empno.employee_id
||' salary:'||v_empno.salary );
END LOOP;
CLOSE c_emp;
END;
/ DECLARE
CURSOR c_emp IS SELECT * FROM employees where salary>(select AVG(salary) FROM employees);
v_empno c_emp%ROWTYPE;
BEGIN
OPEN c_emp;
LOOP
FETCH c_emp INTO v_empno;
EXIT WHEN c_emp%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(v_empno.first_name||' '||v_empno.last_name||' ID:'||v_empno.employee_id
||' salary:'||v_empno.salary );
END LOOP;
CLOSE c_emp;
END;
/ DECLARE
CURSOR c_emp IS SELECT a.first_name,a.last_name,
a.employee_id,a.department_id,
b.first_name mfirst_name,b.last_name mlast_name
FROM employees a inner join employees b
on a.employee_id=b.manager_id;
v_emp c_emp%ROWTYPE;
BEGIN
FOR v_emp IN c_emp LOOP
DBMS_OUTPUT.PUT_LINE(v_emp.first_name||' '||
v_emp.last_name||' '||v_emp.employee_id||' '||
v_emp.mfirst_name||' '||v_emp.mlast_name||' '||
v_emp.department_id);
END LOOP;
END;
/ DECLARE
v_emp employees%ROWTYPE;
BEGIN
SELECT * INTO v_emp FROM employees WHERE last_name='Smith';
DBMS_OUTPUT.PUT_LINE(v_emp.employee_id||' '||v_emp.first_name||' '||
v_emp.last_name||' '||v_emp.salary||' '||v_emp.department_id);
EXCEPTION
WHEN no_data_found THEN
INSERT INTO employees(employee_id,last_name,salary,
email,hire_date,job_id,department_id) VALUES
(2010,'Smith',7500,'smith@sau.edu.cn','10-10月 -2018','AD_VP',50);
WHEN too_many_rows THEN
FOR v_emp IN (SELECT * FROM employees WHERE last_name='Smith')LOOP
DBMS_OUTPUT.PUT_LINE(v_emp.employee_id||' '||v_emp.first_name||' '||
v_emp.last_name||' '||v_emp.salary||' '||v_emp.department_id);
END LOOP;
END;
/ DECLARE
v_sal employees.salary%TYPE;
BEGIN
FOR v_emp IN (SELECT * FROM employees) LOOP
IF v_emp.job_id='AD_PRES' OR v_emp.job_id='AD_VP' OR v_emp.job_id='AD_ASST'
THEN v_sal:=1000;
ELSIF v_emp.job_id='AD_MGR' OR v_emp.job_id='AD_ACCOUNT'
THEN v_sal:=800;
ELSIF v_emp.job_id='SA_MAN' OR v_emp.job_id='SA_REP'
THEN v_sal:=600;
ELSIF v_emp.job_id='PU_MAN' OR v_emp.job_id='PU_CLERK'
THEN v_sal:=500;
ELSIF v_emp.job_id='ST_MAN' OR v_emp.job_id='ST_CLERK' OR v_emp.job_id='SH_CLERK'
THEN v_sal:=400;
ELSIF v_emp.job_id='IT_PROG' OR v_emp.job_id='MK_MAN' OR v_emp.job_id='MK_REP'
THEN v_sal:=300;
ELSE v_sal:=200;
END IF;
UPDATE employees SET salary=v_emp.salary+v_sal WHERE employee_id=v_emp.employee_id;
END LOOP;
END;
/ DECLARE
v_salmin employees.salary%TYPE;
v_salmax employees.salary%TYPE;
v_sal employees.salary%TYPE;
e_highlimit EXCEPTION;
e_lowlimit EXCEPTION;
BEGIN
SELECT MAX(salary) INTO v_salmax FROM employees;
SELECT MIN(salary) INTO v_salmin FROM employees;
SELECT salary INTO v_sal FROM employees WHERE employee_id=201 ;
UPDATE employees SET salary=8000 WHERE employee_id=201;
IF v_sal>v_salmax THEN
RAISE e_highlimit;
ELSIF v_sal<v_salmin THEN
RAISE e_lowlimit;
END IF;
EXCEPTION
WHEN e_highlimit THEN
DBMS_OUTPUT.PUT_LINE('The salary is too large');
WHEN e_lowlimit THEN
DBMS_OUTPUT.PUT_LINE('The salary is too little');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('There is some wrong in selecting!');
END;
/