Search code examples
oracleplsqloracle11g

collection of records to out sys_refcursor


Oracle 11g

This seems harder than it should be so I might be on the wrong path here.

I have an application that generates user defined forms, my data is a bit more complicated than this but the idea is -- I have a data table which contains all the data input from the user defined forms

create table formData(
      id number
    , fName varchar(100)
    , lName varChar(100)
    , mName varChar(100)
    , formType varchar(100)
    ...
);

insert all 
    into formData(id,fName,lName,mName,formType)values(1,'Bob','Smith',NULL,'birthday')
    into formData(id,fName,lName,mName,formType)values(2,'Jim','Jones','Wilber','birthday')
    into formData(id,fName,lName,mName,formType)values(3,'Frank','Peterson',NULL,'general')
    into formData(id,fName,lName,mName,formType)values(4,'Alex','Anderson',NULL,'general')

I have a table which contains the field options for the dynamic forms

create table fieldOptions(
      id number
    , fieldName varchar(100)
    , fieldLabel varChar(100)
    , formType varchar(10)
    , fieldUsed number
    , ...
);
insert all 
    into fieldOptions (fieldName,fieldLabel,formType,fieldUsed)values('fName','First Name','birthday',1)
    into fieldOptions (fieldName,fieldLabel,formType,fieldUsed)values('lName','Last Name','birthday',1)
    into fieldOptions (fieldName,fieldLabel,formType,fieldUsed)values('mName','Middle','birthday',1)
    into fieldOptions (fieldName,fieldLabel,formType,fieldUsed)values('fName','First','general',1)
    into fieldOptions (fieldName,fieldLabel,formType,fieldUsed)values('lName','Surname','general',1)
    into fieldOptions (fieldName,fieldLabel,formType,fieldUsed)values('mName','Middle Initial','general',0)

I want to create a procedure in my package that will return a cursor to my .net page that contains data that looks like:

where ID=3 (general output)

  Label | Value
--------+---------
First   | Frank
Surname | Peterson

or where ID=1 (birthday output)

  Label     | Value
------------+---------
First Name  | Bob
Last Name   | Smith
Middle      | NULL

I'm unsure if I can do this in a (pivot?) query. I started toying with a collection of records built by processing the data but how would I get a collection of records into an out sys_refcursor if that's the solution? Perhaps I'm over thinking this and it can be done with a few sub queries? A shove in the right direction would be perfect, thanks.


Solution

  • Assuming your formData table structure is fixed and known, you can just use a case expression to translate the formOption.fName to the matching column value:

    select fo.fieldLabel as label,
      case fo.fieldName
        when 'fName' then fd.fName
        when 'lName' then fd.lName
        when 'nName' then fd.mName
      end as value
    from formData fd
    join fieldOptions fo
    on fo.formType = fd.formtype
    where fd.id = 3;
    
    LABEL                VALUE               
    -------------------- --------------------
    First                Frank               
    Surname              Peterson            
    Middle Initial                           
    
    ...
    where fd.id = 3;
    
    LABEL                VALUE               
    -------------------- --------------------
    First Name           Bob                 
    Last Name            Smith               
    Middle                                   
    

    You can then have your procedure open a ref cursor for that query, using an argument value for the ID value.

    If the formData structure isn't known, or isn't static, then you probably have bigger problems; but for this you'd need to fall back to dynamic SQL. As a starting point, you could do something like:

    create procedure p42 (p_id number, p_refcursor out sys_refcursor) as
      l_stmt varchar2(32767);
    begin
      l_stmt := 'select fo.fieldLabel as label, case lower(fo.fieldName) ';
      for r in (
        select column_name from user_tab_columns
        where table_name = 'FORMDATA'
        and data_type = 'VARCHAR2'
      )
      loop
        l_stmt := l_stmt || ' when ''' || lower(r.column_name) || ''' then fd.' || r.column_name;
      end loop;
      l_stmt := l_stmt || ' end as value '
        || 'from formData fd '
        || 'join fieldOptions fo '
        || 'on fo.formType = fd.formtype '
        || 'where fd.id = :d1';
      open p_refcursor for l_stmt using p_id;
    end p42;
    /
    

    This uses all the columns actually defined in the table to create the case expression at run time; because the case of your fieldName may not match the data dictionary, I'm forcing everything to lowercase for comparison. I'm also restricting to string columns to make the case simpler, but if you need columns which are other data types then each when ... then clause of the case expressions would need to check that column's data type (which you can add to the r cursor) and convert the actual column value to a string appropriately. All of the values have to end up the same data type, so it has to be strings, really.

    Anyway, testing this from SQL*Plus:

    var rc refcursor
    exec p42(1, :rc);
    
    PL/SQL procedure successfully completed.
    
    print rc
    
    LABEL                VALUE
    -------------------- --------------------
    First Name           Bob
    Last Name            Smith
    Middle
    
    3 rows selected.
    

    You could query fieldOptions to get the possible column names instead, but you still may have the data type conversion issue, wich would be harder to deal with; but if all the referenced formData fields are actually strings then that would be:

      for r in (
        select fo.fieldName
        from formData fd
        join fieldOptions fo
        on fo.formType = fd.formtype
        where fd.id = p_id
      )
      loop
        l_stmt := l_stmt || ' when ''' || r.fieldName || ''' then fd.' || r.fieldName;
      end loop;