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

  • Home
  • SEARCH
  • 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 166299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:04:24+00:00 2026-05-11T12:04:24+00:00

Is there any way to access a dynamically accessing member of a User- defined

  • 0

Is there any way to access a dynamically accessing member of a User- defined record, object or reference cursor using a variable? E.g. Something like

  get_member(my_object, 'member name'); 

Or maybe

  my_object.$'member name'; 

EXECUTE IMMEDIATE won’t work as it does not operate within the scope of my procedure.

Let me brifly explain what i am trying to accomplish. I have a mapping table M which describes how records of table A should be transformed into records of table B. The mapping must vary according the specific type of record in A (given by A.type). I wanted to perform the mapping something like this (not exactly, planning on encapsulating the mapping logic inside a stream function, but the principle remains similar):

SELECT   ...      CASE      WHEN M.field_1_mapping IS NOT NULL THEN       -- column of A given by value of M.field_1_mapping     ELSE       null -- field_1 not filled for record type   END field_1,   --- etc. FROM   table_a A,   mapping_table M WHERE   A.TYPE = M.TYPE 

So my question is how I might do this. Again i cannot use dynamic SQL as it has to different for each record type, but if column could be selected based on the value of the mapping field then the obovemention sql would work.

I realize that it simply might not be possible (and may go against the PL/SQL design philosophy), in which case i would welcome any suggestions you might have as to how this problem could be solved.

P.S: I suppose it would be possible to simply hard-code a mapping function e.g.:

FUNCTION get_field(field_key IN VARCHAR(32), a NOCOPY IN table_a%rowtype) RETURN VARCHAR(2000) IS   out VARCHAR2(2000) BEGIN   -- ...   IF field_key = 'field_1' THEN     RETURN a.field_1; END IF;   -- .. END; 

But that seems really inelegant.

  • 1 1 Answer
  • 2 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. 2026-05-11T12:04:25+00:00Added an answer on May 11, 2026 at 12:04 pm

    Here is some code that will construct dynamic SQL from the mapping definitions. For simplicity I have used the EMP table as table A, with column DEPTNO serving as the type.

    declare    q long;    rc sys_refcursor;    first boolean := true;    l_field1 varchar2(100);    l_field2 varchar2(100);     function mapcol (p_field_mapping varchar2) return varchar2    is       l_retval varchar2(32);    begin       if p_field_mapping is not null then          l_retval := 'to_char(a.' || p_field_mapping || ')';       else          l_retval := 'null';       end if;       return l_retval;    end; begin    -- Construct dynamic SQL    for r_map in (select * from mapping_table)    loop       if first then          first := false;       else          q := q || ' union all ';       end if;       q := q || 'select ';       q := q || mapcol(r_map.field_1_mapping) || ', ';       q := q || mapcol(r_map.field_2_mapping);       q := q || ' from emp a where a.deptno = ' || r_map.type;    end loop;     -- Run SQL and show results    dbms_output.put_line('SQL = ' || q);    dbms_output.put_line('');    dbms_output.put_line('Results');    dbms_output.put_line('-------');    open rc for q;    loop       fetch rc into l_field1, l_field2;       exit when rc%notfound;       dbms_output.put_line(l_field1 || ', ' || l_field2);    end loop; end; 

    I then created this mapping table:

    SQL> create table mapping_table (type integer,   2>   field_1_mapping varchar2(30), field_2_mapping varchar2(30));  Table created.  SQL> insert into mapping_table values (10, 'ENAME', 'SAL');  1 row created.  SQL> insert into mapping_table values (20, 'SAL', 'JOB');  1 row created.  SQL> insert into mapping_table values (30, 'JOB', 'HIREDATE');  1 row created.  SQL> commit;  Commit complete. 

    When I run it (with SERVEROUTPUT ON in SQL Plus) I get:

    SQL = select to_char(a.ENAME), to_char(a.SAL) from emp a where a.deptno = 10 union all select to_char(a.SAL), to_char(a.JOB) from emp a where a.deptno = 20 union all select to_char(a.JOB), to_char(a.HIREDATE) from emp a where a.deptno = 30  Results ------- CLARK, 7450 KING, 10000 TEST, MILLER, 6500 BINNSY, 100 FARMER, 123 7975, MANAGER 4566, ANALYST 8000, ANALYST 5000, janitor SALESMAN, SALESMAN, 22-FEB-1981 SALESMAN, 28-SEP-1981 MANAGER, 01-MAY-1981 SALESMAN, 08-SEP-1981 MANAGER, 19-JUL-2008  PL/SQL procedure successfully completed. 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My AST model needs to carry location information (filename, line, index). Is there any
so i am using java servlets to response to a request from a jsp
My company is interested in using the azure storage tables. They have asked me
I have the need to pass an object from jstl to a Jquery click
Due to circumstances not fully under my control, I have to develop ACCESS queries
I have downloaded Xcode 4 but i do not like it much. it also
This is an Android noob question. I am trying to start an activity of
I'm starting to use jQuery Deferred objects a bit more and I run into
I've got ASP.NET intranet application written in VB. It gets a file from the
In C++, I'm trying to create a specialized point class as a union, like

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.