Establish package specifications
- Create Or ReplacePackage emp_packageIs
- G_deptno number (3): = 30;
- ProcedureAdd_employee (eno number, ename varchar2, sal number, dno numberDefaultG_deptno );
- ProcedureFire_employee (eno number );
- FunctionGet_sal (eno number)ReturnNumber;
- EndEmp_package;--- The end can be followed by the defined name
Create a package
- Create Or ReplacePackage body emp_packageIs
- FunctionValidate_deptno (v_deptno number)ReturnBooleanIs---- Validate_deptno
- V_tempInt;
- Begin
- Select1IntoV_tempFromDeptWhereDeptno = v_deptno;
- Return True;
- Exception
- WhenNo_data_foundThen Return False;
- EndValidate_deptno;
- ProcedureAdd_employee (eno number, ename varchar2, sal number, dno numberDefaultG_deptno)Is--- Add_employee
- Begin
- If validate_deptno (dno)Then
- Insert IntoEmp (empno, ename, sal, deptno)Values(Eno, ename, sal, dno );
- Else
- RAISE_ApPLICATION_ERROR (-20000,'The team does not exist');
- EndIf;
- Exception
- WhenDup_val_on_indexThenRAISE_APpLICATION_ERROR (-20011,'This employee already exists');
- EndAdd_employee;
- ProcedureFire_employee (eno number)Is-------- Fire_employee
- Begin
- Delete FromEmpWhereEmpno = eno;
- If SQL % notfoundThen
- RAISE_APPLICATION_ERROR (-20012,'The employee does not exist');
- EndIf;
- EndFire_employee;
- FunctionGet_sal (eno number)ReturnNumberIs --- Get_sal
- V_sal emp. sal % type; <preName="Code"Class ="SQL">Create Or ReplacePackage body emp_packageIs
- ProcedureAdd_employee (eno numberk, ename varchar2, salary number, dno numberDefaultG_deptno)Is
- Begin
- If validate_deptno (dno)Then
- Insert IntoEmp (empno, ename, sal, deptno)Values(Eno, ename, salary, dno );
- Else
- Raise_application_error (-20010,'The team does not exist');
- EndIf;
- Exception
- WhenDup_val_on_indexThen
- Raise_application_error (-20011,'This employee already exists');
- End;
Begin select sal into v_sal from emp where empno = eno; return v_sal; exception when no_data_found then RAISE_ApPLICATION_ERROR (-20012, 'this employee does not exist'); end get_sal; end emp_package;
Call package components 1. Call in the same package
- Create Or ReplacePackage body emp_packageIs<PreName="Code"Class ="SQL">
Procedure add_employee (eno numberk, ename varchar2, salary number, dno number default g_deptno) isbegin if exist (dno) then insert into emp (empno, ename, sal, deptno) values (eno, ename, salary, dno); else raise_application_error (-20010, 'this department does not exist'); end if; exception when dup_val_on_index then raise_application_error (-20011, 'this employee already exists '); end;
2. Call Public Variables
- Declare
- Begin
- Emp_package.g_deptno: = 21;
- End;
3. Call the public process
- Declare
- Begin
- Emp_package.add_employee (1212,'Yang');------ An error will be reported if the Department does not give a value
- Emp_package.add_employee (2121,'Authorization');
- End;
4. Call Public Functions
- Declare
- Salary number;
- Begin
- Salary: = emp_package.get_sal (7788 );
- Dbms_output.put_line (salary );
- End;
---- When using other user identities to call a public component, the user name and package name must be prefixed before the component name
---- SCOTT. EMP_PACKAGE .....
---- When the public component that calls the remote database package is, add the package name as the prefix before the component name. After the component name, the database chain name must be followed as the suffix.
---- EMP_PACKAGE.ADD_EMPLOYEE @ Clerk (1111, 'Scott ', 1233,10)
---- View source code
---- Select text from user_source where name = 'emp_package' and type = 'package'