【问题标题】:Trying to create a type body in PL/SQL, but keep getting errors尝试在 PL/SQL 中创建类型体,但不断出现错误
【发布时间】:2014-10-25 05:46:44
【问题描述】:

我正在尝试为我的类型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 中是否可行。其次,有谁知道我可以做些什么来使语法正确吗?

【问题讨论】:

    标签: sql oracle plsql sqlplus


    【解决方案1】:

    通过一些格式,我使您的代码更具可读性,现在您可以理解您遗漏了什么。

    主体可以有 n 个函数和过程。每个都有一个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 */
    /
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-31
      • 1970-01-01
      • 2018-10-25
      • 1970-01-01
      相关资源
      最近更新 更多