oracle 11g中调用成员函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2290668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 20:01:31  来源:igfitidea点击:

Calling member function in oracle 11g

oracleoracle11g

提问by joec

I have a type called budget defined as

我有一个称为预算的类型定义为

create type budget as object ( 
    year number,
    amount number,
    member function left_over (year in number) return number
)

Body:

身体:

create type body budget as
    member function left_over(year in number) return number is
    begin
        return amount;
    end left_over;
end;

And an object table

和一个对象表

create table budget_table of budget;

How do I use the member function to return the amount? Something like:

如何使用成员函数返回金额?就像是:

select b.left_over(2010) from budget_table b;

Thanks

谢谢

回答by Tony Andrews

You don't need a parameter to the method:

您不需要该方法的参数:

SQL> create or replace type budget as object (
  2      year number,
  3      amount number,
  4      member function left_over return number
  5  )
  6  /

Type created.

SQL> create or replace type body budget as
  2      member function left_over return number is
  3      begin
  4          return amount;
  5      end left_over;
  6  end;
  7  /

Type body created.

SQL> create table budget_table of budget;

Table created.

SQL> insert into budget_table values (budget(2010,99));

1 row created.

SQL> commit;

Commit complete.

SQL> select b.left_over() from budget_table b;

B.LEFT_OVER()
-------------
           99

(I assume this is an academic exercise, as it would make no sense to create tables like this in a real business database!)

(我认为这是一个学术练习,因为在真实的商业数据库中创建这样的表是没有意义的!)

To restrict to the budget for a particular year:

限制特定年份的预算:

SQL> insert into budget_table values (budget(2010,99));

1 row created.
SQL> select b.left_over() from budget_table b;

B.LEFT_OVER()
-------------
           88
           99

SQL> select b.left_over() from budget_table b
  2  where b.year = 2010;

B.LEFT_OVER()
-------------
           99

回答by APC

It is a scoping issue. Your function left_over()is a method on Budget, which is an individual thing. However, you want to have a method which does a look up from a range of budgets. The method as you have written it cannot do this, because an instance can only know about itself. How can the Budgetfor 2009 possibly know the figures for 2010?

这是一个范围界定问题。你的函数left_over()是一个方法Budget,它是一个单独的东西。但是,您希望有一种方法可以从一系列预算中进行查找。您编写的方法无法执行此操作,因为实例只能了解自身。Budget2009 年的数字怎么可能知道 2010 年的数字?

What you need is an object Budgetswhich has a collection of Budgetas an attribute, and a member function left_over()which returns the whatever for a given Budgetin its collection. Of course, the only way of getting that information is for the method to iterate over the collection, which will perform far less efficiently than a WHERE clause on a regular table but seems to be the standard approach in OO data practices.

您需要的是一个对象Budgets,它具有Budget作为属性的集合,以及一个成员函数 left_over(),它返回Budget其集合中给定的任何内容。当然,获取该信息的唯一方法是迭代集合的方法,其执行效率远低于常规表上的 WHERE 子句,但似乎是 OO 数据实践中的标准方法。