oracle - write package output results to a file on client side - oracle

So i have this:
CREATE OR REPLACE PACKAGE my_first_package
IS
PROCEDURE employee_analysis
(p_id IN NUMBER := 100, /*default formal parameter with no arguments for invokation */
p_percent IN NUMBER := 0.01); /* -||- */
END my_first_package;
CREATE OR REPLACE PACKAGE BODY my_first_package
IS
PROCEDURE employee_analysis
(p_id IN NUMBER := 100,
p_percent IN NUMBER := 0.01)
IS
CURSOR c_city IS
(SELECT l.city
FROM employees e
INNER JOIN departments d
ON (e.department_id = d.department_id)
INNER JOIN locations l
ON (l.location_id = d.location_id)
WHERE e.employee_id = p_id);
CURSOR c_manager IS
(SELECT e1.last_name
FROM employees e1
INNER JOIN
employees e2
ON (e1.employee_id = e2.manager_id)
WHERE e2.employee_id = p_id);
CURSOR c_department_name IS
(SELECT department_name
FROM employees e
INNER JOIN
departments d
ON (e.department_id = d.department_id)
WHERE e.employee_id = p_id);
/*-----------------------------------------------------------------------------*/
v_annual_sal NUMBER(9,2);
v_monthly_sal NUMBER(9,2);
v_last_name VARCHAR2(10);
v_deptno NUMBER(3);
v_length NUMBER(2);
v_tenure NUMBER(5);
v_job_id VARCHAR2(20);
v_hire_date DATE;
v_city VARCHAR(25);
v_commission_pct NUMBER(2,2);
v_phone_number VARCHAR2(20);
v_manager VARCHAR2(20);
v_comm_calc NUMBER(10,2);
v_email VARCHAR2(10);
v_department VARCHAR2(20);
v_count NUMBER(4);
v_old_salary NUMBER(9,2);
v_new_salary NUMBER(9,2);
v_lname VARCHAR2(10);
v_phone_number_format VARCHAR2(25);
v_phone_number_length NUMBER(3);
v_tax NUMBER(8,4);
v_sum_sal_departments NUMBER;
BEGIN
DBMS_OUTPUT.PUT_LINE('Welcome to the summary of an employee based on his unique id');
DBMS_OUTPUT.PUT_LINE('============================================================');
/*-----------------------------------------------------------------------------*/
SELECT salary, last_name, department_id,
TRUNC(MONTHS_BETWEEN(SYSDATE,hire_date),0), job_id,
hire_date, commission_pct, phone_number, email, LENGTH(phone_number)
INTO v_monthly_sal, v_last_name, v_deptno,
v_tenure, v_job_id, v_hire_date, v_commission_pct,
v_phone_number, v_email, v_phone_number_length
FROM employees
WHERE employee_id = p_id;
/*-----------------------------------------------------------------------------*/
v_count := SQL%ROWCOUNT;
DBMS_OUTPUT.PUT_LINE(v_count||' Employee found...');
/*-----------------------------------------------------------------------------*/
v_annual_sal := v_monthly_sal * 12;
v_length := LENGTH(v_last_name);
DBMS_OUTPUT.PUT_LINE('Employee:-> ' || v_last_name || ' ,and his name contains: ' || v_length ||' chars');
DBMS_OUTPUT.PUT_LINE(q'[Belong's to department: ]' || v_deptno);
/*-----------------------------------------------------------------------------*/
IF (v_monthly_sal < v_annual_sal)
THEN
DBMS_OUTPUT.PUT_LINE('Has a annual salary of:-> ' || v_annual_sal);
ELSE
DBMS_OUTPUT.PUT_LINE('Something wrong in the formula!');
END IF;
/*-------------------------------------------------------------------------------*/
IF v_commission_pct IS NULL
THEN
DBMS_OUTPUT.PUT_LINE('No Commission added to the annual salary!');
ELSE
DBMS_OUTPUT.PUT_LINE('Commission percentage to the salary is:-> '|| v_commission_pct ||'%');
v_comm_calc := (v_annual_sal * v_commission_pct) + v_annual_sal;
DBMS_OUTPUT.PUT_LINE('And calculated with annual salary is:-> ' ||v_comm_calc);
END IF;
/*-------------------------------------------------------------------------------*/
DBMS_OUTPUT.PUT_LINE('Working for:-> '|| v_tenure || ' months as '|| v_job_id);
DBMS_OUTPUT.PUT_LINE('Started in:-> '|| v_hire_date);
/*-------------------------------------------------------------------------------*/
IF v_phone_number_length = 12
THEN
v_phone_number_format := '(' || SUBSTR(v_phone_number,1,3) || ')' ||
'-' || SUBSTR(v_phone_number,5,3) ||
'-' || SUBSTR(v_phone_number,9,4);
DBMS_OUTPUT.PUT_LINE('Phone number:-> '|| v_phone_number_format);
ELSIF v_phone_number_length = 18
THEN
v_phone_number_format := '(' || SUBSTR(v_phone_number,1,3) || ')' ||
'-' || SUBSTR(v_phone_number,5,2) ||
'-' || SUBSTR(v_phone_number,8,4) || '-'
|| SUBSTR(v_phone_number,13,6);
DBMS_OUTPUT.PUT_LINE('Phone number:-> '|| v_phone_number_format);
ELSE
DBMS_OUTPUT.PUT_LINE('Phone number digits not in range, check the length of the phone numbers from the table');
END IF;
/*-------------------------------------------------------------------------------*/
DBMS_OUTPUT.PUT_LINE('Email:-> '||v_email);
/*-------------------------------------------------------------------------------*/
OPEN c_city;
FETCH c_city
INTO v_city;
IF c_city%FOUND
THEN
DBMS_OUTPUT.PUT_LINE('Location:-> '||v_city);
ELSE
DBMS_OUTPUT.PUT_LINE('Employee location unknown');
END IF;
CLOSE c_city;
/*-------------------------------------------------------------------------------*/
OPEN c_manager;
FETCH c_manager
INTO v_manager;
IF c_manager%FOUND
THEN
DBMS_OUTPUT.PUT_LINE('Is in the eyes of manager:-> '||v_manager);
ELSE
DBMS_OUTPUT.PUT_LINE('Slave '||v_last_name||' is free!');
END IF;
CLOSE c_manager;
/*-------------------------------------------------------------------------------*/
OPEN c_department_name;
FETCH c_department_name
INTO v_department;
IF c_department_name%FOUND
THEN
DBMS_OUTPUT.PUT_LINE('Department Name:-> '||v_department);
ELSE
DBMS_OUTPUT.PUT_LINE('Employee ' ||v_last_name||' belongs to no department!');
END IF;
/*--------------------------------------------------------------------------------*/
DBMS_OUTPUT.PUT_LINE('Checking the current employee with id:-> '|| p_id ||'..');
IF (check_sal2(p_id) IS NULL)
THEN
DBMS_OUTPUT.PUT_LINE('The function returned NULL due to exception, therefore employee does not exist!');
ELSIF (check_sal2(p_id))
THEN
DBMS_OUTPUT.PUT_LINE('Employees salary > average of department '||v_deptno||' where he belongs.');
ELSE
DBMS_OUTPUT.PUT_LINE('Salary < average of department '||v_deptno||', where he belongs.');
END IF;
/*--------------------------------------------------------------------------------*/
SELECT salary
INTO v_old_salary
FROM employees
WHERE employee_id = p_id;
DBMS_OUTPUT.PUT_LINE('Before the raise of ' || p_percent ||
' %, the salary was:-> '|| v_old_salary);
/*--------------------------------------------------------------------------------*/
IF (p_percent > 0.01)
THEN
DBMS_OUTPUT.PUT_LINE('Maximum increase allowance for the moment is only 0.01 %, thus no increase in salary is made');
ELSIF (p_percent < 0.01)
THEN
DBMS_OUTPUT.PUT_LINE('Minimum percent allowance for the moment is only 0.01 %, thus no increase in salary is made');
ELSE
UPDATE employees
SET salary = salary * (1 + p_percent/100)
WHERE employee_id = p_id;
SELECT last_name, salary
INTO v_lname, v_new_salary
FROM employees
WHERE employee_id = p_id;
DBMS_OUTPUT.PUT_LINE('After the raise of ' || p_percent ||
' %, the salary is:-> '|| v_new_salary);
END IF;
/*--------------------------------------------------------------------------------*/
DBMS_OUTPUT.PUT_LINE('===========================================================');
DBMS_OUTPUT.PUT_LINE('===========================================================');
DBMS_OUTPUT.PUT_LINE('===========================================================');
FOR i IN (SELECT SUM(salary) AS "SUMY", department_id
FROM employees
GROUP BY department_id)
LOOP
IF i.department_id IS NULL
THEN
DBMS_OUTPUT.PUT_LINE('Unknown department '|| i.department_id ||' earns:-> '|| i.sumy);
ELSE
DBMS_OUTPUT.PUT_LINE('Department '|| i.department_id ||' earns:-> '|| i.sumy);
END IF;
END LOOP;
FOR j IN (SELECT SUM(sumy) AS "DEP_SUM"
FROM (SELECT SUM(salary) AS "SUMY"
FROM employees
GROUP BY department_id))
LOOP
v_sum_sal_departments := j.dep_sum;
DBMS_OUTPUT.PUT_LINE('Total income on all departments:-> '|| j.dep_sum);
END LOOP;
SELECT taxes_pkg.tax(salary)
INTO v_tax
FROM employees
WHERE employee_id = p_id;
DBMS_OUTPUT.PUT_LINE('Salary after 0.08 % tax withdrawal:-> '|| v_tax);
FOR k IN (SELECT last_name
FROM employees
WHERE salary = (SELECT MAX(salary)
FROM employees))
LOOP
DBMS_OUTPUT.PUT_LINE('Employee with the highest salary is:-> '||k.last_name);
END LOOP;
FOR k IN (SELECT last_name, hire_date
FROM employees
WHERE hire_date = (SELECT MAX(hire_date)
FROM employees))
LOOP
DBMS_OUTPUT.PUT_LINE('Our newest employees are:-> ' || k.last_name);
END LOOP;
FOR m IN (SELECT last_name
FROM employees
WHERE hire_date = (SELECT MIN(hire_date)
FROM employees))
LOOP
DBMS_OUTPUT.PUT_LINE('Our oldest employees are:-> '||m.last_name);
END LOOP;
/*--------------------------------------------------------------------------------*/
EXCEPTION
WHEN NO_DATA_FOUND
THEN
DBMS_OUTPUT.PUT_LINE('Employee with id:-> ' || p_id || ' does not exist, check data from your tables!');
WHEN OTHERS
THEN
DBMS_OUTPUT.PUT_LINE('Unknown propagation');
END employee_analysis;
END my_first_package;
I seached about something called utl_file package for exporting data from oracle database and so on, but i didn't understand much and didn't found what i was looking for. Basically what i want is, how can i "export" all of the output from the screen after running this useless package above? Is there a way to modify the package add features and stuff to it? It is possible? Thanks...

