创建一个oracle函数

时间:2013-01-05 12:33:39

标签: sql plsql sqlplus

以下代码使用oracle sql * plus创建一个带有get_age函数的对象表。

Create Type C_type as Object(
  Cname varchar(30),
  dob  Date,
  classification Number,
  sample_clip Blob
)
/

ALTER TYPE C_type
  ADD MEMBER FUNCTION get_age RETURN INTEGER;


CREATE OR REPLACE TYPE BODY C_type AS
MEMBER FUNCTION get_age RETURN INTEGER IS
currenttime_age integer; 
BEGIN
  currenttime_age := (SYSDATE - dob)/365.25; 
  RETURN currenttime_age;   
END get_age;
END;
/

我的目的是添加另一个返回角色的函数

  • 分类为“短”(如果低于5),“中等”(介于5-45之间)和“长”(超过45)

1 个答案:

答案 0 :(得分:1)

试试这个。我的坏。

DROP TYPE c_type
/

CREATE TYPE C_type AS OBJECT
(
   Cname VARCHAR (30),
   dob DATE,
   classification NUMBER,
   sample_clip BLOB,
   MEMBER FUNCTION get_age
      RETURN INTEGER,
   MEMBER FUNCTION get_class
      RETURN VARCHAR2
)
/


CREATE OR REPLACE TYPE BODY C_type
AS
   MEMBER FUNCTION get_age
      RETURN INTEGER
   IS
      currenttime_age   INTEGER;
   BEGIN
      currenttime_age := (SYSDATE - dob) / 365.25;
      RETURN currenttime_age;
   END get_age;


   MEMBER FUNCTION get_class
      RETURN VARCHAR2
   IS
      result   VARCHAR2 (10);
   BEGIN
      result :=
         CASE
            WHEN classification < 5 THEN 'short'
            WHEN classification BETWEEN 5 AND 45 THEN 'medium'
            WHEN classification > 45 THEN 'long'
         END;

      RETURN result;
   END get_class;
END;
/