Live ORACLE

if you are Oracle Developer ,than this Blog most likely will make you happy.

About my Blog

This Blog particularized for Oracle Developers ... you will see interesting Codes for SQL , PL/SQL as well as new ideas for Developer Suite and Client Tools that will help you in your professional life ... and I hope I reach for your satisfaction.

About Me

I'm Hany Freedom , 25 years old , I live in EL-Minia City in Egypt , I'm Moderator at ArabOUG.org the best Arabic Forum for ORACLE on the Net. if you interested to know more about me .... just Click Here.

Creating the emp_admin Package


-- create the audit table to track changes
CREATE TABLE emp_audit (
date_of_action DATE,
user_id VARCHAR2(20),
package_name VARCHAR2(30));

CREATE OR REPLACE PACKAGE emp_admin
AS
-- Declare externally visible types, cursor, exception
TYPE emprectyp IS RECORD(emp_id NUMBER,
sal NUMBER);

CURSOR desc_salary RETURN emprectyp;

invalid_salary EXCEPTION;

-- Declare externally callable subprograms
FUNCTION hire_employee(last_name VARCHAR2,first_name VARCHAR2,email VARCHAR2,phone_number VARCHAR2,job_id VARCHAR2,salary NUMBER,commission_pct NUMBER,manager_id NUMBER,department_id NUMBER) RETURN NUMBER;

PROCEDURE fire_employee(emp_id NUMBER); -- overloaded subprogram

PROCEDURE fire_employee(emp_email VARCHAR2); -- overloaded subprogram

PROCEDURE raise_salary(emp_id NUMBER,amount NUMBER);

FUNCTION nth_highest_salary(n NUMBER) RETURN EMPRECTYP;
END emp_admin;
/

CREATE OR REPLACE PACKAGE BODY emp_admin
AS
number_hired NUMBER; -- visible only in this package

-- Fully define cursor specified in package
CURSOR desc_salary RETURN emprectyp IS
SELECT employee_id,
salary
FROM employees
ORDER BY salary DESC;

-- Fully define subprograms specified in package
FUNCTION Hire_employee
(last_name VARCHAR2,
first_name VARCHAR2,
email VARCHAR2,
phone_number VARCHAR2,
job_id VARCHAR2,
salary NUMBER,
commission_pct NUMBER,
manager_id NUMBER,
department_id NUMBER)
RETURN NUMBER
IS
new_emp_id NUMBER;
BEGIN
SELECT employees_seq.nextval
INTO new_emp_id
FROM dual;
INSERT INTO employees
VALUES (new_emp_id,
last_name,
first_name,
email,
phone_number,
SYSDATE,
job_id,
salary,
commission_pct,
manager_id,
department_id);
number_hired := number_hired
+ 1;
dbms_output.Put_line('The number of employees hired is '
||To_char(number_hired));
RETURN new_emp_id;
END hire_employee;

PROCEDURE Fire_employee
(emp_id NUMBER)
IS
BEGIN
DELETE FROM employees
WHERE employee_id = emp_id;
END fire_employee;

PROCEDURE Fire_employee
(emp_email VARCHAR2)
IS
BEGIN
DELETE FROM employees
WHERE email = emp_email;
END fire_employee;

-- Define local function, available only inside package
FUNCTION Sal_ok
(jobid VARCHAR2,
sal NUMBER)
RETURN BOOLEAN
IS
min_sal NUMBER;
max_sal NUMBER;
BEGIN
SELECT Min(salary),
Max(salary)
INTO min_sal,max_sal
FROM employees
WHERE job_id = jobid;
RETURN (sal >= min_sal)
AND (sal <= max_sal);
END sal_ok;

PROCEDURE Raise_salary
(emp_id NUMBER,
amount NUMBER)
IS
sal NUMBER(8,2);
jobid VARCHAR2(10);
BEGIN
SELECT job_id,
salary
INTO jobid,sal
FROM employees
WHERE employee_id = emp_id;
IF Sal_ok(jobid,sal
+ amount) THEN
UPDATE employees
SET salary = salary
+ amount
WHERE employee_id = emp_id;
ELSE
RAISE invalid_salary;
END IF;
EXCEPTION -- exception-handling part starts here

WHEN invalid_salary THEN
dbms_output.Put_line('The salary is out of the specified range.');
END raise_salary;

FUNCTION Nth_highest_salary
(n NUMBER)
RETURN EMPRECTYP
IS
emp_rec EMPRECTYP;
BEGIN
OPEN desc_salary;
FOR i IN 1.. n LOOP
FETCH desc_salary INTO emp_rec;
END LOOP;
CLOSE desc_salary;
RETURN emp_rec;
END nth_highest_salary;
BEGIN -- initialization part starts here

INSERT INTO emp_audit
VALUES (SYSDATE,
USER,
'EMP_ADMIN');
number_hired := 0;
END emp_admin;
/

-- calling the package procedures
DECLARE
new_emp_id NUMBER(6);
BEGIN
new_emp_id := emp_admin.Hire_employee('Belden','Enrique','EBELDEN','555.111.2222',
'ST_CLERK',2500,.1,101,110);
dbms_output.Put_line('The new employee id is '
||To_char(new_emp_id));
emp_admin.Raise_salary(new_emp_id,100);
dbms_output.Put_line('The 10th highest salary is '
||To_char(emp_admin.Nth_highest_salary(10).sal)
||', belonging to employee: '
||To_char(emp_admin.Nth_highest_salary(10).emp_id));
emp_admin.Fire_employee(new_emp_id);
-- you could also delete the newly added employee as follows:
-- emp_admin.fire_employee('EBELDEN');
END;
/



OUTPUT of calling the package procedures:

