暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Oracle 如何从anydata类型字段中选择对象?

askTom 2017-08-09
528

问题描述

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? 
复制

专家解答

将其转换为您将其存储为的类型:

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')
复制


虽然我不得不问:

你为什么要把这个存储在anydata中?

存储对象类型已经足够增加了这一层复杂性。你能让你的列的类型成为对象本身吗?

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')
复制

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论