Oracle数据库中UDT中的Setter方法

时间:2018-04-14 16:14:30

标签: oracle plsql user-defined-types

我还是新手,并试图绕过它。 UDT能够定义你可以在存储的对象上调用的方法,我似乎创建了一个返回值很好的方法,但是想知道它是否可以创建一个setter方法。这种情况并不是非常有用,但仅仅是为了澄清

例如,我有这种类型:

library( dplyr )
library( purrr )

merged.list <-
  map2( .x = seq_along( along.with = LIST )
        , .y = LIST
        , .f = function( i, j )
          if( i %% 2 == 0 ){
            left_join( x = j
                       , y = B
                       , by = "Let" )
          } else{
            left_join( x = j
                       , y = A
                       , by = "Let" )
          })

# view results
merged.list
# [[1]]
# Num Let    Col
# 1   1   a Yellow
# 2   2   b    Red
# 
# [[2]]
# Num Let   Col
# 1   1   a Green
# 2   2   b  Blue

# end of script #

哪个编译好,所以我的假设setter方法是允许的 我尝试过创建下面的身体类型:

create TYPE TestType2 AS OBJECT(
Numb NUMBER(4),
Str VARCHAR2(10),
MEMBER FUNCTION setNum(numba NUMBER) RETURN NUMBER
);

然而,这无法解决以下错误:

CREATE TYPE BODY TestType2 as
member function setNum(numba NUMBER) return NUMBER is
    begin
        SELF.Numb := numba;
        return SELF.Numb;
    END;
END;

有没有办法创建一个set方法,还是只允许在商店程序中使用?

1 个答案:

答案 0 :(得分:1)

这是一个模糊的错误。问题是成员函数采用SELF的隐式参数。因此,如果您想要更改某些内容,则需要将参数设为显式:

create or replace TYPE TestType2 AS OBJECT(   
    Numb NUMBER(4,0),
    Str VARCHAR2(10),
    MEMBER procedure setNum(self in out TestType2, numba NUMBER )
 );
/
CREATE or replace TYPE BODY TestType2 as
    member function setNum(self in out TestType2 , numba NUMBER) return NUMBER 
    is
    begin
        self.Numb := numba;
        return SELF.Numb;
    END;
END;
/

请注意,调用函数时,SELF参数仍然是隐式的:

declare
   t TestType2 := TestType2(4, 'TEST');
   n pls_integer;
begin
    dbms_output.put_line('1' || t.numb);
    n := t.setNum(8);
    dbms_output.put_line('2' || t.numb);
end;
/

顺便提一下,setter方法不需要是函数;我们也可以有成员程序。

create or replace TYPE TestType2 AS OBJECT(
    Numb NUMBER(4,0),
    Str VARCHAR2(10),
    MEMBER procedure setNum(self in out TestType2, numba NUMBER ),
    MEMBER FUNCTION getNum RETURN NUMBER
);
/
CREATE or replace TYPE BODY TestType2 as
    member procedure setNum(self in out TestType2, numba NUMBER )
is
    begin
        self.Numb := numba;
    END;

    MEMBER FUNCTION getNum RETURN NUMBER
    is
    begin
        return self.numb;
    end;
END;
/