views:

182

answers:

2

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

+1  A: 

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
Tony Andrews
It is academic. Also is it possible to pass in a value so i can just get the 2010 budget and not 2009 etc. thanks.
joec
See updated answer - you wouldn't use a parameter to do that, you would select the row you want the amount for.
Tony Andrews
thanks but part of the requirements is that it gets passed into the function. That why i had the parameter in my original attempt...
joec
That doesn't make any sense - you might just as well create a stand-alone function if that is the goal! You are going to have to select the data from the relevant row.
Tony Andrews
Maybe you should post the assignment question - it could be you have misunderstood it?
Tony Andrews
Using the budget information, a Manager should also be able to calculate at any time, the amount she/he has left to spend in the year. In order to achieve this she/he requires a function which takes in the year and returns the amount left to spend.
joec
That says a function, not necessarily a method function of an object.
Tony Andrews
+1  A: 

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 Budget for 2009 possibly know the figures for 2010?

What you need is an object Budgets which has a collection of Budget as an attribute, and a member function left_over() which returns the whatever for a given Budget in 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.

APC
Could you provide a quick code snippet of what you mean? Do you mean create a `budgets` table which has an object type column of `Budget`?
joec