开发者

How to call on a object member procedure a nested table attribute?

Have this objects and table types:

CREATE TYPE Person_typ AS OBJECT
( 
    name CHAR(20),
    ssn CHAR(12),
    address VARCHAR2(100)
);

CREATE TYPE Person_nt IS TABLE OF Person_typ;

CREATE TYPE dept_typ AS OBJECT
( 
    mgr Person_typ,
    emps Person_nt,
    MEMBER PROCEDURE getEmp(name IN  CHAR(20)),
);

CREATE TABLE dept OF dept_typ;

How i can get the employer with the function getEm开发者_StackOverflow中文版p and argument name ?

CREATE TYPE BODY dept_typ AS 
   MEMBER  PROCEDURE getEmp(name IN CHAR(20)),
   BEGIN
      ????? What i put where ????
   END;
END;

My problem is that i can't make self.emps like I can do with self.mgr ... and i don't know why....

Thanks, Joao


My guess is that you want something like this

SQL> ed
Wrote file afiedt.buf

  1  CREATE OR REPLACE TYPE dept_typ AS OBJECT
  2  (
  3      mgr Person_typ,
  4      emps Person_nt,
  5      MEMBER FUNCTION getEmp(p_name IN  VARCHAR2)
  6               RETURN person_typ
  7* );
SQL> /

Type created.

SQL> ed
Wrote file afiedt.buf

  1   CREATE OR REPLACE TYPE BODY dept_typ AS
  2      MEMBER FUNCTION getEmp(p_name IN VARCHAR2)
  3               RETURN person_typ
  4      AS
  5      BEGIN
  6         for i in 1 .. self.emps.count()
  7         loop
  8           if( self.emps(i).name = p_name )
  9           then
 10             return self.emps(i);
 11           end if;
 12         end loop;
 13      END;
 14*  END;
SQL> /

Type body created.
  1. If you want getEmp to return something, it should be a function. You could also, I suppose, define it as a procedure with an OUT parameter but that's generally not what you want.
  2. Input and output parameters do not have lengths. You can't declare a parameter as a CHAR(20). You could declare it as a CHAR or as a VARCHAR2 with no length. But there are essentially no cases where a CHAR would be preferred over a VARCHAR2 and employee names are definitely not one of the cases where it's even close.
  3. There is no comma after the declaration of the member function or procedure in either the type spec or the type body. You also need an IS or an AS in the definition of the member function or procedure in the type body.
  4. Since PERSON_NT is a collection, you need to iterate over the collection. Note here that I'm assuming that the collection is dense in this code. You could use the FIRST and NEXT methods on the collection to loop over the elements in a sparse collection as well.

If you really want to select from the collection, you'd need to use the TABLE operator. This approach, however, is less efficient and generally more cumbersome than simply iterating over the collection.

SQL> ed
Wrote file afiedt.buf

  1   CREATE OR REPLACE TYPE BODY dept_typ AS
  2      MEMBER FUNCTION getEmp(p_name IN VARCHAR2)
  3               RETURN person_typ
  4      AS
  5      BEGIN
  6         for i in (select * from table(self.emps))
  7         loop
  8           if( i.name = p_name )
  9           then
 10             return new person_typ( i.name, i.ssn, i.address );
 11           end if;
 12         end loop;
 13      END;
 14*  END;
SQL> /

Type body created.

If you want to add elements to the collection (assuming the collection has previously been initialized)

SQL> ed
Wrote file afiedt.buf

  1  CREATE OR REPLACE TYPE dept_typ AS OBJECT
  2  (
  3      mgr Person_typ,
  4      emps Person_nt,
  5      MEMBER FUNCTION getEmp(p_name IN  VARCHAR2)
  6               RETURN person_typ,
  7      MEMBER PROCEDURE addEmp( p_person IN person_typ )
  8* );
SQL> /

Type created.

SQL> ed
Wrote file afiedt.buf

  1  CREATE OR REPLACE TYPE BODY dept_typ AS
  2     MEMBER FUNCTION getEmp(p_name IN VARCHAR2)
  3              RETURN person_typ
  4     AS
  5     BEGIN
  6        for i in 1 .. self.emps.count()
  7        loop
  8          if( self.emps(i).name = p_name )
  9          then
 10            return self.emps(i);
 11          end if;
 12        end loop;
 13     END;
 14    MEMBER PROCEDURE addEmp(p_person IN person_typ)
 15    AS
 16    BEGIN
 17      self.emps.extend();
 18      self.emps(self.emps.count) := p_person;
 19    END;
 20* END;
SQL> /

Type body created.


I suppose you should just select from the emps table:


member function getEmp (emp_name in  char(20)) return Person_typ IS
  declare
    res Person_typ;
  begin
    select value (e) into res from self.emps e where e.name = emp_name;
    return Person_typ;
  end;

there's more

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