Related

Adding conditionals to PL/SQL cursors

In the below PL/SQL program I have 2 cursors that are almost similar in functionality. I'm not sure, how I can retain the same functionality just by having one cursor and get rid of the get_all_employes cursor which doesn't have the where clause.
Thanks in advance for your time and help!
CREATE OR REPLACE PROCEDURE EMP_DETAILS(dept_id VARCHAR2) IS
CURSOR get_all_employes IS
SELECT EMP_ID, EMP_NAME, EMP_SAL, EMP_DEPT FROM EMP;
CURSOR get_employee_info IS
SELECT EMP_ID, EMP_NAME, EMP_SAL, EMP_DEPT FROM EMP WHERE EMP_DEPT = dept_id;
BEGIN
IF dept_id IS NULL THEN
FOR X IN get_all_employes;
DBMS_OUTPUT.PUT_LINE(X.EMP_ID || ' ' || X.EMP_NAME || ' ' || X.EMP_SAL || X.EMP_DEPT);
END LOOP;
ELSE
FOR Y IN get_all_employes;
DBMS_OUTPUT.PUT_LINE(Y.EMP_ID || ' ' || Y.EMP_NAME || ' ' || Y.EMP_SAL || Y.EMP_DEPT);
END LOOP;
END IF;
END EMP_DETAILS;
/
Use one cursor and include WHERE EMP_DEPT = dept_id OR dept_id IS NULL:
CREATE PROCEDURE EMP_DETAILS(
dept_id IN EMP.EMP_DEPT%TYPE
)
IS
CURSOR get_employee_info IS
SELECT EMP_ID,
EMP_NAME,
EMP_SAL,
EMP_DEPT
FROM EMP
WHERE EMP_DEPT = dept_id
OR dept_id IS NULL;
BEGIN
FOR Y IN get_employee_info LOOP
DBMS_OUTPUT.PUT_LINE(Y.EMP_ID || ' ' || Y.EMP_NAME || ' ' || Y.EMP_SAL || Y.EMP_DEPT);
END LOOP;
END EMP_DETAILS;
/
So, for the test data:
CREATE TABLE emp ( EMP_ID, EMP_NAME, EMP_SAL, EMP_DEPT ) AS
SELECT 1, 'a', 100, 1 FROM DUAL UNION ALL
SELECT 2, 'b', 200, 1 FROM DUAL UNION ALL
SELECT 3, 'c', 300, 2 FROM DUAL;
Then:
BEGIN
emp_details( NULL );
END;
/
Would output all the rows:
1 a 1001
2 b 2001
3 c 3002
and:
BEGIN
emp_details( 1 );
END;
/
would output only the rows for department 1:
1 a 1001
2 b 2001
db<>fiddle here
Try something like this (fyi your example code does not compile)
create or replace PROCEDURE EMP_DETAILS(dept_id VARCHAR2) IS
TYPE EmpCurTyp IS REF CURSOR;
emp_cv EmpCurTyp;
empRec EMP%ROWTYPE;
BEGIN
IF dept_id IS NULL THEN
open emp_cv for SELECT EMP_ID, EMP_NAME, EMP_SAL, EMP_DEPT FROM EMP;
ELSE
open emp_cv for SELECT EMP_ID, EMP_NAME, EMP_SAL, EMP_DEPT FROM EMP WHERE EMP_DEPT = dept_id;
END IF;
LOOP
FETCH emp_cv INTO empRec;
IF emp_cv%NOTFOUND
THEN
EXIT;
END IF;
DBMS_OUTPUT.PUT_LINE(empREc.EMP_ID || ' ' || empREc.EMP_NAME || ' ' || empREc.EMP_SAL || empREc.EMP_DEPT);
END LOOP;
CLOSE emp_cv;
END EMP_DETAILS;

