我有以下 PL/SQL 对象:
CREATE OR REPLACE TYPE TestType AS OBJECT
(
firstname VARCHAR2(30),
lastname VARCHAR2(30),
MEMBER PROCEDURE dosomething,
MEMBER function downgrade return number,
CONSTRUCTOR FUNCTION TestType(fname VARCHAR2, lname VARCHAR2) RETURN SELF AS RESULT
)
/
CREATE OR REPLACE TYPE BODY TestType AS
CONSTRUCTOR FUNCTION TestType(fname VARCHAR2, lname VARCHAR2)
RETURN SELF AS RESULT
IS
BEGIN
SELF.firstname := fname;
SELF.lastname := lname;
RETURN;
END;
MEMBER function downgrade return number IS
BEGIN
self.dosomething; /* Compilation Error Here */
return 1;
END;
MEMBER PROCEDURE dosomething IS
BEGIN
null;
END;
END;
/
这是我拥有的复杂对象的一个示例。基本上,我需要从成员函数调用成员过程。这样做时,我收到编译错误:
PLS-00363: expression cannot be used as an assignment target
为什么会出现这个错误?
最佳答案
Member methods have a built-in parameter named SELF that denotes the object instance currently invoking the method.
SELF can be explicitly declared, but that is not necessary. ...
后来还说:
SELF is always the first parameter passed to the method.
- In member functions, if SELF is not declared, its parameter mode defaults to IN.
- In member procedures, if SELF is not declared, its parameter mode defaults to IN OUT. The default behavior does not include the NOCOPY compiler hint.
您收到错误是因为您的过程调用
self.dosomething;
将self
作为IN OUT
隐式传递给过程,但它又以IN
隐式传递到函数中>。这意味着该过程调用的模式不正确 - 该模式意味着该过程可以修改 self
,而该函数却表示不会。
您可以通过更改函数中的模式、在以下两种类型中显式声明 self
参数来避免错误:
MEMBER function downgrade (self in out TestType) return number,
并输入正文:
MEMBER function downgrade (self in out TestType) return number IS
如果您的真实过程不会修改 self
(也许不太可能),那么您可以将其声明为 self IN
。
人们常说函数应该只返回值并且不应该有副作用,所以可以说这会更正确 - 调用你的成员函数的人可能不会期望它改变任何东西。
因此,如果 dosomething
正在更改数据,那么也许 downgrade
也应该是一个带有 OUT
参数的过程而不是返回值。这样您就不需要声明 self
,因为默认情况下两者都是 IN OUT
。
关于oracle - Oracle中的错误 "PLS-00363: expression cannot be used as an assignment target"如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75394876/