PL/SQL checking if number is Integer - oracle

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

Related

Control structures (IF/ELSE)

I try to create plsql block but it doesnt work. Oracle says that i have error at line 2. I think it could be because substitution variable is not placed right !?
DECLARE
V_ENAME EMPLOYEES.LAST_NAME%TYPE := '&LNAME';
V_SAL EMPLOYEES.SALARY%TYPE;
BEGIN
SELECT LAST_NAME, SALARY
INTO V_ENAME, V_SAL
FROM employees WHERE LAST_NAME = V_ENAME;
IF V_SAL < 3000 THEN
v_sal := v_sal + 500;
DBMS_OUTPUT.PUT_LINE (v_ename || 'have increasement ');
ELSIF V_SAL > 3000 THEN
DBMS_OUTPUT.PUT_LINE (v_ename || 'do not have increasement ');
ELSE
DBMS_OUTPUT.PUT_LINE ('error');
END IF;
END;
Since there can be many employees with the last name passed by the user, you may use an implicit CURSOR FOR LOOP. Also, add first_name in the output for clarity.
DECLARE
v_ename employees.last_name%TYPE := '&LNAME';
v_sal employees.salary%TYPE;
BEGIN
for rec IN ( SELECT first_name,last_name,salary
FROM employees WHERE last_name = v_ename
)
LOOP
IF rec.salary < 3000 THEN
v_sal := rec.salary + 500;
dbms_output.put_line (rec.first_name||' '||rec.last_name ||
' has an increase');
ELSIF rec.salary > 3000 THEN
dbms_output.put_line (rec.first_name||' '||rec.last_name ||
' does not have an increase ');
ELSE dbms_output.put_line ('error');
END IF;
END LOOP;
END;
/
Execution
..
old:DECLARE
v_ename employees.last_name%TYPE := '&LNAME';
..
new:DECLARE
v_ename employees.last_name%TYPE := 'Grant';
..
Douglas Grant has an increase
Kimberely Grant does not have an increase
PL/SQL procedure successfully completed.
Not sure if it's your problem but there is really no need to select last_name into v_ename when you are already ensuring last_name is equal to v_ename in the where clause.

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;

Date format in pl sql procedure pass into a string

