PL / SQL使用表中的数据填充对象(PL/SQL Fill object with data from tables)

我在pl / sql中创建了自定义对象。 我想要做的是从表创建对象传递id并使用来自一行的数据填充对象属性(由此id )。 这个怎么做? 我在oracle 10g工作。

我的类型看起来像这样:

CREATE OR REPLACE TYPE userType AS OBJECT( id number, name varchar2(100), nickname varchar2(100), email varchar2(100), CONSTRUCTOR FUNCTION userType(userId number) RETURN SELF AS RESULT );

并type body声明:

CREATE OR REPLACE TYPE BODY userType AS CONSTRUCTOR FUNCTION userType(userId number) RETURN SELF AS RESULT AS BEGIN self.id := userId; self.name := ???; --<- need help there self.nickname := ??? --(something like select name from userType where id = userId) (and so on) RETURN; END; END;

表列与userType属性具有相同的名称。

I have custom object created in pl/sql. What I want to do is to create object passing id from table and fill object attributes with data from one row (speciefied by this id). How to do this? I'm working in oracle 10g.

My type looks like this:

CREATE OR REPLACE TYPE userType AS OBJECT( id number, name varchar2(100), nickname varchar2(100), email varchar2(100), CONSTRUCTOR FUNCTION userType(userId number) RETURN SELF AS RESULT );

And type body declaration:

CREATE OR REPLACE TYPE BODY userType AS CONSTRUCTOR FUNCTION userType(userId number) RETURN SELF AS RESULT AS BEGIN self.id := userId; self.name := ???; --<- need help there self.nickname := ??? --(something like select name from userType where id = userId) (and so on) RETURN; END; END;

Table columns have the same name as userType attributes.

最满意答案

我认为你应该能够做到以下几点:

CREATE OR REPLACE TYPE BODY userType AS CONSTRUCTOR FUNCTION userType(userId number) RETURN SELF AS RESULT AS BEGIN self.id := userId; SELECT name, nickname INTO self.name, self.nickname FROM user_table WHERE id = userId; RETURN; END; END;

虽然我不确定这是正确的还是最好的方式。

I think you should be able to do the following:

CREATE OR REPLACE TYPE BODY userType AS CONSTRUCTOR FUNCTION userType(userId number) RETURN SELF AS RESULT AS BEGIN self.id := userId; SELECT name, nickname INTO self.name, self.nickname FROM user_table WHERE id = userId; RETURN; END; END;

Although I'm not sure on whether this is the correct or best way.

更多推荐