can any one help me on this from dumps? [closed] - oracle

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
View Exhibit 1 and examine the structure of the employees table :
desc employees
Name Null Type
-------------------------------------------------
EMPLOYEE_ID NOT NULL NUMBER(6)
FIRST_NAME VARCHAR2(20)
LAST_NAME NOT NULL CHAR(25)
EMAIL NOT NULL VARCHAR2(25)
PHONE_NUMBER VARCHAR2(20)
HIRE_DATE NOT NULL DATE
JOB_ID NOT NULL VARCHAR2(10)
SALARY NUMBER(8,2)
COMMISSION_PCT NUMBER(2,2)
MANAGER_ID NUMBER(6)
DEPARTMENT_ID NUMBER(4)
View Exhibit 2 and examine the code:
CREATE OR REPLACE FUNCTION increase
(emp_num NUMBER) RETURN NUMBER
IS
inc_amt NUMBER;
sal NUMBER;
BEGIN
SELECT salary INTO sal FROM employees WHERE employee_id = emp_num;
inc_amt := sal*10;
RETURN inc_amt;
END increase;
/
CREATE OR REPLACE PROCEDURE calc_sal IS
emp_num NUMBER(7) := 120;
amt NUMBER :=0;
PROCEDURE raise_salary
(emp_id NUMBER)
IS
BEGIN
amt := increase(emp_num);
UPDATE employees SET salary = salary+amt WHERE employee_id=emp_id;
END raise_salary;
BEGIN
raise_salary(emp_num);
END calc_sal;
/
What is the outcome when the code is executed?
A.Both blocks compile and execute successfully when called.
B.Both blocks compile successfully but the CALC_SAL procedure gives an error on execution.
C.The CALC_SAL procedure gives an error on compilation because the amt variable should be declared in the RAISE_SALARY procedure.
D.The CALC_SAL procedure gives an error on compilation because the RAISE_SALARY procedure cannot call the stand-alone increase function.
I chose B because i compiled function and procedure and its compile sucssfully but I've tried to invoke(execute) procedure calc_sal and its give me error.
can you Explain to me what's the reason for this error and is my answer correct or false? because in dumps the answer was A
i invoke the procedure with this code :
begin
calc_sal;
end;
and the error i get :
Error report:
ORA-01438: value larger than specified precision allowed for this column
ORA-06512: at "HR.CALC_SAL", line 7
ORA-06512: at "HR.CALC_SAL", line 11
ORA-06512: at line 2
01438. 00000 - "value larger than specified precision allowed for this column"
*Cause: When inserting or updating records, a numeric value was entered
that exceeded the precision defined for the column.
*Action: Enter a value that complies with the numeric column's precision,
or use the MODIFY option with the ALTER TABLE command to expand
the precision.
and employee_id column there is 107 employee Their numbers from 100 to 206.

As Bob's comments state correctly, the code does work correctly, so the quiz answer A is defensible.
You're saying B because you get this error:
ORA-01438: value larger than specified precision allowed for this
column
This is a data problem. The function CALC_SAL() will succeed providing SAL + SAL*10 is <= 999999.99, the maximum value which will fit into a column defined as NUMBER(8,2). So either your table is not defined the way you think it is, or the employee's original salary ishigher then you think it is.

Related

Invalid reference in Oracle database

