Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Methods
The UDTs that we have seen to this point have attributes, but not methods. It is certainly possible, however, to declare methods as part of a UDT and then to use SQL programming to define the body of the methods. Like classes used by OO programming languages such C++, SQL the body a method is defined separately from the declaration of the UDT.
You declare a method after declaring the structure of a UDT. For example, we could add a method to display the instructions of a recipe with
CREATE TYPE recipe_type AS OBJECT
(recipe_name CHAR (256),
instruction_list instruction ARRAY[20],
numb_servings INT)
NOT INSTANTIABLE,
NOT FINAL
METHOD show_instructions ();
This particular method does not return a value and the declaration therefore does not include the optional RETURNS clause. However, a method to compute the cost of a recipe (if we were to include ingredient costs in the database) could be declared as
CREATE TYPE recipe_type AS OBJECT
(recipe_name CHAR (256),
instruction_list instruction ARRAY[20],
numb_servings INT)
NOT INSTANTIABLE,
NOT FINAL
METHOD show_instructions ()
METHOD compute_cost ()
RETURNS DECIMAL (5,2));

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint