尝试在PL / SQL中创建一个类型主体,但不断收到错误

时间:2014-10-25 05:46:45

标签: sql oracle plsql sqlplus

我正在尝试为我的类型components_t创建一个类型正文。这是该类型的代码:

create type components_t under product_t (compPrice number(5,2), 
compDesc varchar(15), compYear number(4),
member function changeDesc return varchar);
/

这是我目前为类型体提供的代码:

create type body components_t as member function changeDesc
return varchar
is
begin
if self.compYear < 2005 then
return self.compDesc||' Discontinued';
end if;
end;
/

我正在尝试使用类型体,如果组件的年份小于2005,则将' Discontinued'连接到描述的末尾。我尝试输入此内容并收到此错误:

SQL> show errors
Errors for TYPE BODY COMPONENTS_T:

LINE/COL ERROR
-------- -----------------------------------------------------------------
8/4      PLS-00103: Encountered the symbol "end-of-file" when expecting
     one of the following:
     end not pragma final instantiable order overriding static
     member constructor map

首先,我想知道这是否在PL / SQL中是可能的。其次,有谁知道我能做些什么才能使语法正确?

1 个答案:

答案 0 :(得分:0)

通过一些格式化,我让你的代码更具可读性,现在你可以理解你错过了什么。

身体可以有许多功能和程序。每个都有BEGIN-END ..最后,BODY本身需要END

然后,对于您的第一个问题,您可以在此处使用CONSTRUCTOR功能。

create type components_t under product_t 
(
  compPrice number(5,2), 
  compDesc varchar(15),
  compYear number(4),
  constructor function changeDesc return self as result
);

/
create type body components_t as 
    /* First function */
    construction function changeDesc(compPrice number, compDesc varchar,compYear )
      return self as result
    is
    begin
       self.compYear := compYear;
       self.compDesc := compDesc;

       if self.compYear < 2005 then
         self.compDesc := self.compDesc||' Discontinued';
       end if;
       return; /* it has to return by default*/
    end changeDesc; /* your member function ends here */

    /* Next function or procedure */

end; /* the type declaration ends here */
/