I am using Oracle database 19c for a project. I have created the alumni table as shown below:
CREATE TABLE Alumni (
ID NUMBER GENERATED ALWAYS AS IDENTITY ,
First_Name varchar2(20) NOT NULL,
Last_Name varchar2(20) NOT NULL,
Gender char(1) NOT NULL,
CHECK (Gender IN('M','F')),
Graduation_Year number NOT NULL,
check (Graduation_Year>=1980 AND Graduation_Year<=2020),
Degree_Course varchar2(255) not null,
Award_Nominated CHAR(1),
CHECK (Award_Nominated IN('Y','N')),
Award_Won CHAR(1),
CHECK (Award_Won IN('Y','N')),
Phone_Number VARCHAR2(15) not null,
Email_Address VARCHAR2(255) not null,
CONSTRAINT ALUM_PK PRIMARY KEY(ID)
);
Then I tried to define a function that will allow me to search by First or last name in a section of the table, shown below:
CREATE OR REPLACE TYPE alum_row_type is object(ID number,First_name varchar2(20),Last_Name
varchar2(20),Phone_number varchar2(15),Email_Address varchar2(255));
CREATE OR REPLACE TYPE t_alum_row_type as table of alum_row_type;
CREATE OR REPLACE FUNCTION Name_Search RETURN t_alum_row_type
IS
L_alum_row t_alum_row_type := t_alum_row_type();
X integer :=0;
BEGIN
FOR R IN (SELECT ID,First_Name, Last_Name,Phone_Number,Email_Address FROM alumni)
LOOP
L_alum_row.extend;
X:=X+1;
L_alum_row(X):= alum_row_type(X.ID,X.First_Name, X.Last_Name, X.Phone_Number, X.Email_Address);
END LOOP;
RETURN L_alum_row;
END;
After running this, I get this error:
LINE/COL ERROR
--------- -------------------------------------------------------------
10/9 PL/SQL: Statement ignored
10/41 PLS-00487: Invalid reference to variable 'X'
Errors: check compiler log
How can I resolve this issue. I am relatively new to PL\SQL, so please explain it simply.
This is not possible
L_alum_row(X):= alum_row_type(X.ID,X.First_Name, X.Last_Name, X.Phone_Number, X.Email_Address);
You are referencing X.ID, X.Phone_Number etc. . . but declared X as an integer. If you are trying to access the current loop record, try with
L_alum_row(X):= alum_row_type(R.ID,R.First_Name, R.Last_Name, R.Phone_Number, R.Email_Address);
As you declared the loop iterator as R for your cursor
FOR R IN (SELECT ID,First_Name, Last_Name,Phone_Number,Email_Address FROM alumni)
You can eliminate the loop altogether by using BULK COLLECT. Thereby reducing the function to basically a single select statement.
function name_search
return t_alum_row_type
is
l_alum_row t_alum_row_type := t_alum_row_type();
begin
select alum_row_type(id,first_name, last_name,phone_number,email_address)
bulk collect
into l_alum_row
from alumni;
return l_alum_row;
end;

is there a better way to write avg procedure?

I wrote this procedure but it keeps showing "WARNING: Procedure creates with compilation errors" and I don't know why, here's the table I created
create table EnrolledInClasses (
St_Id char(9) primary key,
C_Id char(6),
GradeN Number(2),
constraint FK_StId Foreign key (St_Id) references Student (St_Id),
constraint FK_CoID Foreign key (C_Id) references course (C_Id)
constraint CheckGrade check (Grade>-1)
);
and here's the procedure:
CREATE OR REPLACE PROCEDURE Avg_grades
IS
avg_grades NUMBER := 0;
BEGIN
SELECT AVG (Grade)
INTO avg_grades
FROM EnrolledInClasses
dbms_output.put_line('The average of grades is :'||avg_grades);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.put_line ('No Data Is Found..');
END;
/
Since you are using PL/SQL Developer, make sure you are writing your code in a Procedure window. Then the compilation error will be clearly highlighted. In this case it's the missing semicolon at the end of the select into statement.
Or in SQL*Plus you can use show errors:
Warning: Procedure created with compilation errors.
SQL> show errors
Errors for PROCEDURE AVG_GRADES:
LINE/COL ERROR
-------- -----------------------------------------------------------------
6/1 PL/SQL: SQL Statement ignored
9/12 PL/SQL: ORA-00933: SQL command not properly ended
or the full syntax, show errors procedure avg_grades.
Or you can directly query dba|all|cdb|user_errors:
select * from user_errors e
where e.name = 'AVG_GRADES'
and e.attribute = 'ERROR';
Also, the table definition could be written more succinctly as:
create table enrolledinclasses
( st_id primary key constraint fk_stid references student(st_id)
, c_id constraint fk_coid references course (c_id)
, grade number(2) constraint checkgrade check (grade>-1)
);
student.st_id and course.c_id should probably be numbers or integers, or at least varchar2, and not char.
It is good practice to use code indentation to align code into blocks showing their dependency structure. That would give something like this:
create or replace procedure avg_grades as
avg_grades number := 0;
begin
select avg(grade) into avg_grades
from enrolledinclasses;
dbms_output.put_line('The average grade is: ' || avg_grades);
exception
when no_data_found then
dbms_output.put_line('No Data Is Found.');
end;
You might want to round() that average before displaying it, as by default it could be displayed with a lot more decimal places than you might want.
Also, it's not possible to get a no_data_found exception from a single-group aggregate query like this. If there are no rows in the table you will get a null value as the result, not an error.