I am having trouble to pass the right date format to my procedure. I did search on the forum but did not find a solution. When I extract the value in my string I get the following result
select *
from (select a.col1,
a.col2,
a.col3,
dense_rank() over(order by a.col2) as TheRank,
round((count(*) over(order by a.col2)), 1) as UniqueRank
from abt_t a
where a.col4 >= to_date(05 - JAN - 98, 'YYYY-MM-DD')
and a.col4 <= 05 - JAN - 98
and (a.col2 <= '15:59' and a.col2 >= '09:30')
order by 1 asc, 2)
where TheRank = 390
and UniqueRank = 05 - JAN - 98
the value 05 - JAN - 98 is pretty different then the one I have initiated p_start_dt date:= to_date('1998-01-05', 'YYYY-MM-DD');
I have tried
Here is my procedure :
create or replace procedure GET_VOL_OPTION_test is
BEGIN
DECLARE
-- Local variables here
w_sqlcode number := 0;
w_sqlerrm char(500) := '';
query_str VARCHAR2(2000);
TYPE cur_typ IS ref CURSOR;
V_TABLE varchar2(200) :=NULL;
c cur_typ;
inv_num NUMBER;
exp_dt date;
Curr_dt date;
inv_amt varchar2(5);
p_start_dt date:= to_date('1998-01-05', 'YYYY-MM-DD');
CURSOR CUR_OVRDDEF_PARAM_EN IS
select tname from OPT_TBL_NM_2015_T order by tname;
BEGIN
FOR XX IN CUR_OVRDDEF_PARAM_EN LOOP
BEGIN
----
query_str := 'select * from (select a.col1, a.col2, a.col3,' ||
'dense_rank() over (order by a.col2) as TheRank,' ||
'round((count(*) over (order by a.col2)),1) as UniqueRank ' ||
/*'from '|| p_stock || ' a' ||*/
'from abc_t a' ||
' where a.col4 >= to_date(' || to_char(p_start_dt) || ', ''YYYY-MM-DD'')' || ' and ' || 'a.col4 <= ' || p_start_dt ||
' and (a.col2 <= ''15:59'' and a.col2 >= ''09:30'')' ||
'order by 1 asc,2)' ||
'where TheRank = 390 and UniqueRank = ' || p_start_dt;
OPEN c FOR query_str ;
LOOP
FETCH c INTO inv_num, exp_dt, curr_dt, inv_amt;
EXIT WHEN c%NOTFOUND;
-- process row here
END LOOP;
/* EXCEPTION
WHEN OTHERS THEN NULL;
END;*/
EXCEPTION
WHEN OTHERS THEN
w_sqlcode := SQLCODE;
w_sqlerrm := SQLERRM;
END;
END LOOP;
COMMIT;
END;
END;
I am using oracle 12. If you need more information, please don't hesitate.
Regards,
Christian
The problem is that you are calling to_char(p_start_dt) without a format mask (or, later, using string concatenation on the dates) and Oracle will convert the date to a string using the NLS_DATE_FORMAT session parameter. As you have found, this is probably set to DD-MON-YY and is causing you issues. If you are going to convert dates to strings and embed it into the dynamic SQL then you need to specify the format mask for the conversion (i.e. TO_CHAR( p_start_date, 'YYYY-MM-DD' )); however, a better solution would be to:
Use bind variables in your dynamic SQL and then just pass the date in when you open the cursor:
DECLARE
-- Local variables here
w_sqlcode number := 0;
w_sqlerrm char(500) := '';
query_str VARCHAR2(2000)
:= 'select * from (select a.col1, a.col2, a.col3,' ||
'dense_rank() over (order by a.col2) as TheRank,' ||
'count(*) over (order by a.col2) as UniqueRank' ||
' from abc_t a' ||
' where a.col4 BETWEEN :start_dt AND :start_dt' ||
' and (a.col2 <= ''15:59'' and a.col2 >= ''09:30'')' ||
'order by 1 asc,2)' ||
'where TheRank = 390 and UniqueRank = :start_dt';
c SYS_REFCURSOR;
inv_num NUMBER;
exp_dt DATE;
Curr_dt DATE;
inv_amt VARCHAR2(5);
p_start_dt DATE := DATE '1998-01-05';
CURSOR CUR_OVRDDEF_PARAM_EN IS
select tname from OPT_TBL_NM_2015_T order by tname;
BEGIN
FOR XX IN CUR_OVRDDEF_PARAM_EN LOOP
BEGIN
OPEN c FOR query_str USING p_start_dt;
LOOP
FETCH c INTO inv_num, exp_dt, curr_dt, inv_amt;
EXIT WHEN c%NOTFOUND;
-- process row here
END LOOP;
EXCEPTION
WHEN OTHERS THEN
w_sqlcode := SQLCODE;
w_sqlerrm := SQLERRM;
END;
END LOOP;
COMMIT;
END;
/
Also, why are you comparing UniqueRank to a date value?
This is the line where you create the date comparision:
' where a.col4 >= to_date(' || to_char(p_start_dt) || ', ''YYYY-MM-DD'')' || ' and ' || 'a.col4 <= ' || p_start_dt ||
and you get
where a.col4 >= to_date(05 - JAN - 98, 'YYYY-MM-DD') and a.col4 <= 05 - JAN - 98
So obviously your standard date format is 'DD - MON - RR'. Why do you even rely on a setting? You should specify the format in to_char, such as:
to_char(p_start_dt, 'YYYY-MM-DD')
rather than using to_char(p_start_dt) or p_start_dt (which is both the same; a date converted to string using the default setting).
You are also missing quotes. The correct syntax would have been:
' where a.col4 >= to_date(''' || to_char(p_start_dt, 'YYYY-MM-DD') || ''', ''YYYY-MM-DD'') and ' ||
'a.col4 <= to_date(''' || to_char(p_start_dt, 'YYYY-MM-DD') || ''', ''YYYY-MM-DD'')' ||
or simpler with ISO date literals:
' where a.col4 >= date ''' || to_char(p_start_dt, 'YYYY-MM-DD') || ''' and ' ||
'a.col4 <= date ''' || to_char(p_start_dt, 'YYYY-MM-DD') || '''' ||
which is of course the same as
' where a.col4 = date ''' || to_char(p_start_dt, 'YYYY-MM-DD') || '''' ||

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

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...

Encountered the symbol "WHILE" when expecting one of the following: . ( * # % & - + / at loop mod remainder rem

Hi i'm a begginer in pl/sql under oracle. i'tried to write this pl/SQL programm but i had this error:
Encountered the symbol "WHILE" when expecting one of the following: . ( * # % & - + / at loop mod remainder rem ..'
SET SERVEROUTPUT ON
DECLARE
CURSOR EMP_CURSOR IS SELECT EMPLOYEE_NAME, SALARY, DATE_HIRE FROM EMPLOYEES WHERE DEPARTMENT_ID=1;
BEGIN
FOR EMP_REC IN EMP_CURSOR
WHILE EMP_REC%FOUND
LOOP
IF EMP_REC.SALARY > 10000 AND EMP_REC.DATE_HIRE< DATE '2000-01-01'
THEN
DBMS_OUTPUT.PUT_LINE (EMP_REC.EMPLOYEE_NAME || ' earns ' || EMP_REC.SALARY || 'and joined the organization on ' || EMP_REC.DATE_HIRE);
END IF;
END LOOP;
END LOOP;
END;
/
SET SERVEROUTPUT OFF
Try this:
DECLARE
CURSOR EMP_CURSOR IS SELECT EMPLOYEE_NAME, SALARY, DATE_HIRE FROM EMPLOYEES WHERE DEPARTMENT_ID=1;
BEGIN
FOR EMP_REC IN EMP_CURSOR
LOOP
IF EMP_REC.SALARY > 10000 AND EMP_REC.DATE_HIRE< DATE '2000-01-01'
THEN
DBMS_OUTPUT.PUT_LINE (EMP_REC.EMPLOYEE_NAME || ' earns ' || EMP_REC.SALARY || 'and joined the organization on ' || EMP_REC.DATE_HIRE);
END IF;
END LOOP;
END;

Resources