Skip to Main Content
  • Questions
  • how to select object from anydata type field?

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, David.

Asked: August 09, 2017 - 1:27 pm UTC

Last updated: August 10, 2017 - 1:53 pm UTC

Version: 11c

Viewed 1000+ times

You Asked

CREATE OR REPLACE Type t_Au_Delivery_Service As Object
(
    Yes_No         Integer,
    Delivery_Price Number,
    Note           Varchar2(1000)
);

create table AU_AUCTION_PARAM
(
  id_au_auction_param NUMBER not null,
  id_au_auction       NUMBER not null,
  id_au_param_lib     NUMBER not null,
  au_param_value      SYS.ANYDATA
);
Declare
    d t_Au_Delivery_Service;
Begin
  d:= t_Au_Delivery_Service(1,1,'');
    d.Yes_No         := 1;
    d.Delivery_Price := 13.765;
    d.Note           := 'any text';

    Insert Into Au_Auction_Param
        (Id_Au_Auction,
         Id_Au_Param_Lib,
         Au_Param_Value)
    Values
        (1,
         1,
         Sys.Anydata.Convertobject(d));
End;
and this select get error ora-22636:

select t.au_param_value from AU_AUCTION_PARAM t  -- ???

how to select object from anydata field? 

and Chris said...

Cast it as the type you're storing it as:

create or replace type t_au_delivery_service as object (
  yes_no           integer,
  delivery_price   number,
  note             varchar2( 1000 )
);
/

create table au_auction_param (
  id_au_auction_param   number not null,
  id_au_auction         number not null,
  id_au_param_lib       number not null,
  au_param_value        sys.anydata
);

declare
  d   t_au_delivery_service;
begin
  d := t_au_delivery_service( 1,1,'' );
  d.yes_no := 1;
  d.delivery_price := 13.765;
  d.note := 'any text';
  insert into au_auction_param ( 
    id_au_auction_param, id_au_auction,id_au_param_lib,au_param_value 
  ) values (
    1, 1, 1, sys.anydata.convertobject( d )
  );

end;
/

select cast(t.au_param_value as t_au_delivery_service) 
from   au_auction_param t;

T_AU_DELIVERY_SERVICE(1, 13.765, 'any text')


Though I have to ask:

Why are you storing this in anydata?

Storing object types is enough of a faff already with adding this layer of complexity. Can you make the type of your column the object itself?

drop table au_auction_param purge;
create table au_auction_param (
  id_au_auction_param   number not null,
  id_au_auction         number not null,
  id_au_param_lib       number not null,
  au_param_value        t_au_delivery_service
);

declare
  d   t_au_delivery_service;
begin
  d := t_au_delivery_service( 1,1,'' );
  d.yes_no := 1;
  d.delivery_price := 13.765;
  d.note := 'any text';
  insert into au_auction_param ( 
    id_au_auction_param, id_au_auction,id_au_param_lib,au_param_value 
  ) values (
    1, 1, 1, d
  );

end;
/

select t.au_param_value
from   au_auction_param t;

T_AU_DELIVERY_SERVICE(1, 13.765, 'any text')

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Thank for response

David, August 09, 2017 - 5:25 pm UTC

Thank you for your comprehensive response.
Further to your question, would like to inform you that in the table below number of values of vaarious parametres are stored: some of which - of data type, others - number type, etc. I used anydata type for this.

Thank you.
Chris Saxon
August 10, 2017 - 1:53 pm UTC

I don't see why this requires the use of anydata. But if that's what you want to do...

More to Explore

Design

New to good database design? Check out Chris Saxon's full fundamentals class.