"No Data found" error when compiling PLSQL Function

When trying to compile in order to execute both functions(#directory) I keep getting a no data error on line 48. (or line 13 for the function). This is the error i receive. NOTE: SYSTEM is the server I'm connected to.
ERROR at line 1:
ORA-01403: no data found
ORA-06512: at "SYSTEM.DEPTPROJECT", line 13
ORA-06512: at line 4
ORA-06512: at line 4
these are my function
SET ECHO ON;
SET FEEDBACK ON;
SET LINESIZE 100;
SET PAGESIZE 100;
SET SERVEROUTPUT ON;
CREATE OR REPLACE FUNCTION DEPTPROJECT(departmentNumber IN NUMBER)
RETURN VARCHAR IS
dep# DEPARTMENT.D#%TYPE;
depName DEPARTMENT.DName%TYPE;
counter NUMBER(10,0);
empNumber CHAR(5);
empName VARCHAR2(30);
result VARCHAR2(600);
BEGIN
SELECT D#, DNAME INTO dep#, depName FROM DEPARTMENT WHERE DEPARTMENT.D# = dep#;
result := result || 'Department'|| dep# || '' || depName || chr (10);
FOR i IN(SELECT P# , PTitle , Budget FROM PROJECT WHERE PROJECT.D# = dep# ORDER BY BUDGET DESC)
LOOP
result:= result || chr(9)|| 'Project: ' || i.P# || '' || i.PTitle || i.Budget || chr(10);
FOR j IN(SELECT EMPLOYEE.Name, EMPLOYEE.E# FROM WORKSON INNER JOIN EMPLOYEE ON EMPLOYEE.E# = WORKSON.E# WHERE WORKSON.P# = i.P# ORDER BY EMPLOYEE.NAME ASC )
LOOP
result:= result || chr(10) || j.E# || '' || j.Name || chr(10);
END LOOP;
END LOOP;
RETURN result;
END;
/
BEGIN
FOR x IN(SELECT D# FROM DEPARTMENT)
LOOP
DBMS_OUTPUT.PUT_LINE(DEPTPROJECT(x.D#));
END LOOP;
END;
/
When you are selecting INTO a variable and there are no records returned you should get a NO DATA FOUND error. I believe the correct way to write the above code would be to wrap the SELECT statement with it's own BEGIN/EXCEPTION/END block.
Example:
SET ECHO ON;
SET FEEDBACK ON;
SET LINESIZE 100;
SET PAGESIZE 100;
SET SERVEROUTPUT ON;
CREATE OR REPLACE FUNCTION DEPTPROJECT(departmentNumber IN NUMBER)
RETURN VARCHAR IS
dep# DEPARTMENT.D#%TYPE;
depName DEPARTMENT.DName%TYPE;
counter NUMBER(10,0);
empNumber CHAR(5);
empName VARCHAR2(30);
result VARCHAR2(600);
BEGIN
SELECT D#, DNAME INTO dep#, depName FROM DEPARTMENT WHERE DEPARTMENT.D# = departmentNumber;
result := result || 'Department'|| dep# || '' || depName || chr (10);
FOR i IN(SELECT P# , PTitle , Budget FROM PROJECT WHERE PROJECT.D# = dep# ORDER BY BUDGET DESC)
LOOP
result:= result || chr(9)|| 'Project: ' || i.P# || '' || i.PTitle || i.Budget || chr(10);
FOR j IN(SELECT EMPLOYEE.Name, EMPLOYEE.E# FROM WORKSON INNER JOIN EMPLOYEE ON EMPLOYEE.E# = WORKSON.E# WHERE WORKSON.P# = i.P# ORDER BY EMPLOYEE.NAME ASC )
LOOP
result:= result || chr(10) || j.E# || '' || j.Name || chr(10);
END LOOP;
END LOOP;
EXCEPTION
WHEN NO_DATA_FOUND THEN
result := 'Record Not Found';
RETURN result;
END;
/
BEGIN
FOR x IN(SELECT D# FROM DEPARTMENT)
LOOP
DBMS_OUTPUT.PUT_LINE(DEPTPROJECT(x.D#));
END LOOP;
END;
/
I'd guess that line 13 should use the variable being passed to the function:
SELECT D#, DNAME INTO dep#, depName FROM DEPARTMENT WHERE DEPARTMENT.D# = dep#;
Should be:
SELECT D#, DNAME INTO dep#, depName FROM DEPARTMENT WHERE DEPARTMENT.D# = departmentNumber;
You are using following query to fetch dep# and depName but your where clause is WHERE DEPARTMENT.D# = dep# .
SELECT D#, DNAME INTO dep#, depName FROM DEPARTMENT WHERE DEPARTMENT.D# = dep#;
Here dep# is a variable which you have declared but not initialized.
So dep# is null and comparision to NULL will always lead to false which means your query is returning no record.
As you have used INTO in your query, Oracle is seeking for exactly one record from this query but it is returning no records. Hence, you are facing the issue.
According to your logic either change the WHERE clause to include departmentNumber in comparision or assign proper value to dep# before using it in your query.
Also, make sure to wrap your SELECT .. INTO query in BEGIN..EXCEPTION..END to avoid such a dynamic exception.
Cheers!!

How to take User Input in PL/sql Cursor FOR LOOP and update table based on the user input?

I am taking the emp3 table which is same as emp table.
DECLARE
CURSOR incr_cur IS SELECT * FROM emp3 FOR UPDATE OF sal;
v_job emp3.job%TYPE := '&ENTER_Job';
v_cnt INTEGER;
BEGIN
FOR r_l IN incr_cur LOOP
IF v_job = r_l.job THEN
UPDATE emp3 SET sal = sal + 100 WHERE CURRENT OF incr_cur;
END IF;
END LOOP;
FOR p_l IN incr_cur LOOP
IF v_job = p_l.job THEN
DBMS_OUTPUT.PUT_LINE('The Salary of ' || p_l.ename || ' is: ' || p_l.sal || ' (Incremented).');
ELSE
DBMS_OUTPUT.PUT_LINE('The Salary of ' || p_l.ename || ' is: ' || p_l.sal || ' (Not Incremented).');
END IF;
END LOOP;
END;
After executing the script it will ask for user input.
I gave the INPUT 'CLERK' and the OUTPUT,
But I want output like this,
Just increment a variable after the if condition and then display it at the end.
DECLARE
CURSOR ..
..
v_cnt INTEGER;
BEGIN
..
..
FOR p_l in incr_cur --second for loop
LOOP
IF v_job = p_l.job
v_cnt := v_cnt + 1;
..
END LOOP;
DBMS_OUTPUT.PUT_LINE('The Salary of '||v_cnt||' Employees are Incremented by 100');
END;
DECLARE
CURSOR incr_cur IS SELECT * FROM emp3 FOR UPDATE OF sal;
v_job emp3.job%TYPE := '&ENTER_Job';
v_cnt INTEGER := 0;
BEGIN
FOR r_l IN incr_cur LOOP
IF v_job = r_l.job THEN
UPDATE emp3 SET sal = sal + 100 WHERE CURRENT OF incr_cur;
END IF;
END LOOP;
FOR p_l IN incr_cur LOOP
IF v_job = p_l.job THEN
v_cnt := v_cnt + 1;
DBMS_OUTPUT.PUT_LINE('The Salary of ' || p_l.ename || ' is: ' || p_l.sal || ' (Incremented).');
ELSE
DBMS_OUTPUT.PUT_LINE('The Salary of ' || p_l.ename || ' is: ' || p_l.sal || ' (Not Incremented).');
END IF;
END LOOP;
DBMS_OUTPUT.PUT_LINE('The Salary of '|| v_cnt ||' Employees are Incremented by 100');
END;

cannot reference to cursor instances oracle pl/sql

CREATE OR REPLACE PROCEDURE employee_info_all_in_one
(p_id IN NUMBER)
IS
CURSOR c_city IS
(SELECT l.city
FROM employees e
INNER JOIN departments d
ON (e.department_id = d.department_id)
INNER JOIN locations l
ON (l.location_id = d.location_id)
WHERE e.employee_id = p_id);
CURSOR c_manager IS
(SELECT e1.last_name
FROM employees e1
INNER JOIN
employees e2
ON (e1.employee_id = e2.manager_id)
WHERE e2.employee_id = p_id);
CURSOR c_department_name IS
(SELECT department_name
FROM employees e
INNER JOIN
departments d
ON (e.department_id = d.department_id)
WHERE e.employee_id = p_id);
TYPE EmpRecTyp IS RECORD
( v_annual_sal NUMBER(9,2),
v_monthly_sal NUMBER(9,2),
v_last_name VARCHAR2(10),
v_deptno NUMBER(3),
v_length NUMBER(2),
v_tenure NUMBER(5),
v_job_id VARCHAR2(20),
v_hire_date DATE,
v_city VARCHAR(25),
v_commission_pct NUMBER(2,2),
v_phone_number VARCHAR2(20),
v_manager VARCHAR2(20),
v_comm_calc NUMBER(10,2),
v_email VARCHAR2(10),
v_department VARCHAR2(20),
v_count NUMBER(4)
);
TYPE EmpRecTyp IS REF CURSOR;
emp_c_v EmpRecTyp;
BEGIN
DBMS_OUTPUT.PUT_LINE('Welcome to the summary of an employee based on his unique id');
DBMS_OUTPUT.PUT_LINE('============================================================');
SELECT salary, last_name, department_id,
TRUNC(MONTHS_BETWEEN(SYSDATE,hire_date),0), job_id,
hire_date, commission_pct, phone_number, email
INTO emp_c_v.v_monthly_sal, emp_c_v.v_last_name, emp_c_v.v_deptno,
emp_c_v.v_tenure, emp_c_v.v_job_id, emp_c_v.v_hire_date, emp_c_v.v_commission_pct,
emp_c_v.v_phone_number, emp_c_v.v_email
FROM employees
WHERE employee_id = p_id;
emp_c_v.v_count := SQL%ROWCOUNT;
DBMS_OUTPUT.PUT_LINE(emp_c_v.v_count||' row retrieved...');
DBMS_OUTPUT.PUT_LINE('=============================');
emp_c_v.v_annual_sal := emp_c_v.v_monthly_sal * 12;
emp_c_v.v_length := LENGTH(emp_c_v.v_last_name);
DBMS_OUTPUT.PUT_LINE('Employee:-> ' || emp_c_v.v_last_name || ' ,and his name contains: ' || emp_c_v.v_length ||' chars');
DBMS_OUTPUT.PUT_LINE('=============================');
DBMS_OUTPUT.PUT_LINE(q'[Belong's to department: ]' || emp_c_v.v_deptno);
DBMS_OUTPUT.PUT_LINE('=============================');
IF (emp_c_v.v_monthly_sal < emp_c_v.v_annual_sal)
THEN
DBMS_OUTPUT.PUT_LINE('Has a annual salary of:-> ' || emp_c_v.v_annual_sal);
ELSE
DBMS_OUTPUT.PUT_LINE('Something wrong in the formula!');
END IF;
IF emp_c_v.v_commission_pct IS NULL
THEN
DBMS_OUTPUT.PUT_LINE('No Commission added to the annual salary!');
ELSE
DBMS_OUTPUT.PUT_LINE('Commission percentage to the salary is:-> '|| emp_c_v.v_commission_pct ||'%');
emp_c_v.v_comm_calc := (emp_c_v.v_annual_sal * emp_c_v.v_commission_pct) + emp_c_v.v_annual_sal;
DBMS_OUTPUT.PUT_LINE('And calculated with annual salary is:->' ||v_comm_calc);
END IF;
DBMS_OUTPUT.PUT_LINE('Working for:-> '|| emp_c_v.v_tenure || ' months as '|| emp_c_v.v_job_id);
DBMS_OUTPUT.PUT_LINE('=============================');
DBMS_OUTPUT.PUT_LINE('Started in:-> '|| emp_c_v.v_hire_date);
DBMS_OUTPUT.PUT_LINE('=============================');
DBMS_OUTPUT.PUT_LINE('Phone number:-> '||emp_c_v.v_phone_number);
DBMS_OUTPUT.PUT_LINE('=============================');
DBMS_OUTPUT.PUT_LINE('Email:-> '||emp_c_v.v_email);
DBMS_OUTPUT.PUT_LINE('=============================');
OPEN c_city;
FETCH c_city INTO v_city;
IF c_city%FOUND
THEN
DBMS_OUTPUT.PUT_LINE('Location:-> '||emp_c_v.v_city);
ELSE
DBMS_OUTPUT.PUT_LINE('Employee location unknown');
END IF;
CLOSE c_city;
OPEN c_manager;
FETCH c_manager INTO emp_c_v.v_manager;
IF c_manager%FOUND
THEN
DBMS_OUTPUT.PUT_LINE('Is in the eyes of manager:-> '||emp_c_v.v_manager);
ELSE
DBMS_OUTPUT.PUT_LINE('Slave '||emp_c_v.v_last_name||' is free!');
END IF;
CLOSE c_manager;
OPEN c_department_name;
FETCH c_department_name INTO emp_c_v.v_department;
IF c_department_name%FOUND
THEN
DBMS_OUTPUT.PUT_LINE('Department Name:-> '||emp_c_v.v_department);
ELSE
DBMS_OUTPUT.PUT_LINE('Employee ' ||emp_c_v.v_last_name||' belongs to no department!');
END IF;
DBMS_OUTPUT.PUT_LINE('================================');
DBMS_OUTPUT.PUT_LINE('Checking for employee with id '|| p_id ||'..');
IF (check_sal2(p_id) IS NULL)
THEN
DBMS_OUTPUT.PUT_LINE('The function returned NULL due to exception, therefore employee does not exist!');
ELSIF (check_sal2(p_id))
THEN
DBMS_OUTPUT.PUT_LINE('Employees salary > average of department '||emp_c_v.v_deptno||' where he belongs.');
ELSE
DBMS_OUTPUT.PUT_LINE('Salary < average of department '||emp_c_v.v_deptno||', where he belongs.');
END IF;
DBMS_OUTPUT.PUT_LINE('=====================================================');
END;
Initially i had only simple variables (v_...) declared in this sub-program but i wanted to make use of true cursor variables i.e like-pointers? i suppose. So i modified and defined a type of record and referenced a cursor "...IS REF CURSOR" for the variables or fields inside it. But when i compile this whole thing i get the error of invalid reference to variable "emp_c_v", why? I am still in the learning stages so sorry for talking nonsense. Thanks
A cursor is a pointer to a result set (sort of), not to an individual record. You can't ever modify a result set. In this case the pointer wouldn't actually point to anything as there isn't a result set (from a query) for it to point to.
Your code will almost compile if you drop the ref cursor and make your variable a record type; so instead of:
TYPE EmpCurTyp IS REF CURSOR;
emp_c_v EmpCurTyp;
use:
emp_c_v EmpRecTyp;
You've also missed a couple of places when changing your references:
DBMS_OUTPUT.PUT_LINE('And calculated with annual salary is:->' ||v_comm_calc);
and
FETCH c_city INTO v_city;
... both need the emp_c_v. prefix before v_comm_calc and v_city respectively.
I assume you're using this as a learning exercise, otherwise this could all be done much more simply.

PL/SQL checking if number is Integer

I want to check my employee table if there is any employee who works as an
employee for exactly one year or two years, or three years, ...
The Problem for me now is that i dont know how i can check if a number is
an integer or not.
CREATE OR REPLACE PROCEDURE jubilar
IS
v_cur_date DATE := TO_DATE('03-JAN-2012');
v_cur_year NUMBER;
v_first_name employees.first_name%TYPE;
v_last_name employees.last_name%TYPE;
v_hire_date employees.hire_date%TYPE;
CURSOR c_emp_cursor IS
SELECT first_name, last_name, hire_date FROM employees;
BEGIN
OPEN c_emp_cursor;
LOOP
FETCH c_emp_cursor INTO v_first_name, v_last_name, v_hire_date;
EXIT WHEN c_emp_cursor%NOTFOUND;
v_cur_year := round((v_cur_date - v_hire_date) / 365, 1);
IF v_cur_year ???
DBMS_OUTPUT.PUT_LINE(v_first_name || ' ' || v_last_name || ' ist heute ' || v_cur_year || ' Jahre im Unternehmen tätig.');
END IF;
END LOOP;
CLOSE c_emp_cursor;
END jubilar;
/
At this line
IF v_cur_year ??
I need to check if v_cur_year is an integer or not. Because if it is the employee
works for exactly X year as an employee. And i need to know that.
EDIT:
I tried this:
CREATE OR REPLACE PROCEDURE jubilar
IS
v_cur_date DATE := TO_DATE('03/01/12', 'dd/mm/yy');
v_cur_year NUMBER;
v_cur_year_temp VARCHAR2(100);
v_first_name employees.first_name%TYPE;
v_last_name employees.last_name%TYPE;
v_hire_date employees.hire_date%TYPE;
CURSOR c_emp_cursor IS SELECT first_name, last_name, hire_date FROM employees;
BEGIN
OPEN c_emp_cursor;
LOOP
FETCH c_emp_cursor INTO v_first_name, v_last_name, v_hire_date;
EXIT WHEN c_emp_cursor%NOTFOUND;
v_cur_year := round((v_cur_date - v_hire_date) / 365, 1);
v_cur_year_temp := TO_CHAR(v_cur_year);
IF REGEXP_COUNT(v_cur_year_temp, ',') = 0 THEN
DBMS_OUTPUT.PUT_LINE(v_first_name || ' ' || v_last_name || ' ist heute ' || v_cur_year || ' Jahre im Unternehmen tätig.' || TO_CHAR(v_hire_date));
END IF;
END LOOP;
CLOSE c_emp_cursor;
END jubilar;
/
But it gives me wrong persons with hire_date for example 17/01/2005
I also tried this:
CREATE OR REPLACE PROCEDURE jubilar
IS
v_cur_date DATE := TO_DATE('03/01/12', 'dd/mm/yy');
v_cur_year NUMBER;
v_first_name employees.first_name%TYPE;
v_last_name employees.last_name%TYPE;
v_hire_date employees.hire_date%TYPE;
CURSOR c_emp_cursor IS SELECT first_name, last_name, hire_date FROM employees;
BEGIN
OPEN c_emp_cursor;
LOOP
FETCH c_emp_cursor INTO v_first_name, v_last_name, v_hire_date;
EXIT WHEN c_emp_cursor%NOTFOUND;
v_cur_year := round((v_cur_date - v_hire_date) / 365, 1);
IF trunc(v_cur_year) = v_cur_year THEN
DBMS_OUTPUT.PUT_LINE(v_first_name || ' ' || v_last_name || ' ist heute ' || v_cur_year || ' Jahre im Unternehmen tätig.' || TO_CHAR(v_hire_date));
END IF;
END LOOP;
CLOSE c_emp_cursor;
END jubilar;
/
But it gives me wrong persons with hire_date for example 17/01/2005
This may help you ..
DECLARE
d1 DATE := TO_DATE ('01/12/12', 'mm/dd/yy');
d2 DATE := SYSDATE;
n1 NUMBER;
chk VARCHAR2 (100);
BEGIN
n1 := ROUND ( (d2 - d1) / 365, 1);
DBMS_OUTPUT.put_line (n1);
chk := TO_CHAR (n1);
DBMS_OUTPUT.put_line (chk);
IF REGEXP_COUNT (chk, '.') != 0
THEN
---YOUR LOGIC
END IF;
END;
To check for employees who are employed exactly two years at e.g. January 14th, you can use this:
select *
from employees
where months_between(DATE '2014-01-14', trunc(hire_date)) / 12 = 2;
(I prefer using ANSI date literals over the to_date() function. Works on multiple DBMS and is less typing)
months_between(...) / 12 will result in a fractional value if it's not exactly one year and therefore the comparison with a non-fractional value will fail.
Just change the comparison date (DATE '2014-01-14') and the condition ( =2) to accommodate for other dates or durations.
SQLFiddle example: http://sqlfiddle.com/#!4/27c3d/4
I would do it like this:
with y as
(select trunc(ADD_MONTHS(SYSDATE, -12 * LEVEL)) as the_date, LEVEL as anniversary
from dual
connect by LEVEL <= 6)
select first_name, last_name, hire_date, anniversary, the_date
join y on the_date = hire_date

Resources