The number of employees hired is 1
The new employee id is 207
The 10th highest salary is 11500, belonging to employee: 168

PL/SQL procedure successfully completed.

Share/Save/Bookmark

Matching Package Specifications and Bodies

CREATE PACKAGE emp_bonus
AS
PROCEDURE calc_bonus(date_hired employees.hire_date%TYPE);
END emp_bonus;
/

CREATE PACKAGE BODY emp_bonus
AS
-- the following parameter declaration raises an exception
-- because 'DATE' does not match employees.hire_date%TYPE
-- PROCEDURE calc_bonus (date_hired DATE) IS
-- the following is correct because there is an exact match
PROCEDURE Calc_bonus
(date_hired employees.hire_date%TYPE)
IS
BEGIN
dbms_output.Put_line('Employees hired on '
||date_hired
||' get bonus.');
END;
END emp_bonus;
/


Share/Save/Bookmark

A Simple Package Specification Without a Body

CREATE PACKAGE trans_data
AS -- bodiless package

TYPE timerec IS RECORD(minutes SMALLINT,
hours SMALLINT);

TYPE transrec IS RECORD(category VARCHAR2(10),
account INT,
amount REAL,
time_of TIMEREC);

minimum_balance CONSTANT REAL := 10.00;

number_processed INT;

insufficient_funds EXCEPTION;
END trans_data;
/



Package created.

Share/Save/Bookmark

Aliasing from Assigning Cursor Variables to Same Work Area


DECLARE
TYPE empcurtyp IS REF CURSOR;
c1 EMPCURTYP;
c2 EMPCURTYP;
PROCEDURE Get_emp_data
(emp_cv1 IN OUT EMPCURTYP,
emp_cv2 IN OUT EMPCURTYP)
IS
emp_rec employees%ROWTYPE;
BEGIN
OPEN emp_cv1 FOR
SELECT *
FROM employees;
emp_cv2 := emp_cv1;
FETCH emp_cv1 INTO emp_rec; -- fetches first row
FETCH emp_cv1 INTO emp_rec; -- fetches second row
FETCH emp_cv2 INTO emp_rec; -- fetches third row
CLOSE emp_cv1;
dbms_output.Put_line('The following raises an invalid cursor');
-- FETCH emp_cv2 INTO emp_rec; raises invalid cursor when get_emp_data is called
END;
BEGIN
Get_emp_data(c1,c2);
END;
/




OUTPUT:-

The following raises an invalid cursor

PL/SQL procedure successfully completed.


Share/Save/Bookmark

Aliasing Passing Same Parameter Multiple Times

 DECLARE
n NUMBER := 10;
PROCEDURE Do_something
(n1 IN NUMBER,
n2 IN OUT NUMBER,
n3 IN OUT NOCOPY NUMBER)
IS
BEGIN
n2 := 20;

dbms_output.Put_line(n1); -- prints 10

n3 := 30;

dbms_output.Put_line(n1); -- prints 30

END;
BEGIN
Do_something(n,n,n);

dbms_output.Put_line(n); -- prints 20

END;
/


OUTPUT:-

10
30
20

PL/SQL procedure successfully completed.


Share/Save/Bookmark

SCROLL_VIEW Built-in

SCROLL_VIEW Built-in moves the view to a different position on its canvas by changing the Viewport X Position on Canvas and Viewport Y Position on Canvas properties. Moving the view makes a different area of the canvas visible to the operator, but does not change the position of the view within the window.



=====================================================
/*
** Built-in: SCROLL_VIEW
** Example: Scroll the view whose name is passed in 10% to
** the right or left depending on the 'direction'
** parameter.
*/
PROCEDURE Scroll_ten_percent
(viewname VARCHAR2,
direction VARCHAR2)
IS
vw_id VIEWPORT;
vw_wid NUMBER;
vw_x NUMBER;
cn_id CANVAS;
cn_wid NUMBER;
ten_percent NUMBER;
new_x NUMBER;
old_y NUMBER;
BEGIN
/*
** Get the id's for the View and its corresponding canvas
*/
vw_id := Find_view(viewname);

cn_id := Find_canvas(viewname);

/*
** Determine the view width and corresponding canvas
** width.
*/
vw_wid := Get_view_property(vw_id,width);

cn_wid := Get_canvas_property(cn_id,width);

/*
** Calculate how many units of canvas width are outside of
** view, and determine 10% of that.
*/
ten_percent := 0.10 * (cn_wid - vw_wid);

/*
** Determine at what horizontal position the view
** currently is on the corresponding canvas
*/
vw_x := Get_view_property(vw_id,viewport_x_pos_on_canvas);

/*
** Calculate the new x position of the view on its canvas
** to effect the 10% scroll in the proper direction.
** Closer than ten percent of the distance to the edge
** towards which we are moving, then position the view
** against that edge.
*/
IF direction = 'LEFT' THEN
IF vw_x > ten_percent THEN
new_x := vw_x - ten_percent;
ELSE
new_x := 0;
END IF;
ELSIF direction = 'RIGHT' THEN
IF vw_x < cn_wid - vw_wid - ten_percent THEN
new_x := vw_x + ten_percent;
ELSE
new_x := cn_wid - vw_wid;
END IF;
END IF;

/*
** Scroll the view that much horizontally
*/
old_y := Get_view_property(vw_id,viewport_y_pos_on_canvas);

Scroll_view(vw_id,new_x,old_y);
END;
=====================================================




Download the fmb file from this link :-






Share/Save/Bookmark

November/December 2009

Look Inside >> 
November/December 2009

Share/Save/Bookmark
Newer Posts Older Posts Home Page
 
http://www.dpriver.com/images/sqlpp-banner-2.png

Thanks for reading my Blog ... you Visitor Number :-