How to write SQL statement when use Method in object relational database

孤街浪徒 提交于 2020-01-06 06:43:05

问题


I am trying to create a small database of bank employees using method in oracle 11g, so at the end I can fetch values of those employees who are entitle for the awards at the end of the year:

Gold medals for employees who have been working at the bank for more than 12 years and supervised more than 6 staff; silver medals for employees who have been working at the bank for more than 8 years and supervised more than 3 staff; bronze medals for employees who have been working at the bank for more than 4 years, displaying their names and Medal awarded (only displaying those who have been awarded).

So here what I am doing

create type EmployeeName as object(
title varchar2(10),
firstName varchar2(20),
surname varchar2(20))
not final
/
create or replace type employeeaward as object(
empID integer,
eName EmployeeName,
number_staff_supervised int,
working_years int,
MEMBER FUNCTION award_given RETURN STRING,
MEMBER FUNCTION number_fraction (N real) RETURN real
);
/
CREATE OR REPLACE TYPE BODY employeeaward AS
MEMBER FUNCTION award_given RETURN STRING IS
BEGIN
IF self.working_years > 12 THEN
RETURN 'gold medal';
ELSIF self.working_years > 8 THEN
RETURN 'silver medal';
ELSIF self.working_years > 4 THEN
RETURN 'bronze medals';
END IF;
END award_given;
MEMBER FUNCTION number_fraction(N real) RETURN real IS
num real;
BEGIN
num :=(self.number_staff_supervised);
return num;
END number_fraction;
END;

and then create a table employeeawardtable of employeeaward like this:

create table employeeawardtable of employeeaward;
/

Then insert some values in the table

insert into employeeawardtable values('2001',EmployeeName('Mr','Rohit','Sharma'),'12','18');
/
insert into employeeawardtable values('2002',EmployeeName('Mr','Andrew','Darson'),'9','7');
/
insert into employeeawardtable values('2003',EmployeeName('Mrs','Sarah','Barlow'),'5','4');
/
insert into employeeawardtable values('2004',EmployeeName('Mr','Ram','Gopal'),'11','9');
/

**This is the SQL query, I am struggling to fetch data. **

select e.ename.firstname, e.award_given(),e.number_fraction(15)
from employeeawardtable e
where e.number_fraction() > 8; 

Thanks.


回答1:


Following are the issues with your code.

  1. The number_fraction member function is invoked without a parameter in the predicate. This will throw an error, "ORA-06553: PLS-306: wrong number or types of arguments in call to 'NUMBER_FRACTION'"

Below is SQL for your requirement.

select e.ename.firstname, e.award_given()
from employeeawardtable e
where e.award_given() = 'gold medal' and e.number_staff_supervised > 6
or e.award_given() = 'silver medal' and e.number_staff_supervised > 3
or e.award_given() = 'bronze medal';
  1. The member_function award_given is not returning a default value. This will throw an error. "ORA-06503: PL/SQL: Function returned without value". Fix the type body as below.

    CREATE OR REPLACE TYPE BODY employeeaward AS MEMBER FUNCTION award_given RETURN STRING IS BEGIN IF self.working_years > 12 THEN RETURN 'gold medal'; ELSIF self.working_years > 8 THEN RETURN 'silver medal'; ELSIF self.working_years > 4 THEN RETURN 'bronze medals'; ELSE--newly added RETURN 'no medal';--newly added END IF; END award_given; MEMBER FUNCTION number_fraction(N real) RETURN real IS num real; BEGIN num :=(self.number_staff_supervised); return num; END number_fraction; END;

Now issue the SQL. Output:

ENAME.FIRSTNAME E.AWARD_GIVEN()
Rohit           gold medal
Ram             silver medal


来源:https://stackoverflow.com/questions/49374140/how-to-write-sql-statement-when-use-method-in-object-relational-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!