Call Procedure Unexpected error

I'm coding a DML package, and I would like create a simple procedure within package to updated commission_pct value. I don't receive any syntax error when I execute the package body but when I call the procedure I receive a unexpected error. Thanks.
PROCEDURE update_commission_pct(
p_empid employees.employee_id%TYPE,
p_new_comm employees.commission_pct%TYPE
)
IS
rec_confirm employees%ROWTYPE;
v_valid_empid BOOLEAN;
BEGIN
-- Simple boolean function to check employee existence
v_valid_empid := dml_employees_pkg.check_employee_id(p_empid);
IF
v_valid_empid = TRUE
AND LENGTH(p_new_comm) <=5 THEN
UPDATE employees
SET commission_pct = p_new_comm
WHERE employee_id = p_empid
RETURNING employee_id, commission_pct
INTO rec_confirm.employee_id, rec_confirm.commission_pct;
DBMS_OUTPUT.PUT_LINE('Comission updated successfully.');
DBMS_OUTPUT.PUT_LINE('Employee ID number ' ||
rec_confirm.employee_id || ' new comm is' ||
rec_confirm.commission_pct);
ELSE
RAISE_APPLICATION_ERROR(-20042, 'Employee ID ' ||
p_empid || ' Employee doesn't exist.');
END IF;
END update_commission_pct;
Call procedure in a simple PL/SQL block:
SET SERVEROUTPUT ON
BEGIN
dml_employees_pkg.update_commission_pct(550, 10);
END;
Oracle ERROR:
Informe de error -
ORA-01438: value larger than specified precision allowed for this column
ORA-06512: at "HR.DML_EMPLOYEES_PKG", line 118
ORA-06512: at line 2
01438. 00000 - "value larger than specified precision allowed for this column"
*Cause: When inserting or updating records, a numeric value was entered
that exceeded the precision defined for the column.
*Action: Enter a value that complies with the numeric column's precision,
or use the MODIFY option with the ALTER TABLE command to expand
the precision.
The commission_pct column in the employees table (in the default HR schema) is defined as number(2, 2).
The meaning of the precision and scale is explained inthe documentation, but essentially here it means the column can only accept values from 0.00 to 0.99. You're trying to insert 10, which - as the error says - exceeds the allowed precision.
If you want to store 10% you can either pass 0.1 as the second argument in your procedure call; or stick with passing 10 but then divide by 100 as part of the update statement:
SET commission_pct = p_new_comm/100
You may want to validate the passed value in some way other than checking its length. And at the moment if the LENGTH(p_new_comm) <=5 check fails, the raised exception doesn't make that clear - it only refers to the employee ID which may actually be valid. The length check doesn't really sense anyway though.

PL/SQL: Executing Procedure

I have created a procedure. It is giving an error ( ORA-01422: exact fetch returns more than requested number of rows). As for a specific department_id there are more than one employees. But how to solve this problem ?
Create Procedure PP1
(ID in number, Percent in number, Sal out number, increase_sal out number) IS
Begin
Select salary, salary *(1+percent/100) into sal, increase_sal
From employees
where department_id= id;
DBMS_OUTPUT.PUT_LINE (sal || ' ' || increase_sal);
END;
/
Variable a number
Variable b number
Exec PP1 (100, 10, :a, :b)
Print a b
Thanks,
Kuntal Roy
My guess is that you want something like (untested)
CREATE TYPE num_tbl IS TABLE OF NUMBER;
CREATE PROCEDURE raise_dept_salaries( p_dept_id IN employees.department_id%type,
p_raise_pct IN NUMBER,
p_old_sals OUT num_tbl,
p_new_sals OUT num_tbl )
AS
BEGIN
SELECT salary
BULK COLLECT INTO p_old_sals
FROM employees
WHERE department_id = p_dept_id;
UPDATE employees
SET salary = salary * (1 + p_raise_pct)
WHERE department_id = p_dept_id
RETURNING salary
BULK COLLECT INTO p_new_sals;
END;
Now, splitting things up this way does introduce the possibility that some other session will modify the data between your first SELECT and your UPDATE so this isn't really safe to use in a multi-user environment. Of course, you really wouldn't want to return both the old and the new salaries in the first place since you already know that they are going to be directly related to each other. If you only returned the collection of new salaries, then you would only need a single UPDATE statement and you wouldn't have the race condition.

Inconsistent datatypes in Oracle

i have the following function:
create or replace
FUNCTION "MXUPGKEYVAL"(tbname varchar2,colname varchar2) return number is
val number;
BEGIN
EXECUTE IMMEDIATE
'select sum(length('||colname||')) from '||tbname into val;
return val;
END;
and the following update:
update ANINTEGDATA set val1=to_char(nvl(MXUPGKEYVAL(MX5T,MX5C),0)) where type=1;
when i execute the update i get:
ORA-00932: inconsistent datatypes: expected NUMBER got LONG
ORA-06512: at "MAXIMO.MXUPGKEYVAL", line 6
ORA-06512: at line 2
any idea why that happens?
Regards,
Radu.
Later edit:
table ANINTEGDATA is:
create table ANINTEGDATA
(
MX5T VARCHAR2(50),
MX5C VARCHAR2(50),
MX6T VARCHAR2(50),
MX6C VARCHAR2(50),
TYPE NUMBER,
VAL1 VARCHAR2(200),
VAL2 VARCHAR2(200)
);
Your function works.
SQL> select mxupgkeyval('EMP', 'SAL') from dual
2 /
MXUPGKEYVAL('EMP','SAL')
------------------------
78
SQL>
Furthermore it works in your update statement.
SQL> update ANINTEGDATA set val1=to_char(nvl(MXUPGKEYVAL(MX5T,MX5C),0)) where type=1;
1 row updated.
SQL>
Where it doesn't work is when the column in question has the LONG datatype. The error message isn't as clear as it could be but is clear enough.
SQL> alter table t34 add long_col long;
Table altered.
SQL> select mxupgkeyval('T34', 'LONG_COL') from dual
2 /
select mxupgkeyval('T34', 'LONG_COL') from dual
*
ERROR at line 1:
ORA-00932: inconsistent datatypes: expected NUMBER got LONG
ORA-06512: at "APC.MXUPGKEYVAL", line 6
SQL>
This is just another reason why LONG is Teh Suck! and should have been done away with a long time ago. As you're doing a data migration exercise now would be a good time to consider moving to the oh-so flexible CLOB data type.
Either way you need a convention to indicate that the target column contains a shedload of data.
If you really need to know the precise extent of the LONG data volumes I suggest you unload the LONGs to CLOB columns and sum those instead.

Resources