Oracle大型数据库 作业10
Posted 霜序0.2℃
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Oracle大型数据库 作业10相关的知识,希望对你有一定的参考价值。
1
将employees 表中所有员工信息存储到一个嵌套表变量中,输出该嵌套表中所有大于10000元的元素。
set SERVEROUTPUT ON
declare
cursor all_EMPS is select *
from employees;
type t_emp is table of employees%ROWTYPE;
v_emp t_emp := t_emp();
indexvalue BINARY_INTEGER := 1;
begin
for emp in all_EMPS
LOOP
v_emp.extend;
v_emp(indexvalue) := emp;
indexvalue := indexvalue + 1;
end loop;
for i in 1..v_emp.count
loop
if v_emp(i).salary > 10000 then
dbms_output.put_line(v_emp(i).employee_id || chr(32) || v_emp(i).first_name || chr(32) ||
v_emp(i).last_name || chr(32) || v_emp(i).salary || chr(32) ||
v_emp(i).department_id);
end if;
end loop;
end;
2
将 departments 表中所有部门信息存储到一个可变数组变量中,输出该可变数组中素的个数及所有元素。
set SERVEROUTPUT ON
declare
cursor c_dept is select *
from departments;
type t_dept is VARRAY(200) of departments%ROWTYPE;
v_dept t_dept := t_dept();
indexvalue BINARY_INTEGER := 1;
begin
for v in c_dept
loop
v_dept.extend;
v_dept(indexvalue) := v;
indexvalue := indexvalue + 1;
end loop;
for i in 1..v_dept.last
loop
dbms_output.put_line(v_dept(i).department_id || chr(32) || v_dept(i).department_name
|| chr(32) || v_dept(i).location_id);
end loop;
DBMS_OUTPUT.put_line('可变数组中素的个数:' || v_dept.count);
end;
3
创建一个存储过程,以表名和一个列名为参数,创建一个表。
set SERVEROUTPUT ON
create or replace PROCEDURE create_table(
tb_name VARCHAR2, col_name VARCHAR2, col_type VARCHAR2)
as
v_create VARCHAR2(100);
begin
v_create := 'create table ' || tb_name || '(' || col_name || ' ' ||
col_type || ')';
EXECUTE IMMEDIATE v_create;
end;
4
创建一个存储过程,将某个表的SELECT权限授予某个用户。
set SERVEROUTPUT ON
create or replace PROCEDURE grant_select(
tb_name VARCHAR2, user_name VARCHAR2)
as
v_grant VARCHAR2(100);
begin
v_grant := 'grant select on ' || tb_name || ' to ' || user_name;
EXECUTE IMMEDIATE v_grant;
end;
5
创建一个存储过程,以员工号为参数删除该员工。
set serveroutput on
CREATE OR REPLACE PROCEDURE del_staff(p_empno NUMBER)
AS
v_str VARCHAR2(100);
BEGIN
v_str := 'DELETE from employees WHERE employee_id = ' || p_empno;
EXECUTE IMMEDIATE v_str;
END;
6
CREATE OR REPLACE PROCEDURE dyn_dml_test(p_col VARCHAR2, p_value VARCHAR2, p_empno NUMBER)
IS
v_str VARCHAR2(100);
BEGIN
v_str := 'UPDATE employees SET ' || p_col ||
'=:ph_value WHERE employee_id=:ph_empno';
EXECUTE IMMEDIATE v_str USING p_value,p_empno;
END;
7
创建一个存储过程,参数为列名、值,查询该员工信息。
set SERVEROUTPUT ON
CREATE OR REPLACE PROCEDURE query_emp(p_col VARCHAR2, p_value VARCHAR2)
IS
v_fname employees.first_name%TYPE;
v_lname employees.last_name%TYPE;
v_empno employees.employee_id%TYPE;
v_deptno employees.department_id%TYPE;
v_sal employees.salary%TYPE;
v_str VARCHAR2(200);
BEGIN
v_str := 'SELECT employee_id,first_name,last_name,salary,
department_id FROM employees WHERE ' || p_col || '=:ph_value';
EXECUTE IMMEDIATE v_str
INTO v_empno,v_fname,v_lname,v_sal,v_deptno
USING p_value;
DBMS_OUTPUT.PUT_LINE(v_empno || chr(32) || v_fname || chr(32) ||
v_lname || chr(32) || v_sal || chr(32) || v_deptno);
END;
8
set SERVEROUTPUT ON
CREATE OR REPLACE PROCEDURE query_dept(P_COL VARCHAR2, P_VALUE VARCHAR2)
is
V_DEPT DEPARTMENTS%ROWTYPE;
V_STR VARCHAR2(200);
BEGIN
V_STR := 'SELECT * FROM DEPARTMENTS WHERE ' || P_COL || '=:PH_VALUE';
EXECUTE IMMEDIATE V_STR INTO V_DEPT USING P_VALUE;
DBMS_OUTPUT.PUT_LINE('部门号:' || V_DEPT.DEPARTMENT_ID ||
chr(32) || '部门名称:' || V_dept.department_name);
END;
9
创建一个存储过程,根据参数查询符合特定条件的员工信息(如根据工资查询、部门号查询、入职时间等)。
CREATE OR REPLACE PROCEDURE multi_query(p_col VARCHAR2, p_condition VARCHAR2, p_value VARCHAR2)
AS
TYPE t_cursor IS REF CURSOR;
v_cursor t_cursor;
lv_query VARCHAR2(200);
v_empno employees.employee_id%TYPE;
v_fname employees.first_name%TYPE;
v_lname employees.last_name%TYPE;
v_sal employees.salary%TYPE;
v_deptno employees.department_id%TYPE;
BEGIN
lv_query := 'SELECT employee_id,first_name,last_name,salary,
department_id FROM employees WHERE ' || p_col || p_condition ||
':ph_value';
OPEN v_cursor FOR lv_query USING p_value;
LOOP
FETCH v_cursor INTO
v_empno,v_fname,v_lname,v_sal,v_deptno;
EXIT WHEN v_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(v_empno || ' ' || v_fname || ' ' ||
v_lname || ' ' || v_sal || ' ' || v_deptno);
END LOOP;
END;
10
创建一个包,实现对HUMAN_RESOURCE数据库中数据进行分页查询。
CREATE OR REPLACE PACKAGE BODY emppkg IS
PROCEDURE sp_page(
p_pageSize IN OUT INT,
p_curPageNo IN OUT INT,
p_outCollection OUT emplist
)
AS
v_startRownum NUMBER;
v_endRownum NUMBER;
v_totalRecords NUMBER;
v_totalPages NUMBER;
indexValue BINARY_INTEGER := 1;
BEGIN
SELECT count(*) INTO v_totalRecords FROM employees;
IF p_pageSize < 0 THEN
p_pageSize := 10;
END IF;
IF MOD(v_totalRecords, p_pageSize) = 0 THEN
v_totalPages := v_totalRecords / p_pageSize;
ELSE
v_totalPages := floor(v_totalRecords / p_pageSize) + 1;
END IF;
IF p_curPageNo < 1 THEN
p_curPageNo := 1;
END IF;
IF p_curPageNo > v_totalPages THEN
p_curPageNo := v_totalPages;
END IF;
v_startRownum := (p_curPageNo - 1) * p_pageSize + 1;
v_endRownum := p_curPageNo * p_pageSize;
p_outCollection := emplist();
FOR v_emp IN (SELECT *
FROM (
SELECT ROWNUM rn,
employee_id,
first_name,
last_name,
salary,
department_id
FROM (
SELECT employee_id,
first_name,
last_name,
salary,
department_id
FROM employees
WHERE salary IS NOT NULL
ORDER BY salary) A
WHERE ROWNUM <= v_endRownum) B
WHERE rn >= v_startRownum )
LOOP
p_outCollection.extend;
p_outCollection(indexValue) := v_emp;
indexValue := indexValue + 1;
END LOOP;
END sp_page;
END emppkg;
如果权限不足需要把权限赋给现在使用的用户
以上是关于Oracle大型数据库 作业10的主要内容,如果未能解决你的问题,请参考以下文章