Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7830327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:11:08+00:00 2026-06-02T11:11:08+00:00

I am writing a procedure to deal with user defined object which is stored

  • 0

I am writing a procedure to deal with user defined object which is stored in ANYDATA. The object type and the attribute name can only be known at run time, so I can’t define the viarable for it in declare section. In Java, I can use reflection to deal with it, I can know the class name and fields name. Then I can access the fields through reflection. Is there any way to do it in PLSQL like that? What in my head right now is creating an sql string in the procedure dynamically and execute it. But it is not what I want exactly.

Let’s say, user A defined a ADT type as create or replace type Person_type as object (fname varchar2(10), lname varchar2(10)); and create an object instance and insert it into ANYDATA.

In my procedure, somehow I know I need to deal with the first attribute of this object, which is fname. So if know the adt type at the first place, my code will be like:

declare
  adobject A.Person_type; -- HERE! I don't know the type yet, so I can't define adobject!
  tempAnydata anydata;
  rt number;
  vbuffer varchar2(10);
  ...
begin
   select somecolumn 
   into tempAnydata 
   from sometable 
   where something='something' for update;

   rt := tempAnydata.GetObject(adobject);

   vbuffer := adobject.fname; -- HERE! I don't know the attribute name is fname!
   -- deal with vbuffer here
end;

So what should I do to make it dynamically? Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T11:11:10+00:00Added an answer on June 2, 2026 at 11:11 am

    You need to use ANYTYPE to describe the ANYDATA and ensure the type is correct. Then you can access the attribute using piecewise and getVarchar2.

    Most of the code below is for checking the type, which you don’t need if you’re not concerned about type safety.

    Function to return value:

    create or replace function get_first_attribute(
        p_anydata in out anydata --note the "out" - this is required for the "piecewise"
    ) return varchar2 is
        v_typecode pls_integer;
        v_anytype anytype;
    begin
        --Get the typecode, and the ANYTYPE
        v_typecode := p_anydata.getType(v_anytype);
    
        --Check that it's really an object
        if v_typecode = dbms_types.typecode_object then
            --If it is an object, find the first item
            declare
                v_first_attribute_typecode pls_integer;
                v_aname          varchar2(32767);
                v_result         pls_integer;
                v_varchar        varchar2(32767);
                --Variables we don't really care about, but need for function output
                v_prec           pls_integer; 
                v_scale          pls_integer;
                v_len            pls_integer;
                v_csid           pls_integer;
                v_csfrm          pls_integer;
                v_attr_elt_type  anytype;
            begin
                v_first_attribute_typecode := v_anytype.getAttrElemInfo(
                    pos            => 1, --First attribute
                    prec           => v_prec,
                    scale          => v_scale,
                    len            => v_len,
                    csid           => v_csid,
                    csfrm          => v_csfrm,
                    attr_elt_type  => v_attr_elt_type,
                    aname          => v_aname);
    
                --Check typecode of attribute
                if v_first_attribute_typecode = dbms_types.typecode_varchar2 then
                    --Now that we've verified the type, get the actual value.
                    p_anydata.piecewise;
                    v_result := p_anydata.getVarchar2(c => v_varchar);
    
                    --DEBUG: Print the attribute name, in case you're curious
                    --dbms_output.put_line('v_aname: '||v_aname);
    
                    return v_varchar;
                else
                    raise_application_error(-20000, 'Unexpected 1st Attribute Typecode: '||
                        v_first_attribute_typecode);
                end if;
            end;
        else
            raise_application_error(-20000, 'Unexpected Typecode: '||v_typecode);
        end if;
    end;
    /
    

    Types:

    create or replace type Person_type as object (fname varchar2(10), lname varchar2(10));
    
    create or replace type other_type as object (first_name varchar2(10), poetry clob);
    

    Test Run:

    declare
        --Create records
        v_type1 person_type := person_type('Ford', 'Prefect');
        v_type2 other_type := other_type('Paula', 'blah blah...');
        v_anydata anydata;
    begin
        --Convert to ANYDATA.
        --Works as long as ANYDATA is an object with a varchar2 as the first attribute.
        v_anydata := anydata.convertObject(v_type1);
        dbms_output.put_line(get_first_attribute(v_anydata));
    
        v_anydata := anydata.convertObject(v_type2);
        dbms_output.put_line(get_first_attribute(v_anydata));
    end;
    /
    

    Outputs:

    Ford
    Paula
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a stored procedure in PlPython with a user defined type. I
When writing a procedure in PL/SQL, I can declare a parameter's type as myTable.myColumn%TYPE
I'm into writing a stored procedure which explodes a passed string by a passed
I am writing a stored procedure which collects all changes that were happend in
I'm writing a stored procedure to copy data from one user's table to another
I'm writing a stored procedure that gets data for a whole screen. How can
I am writing a procedure which is related to apple apns feedback. I have
I am writing a stored procedure that when completed will be used to scan
I am writing a stored procedure where i m using try catch block. Now
I am writing a stored procedure for displaying month and year. It is working,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.