函数以返回具有不同列数的结果集

时间:2019-02-07 20:39:09

标签: postgresql

首先让我说,根据我的研究,我什至不确定在postgres中是否可行!话虽这么说...

我正在使用具有“可配置字段”的保险数据库。许多表具有通用名称的列(字面上为field1,field2,field3等)。我正在尝试编写一个函数,该函数将解析配置表,为所需的特定数据类型确定表名和列名,然后构建,执行并返回该查询的结果。诀窍在于,结果集中的列数将根据所需数据的类型而变化。它可以是2列,十几列或任意数量。它也可以是任意数量的记录。

使用配置表映射这些列中存储的数据。因此,其中一个配置表告诉我该表的名称和该列的名称,另一个配置表告诉该字段的“名称”(从字面上看,这是普通人为数据库列命名的名称)。表中的一行指定“类型”。 “类型”不是指数据类型,而是指与其他“类型”完全不相关的不同类型的数据。将“类型”想像为通常在其自己的数据库表或列中使用的类型。无论如何,数据本身存储在这些随机分配的列之一中。同一表中的同一列可以(并且确实)具有其他,完全

可以并且将来将定义新的数据类型。

我已经研究了多态表,但是看来您必须将结果集定义为函数定义的一部分。在Sybase中,这非常容易,因为Sybase函数可以从字面上返回任何结果集,而无需声明列和类型。我的问题不是产生所需的SQL语句或执行它。我还没有走那么远,但这似乎相对简单,但是我不知道如何返回执行的任何SQL语句。

对于我的POC,我编写了以下代码:

select trim('select ' || bfb.table_name || '.Policy_ID, ' || string_agg('' || bfb.table_name || '.' || bfb.column_name || ' as ' || bf.name , ',' ORDER BY replace(bo.code, 'SISCH', ''))) AS selectstatement, 
            'from ' || '' || bfb.table_name AS fromstatement, 
            -- We want the query limited only to the policy for which we are running this procedure
            'where ' || '' || bfb.table_name || '.Policy_ID = ' || '1000301378' ||  
            -- And we only want the t_business_obect.code value that the cursor is on
                ' and ' || '' || bfb.table_name || '.schedule_info_code = ''' || bo.code  || ''' UNION ALL ' AS wherestatement, 
            -- I am stripping off the prefix so we are left with only the row number (on a schedule))
            replace(bo.code, 'SISCH', '') AS coderownum
from t_business_model bm join t_business_object bo on bm.model_id = bo.model_id
                                join T_BUSINESS_FIELD_BINDING bfb on bo.object_id = bfb.object_id 
                                join T_BUSINESS_FIELD bf on bfb.field_id = bf.field_id  
where bfb.Binding_Data_ID = 6000001969
-- Trying to code this to work on either an entire schedule or a single column
and (bo.code LIKE coalesce('SISCH', '') || '%')
group by bfb.table_name, bo.object_id, replace(bo.code, 'SISCH', ''), bo.code
order by bfb.table_name, replace(bo.code, 'SISCH', '')

哪个(当我从结果中删除最后一个UNION ALL时)产生以下SQL语句:

select T_AS_SCHEDULE_INFO.Policy_ID, T_AS_SCHEDULE_INFO.FIELD199 as NumofLawyers,T_AS_SCHEDULE_INFO.FIELD200 as PriorActsYearscovered,T_AS_SCHEDULE_INFO.FIELD198 as NumofLawyersLPL    from T_AS_SCHEDULE_INFO where T_AS_SCHEDULE_INFO.Policy_ID = 1000301378 and T_AS_SCHEDULE_INFO.schedule_info_code = 'SISCH1' UNION ALL 
select T_AS_SCHEDULE_INFO.Policy_ID, T_AS_SCHEDULE_INFO.FIELD199 as NumofLawyers,T_AS_SCHEDULE_INFO.FIELD200 as PriorActsYearscovered,T_AS_SCHEDULE_INFO.FIELD198 as NumofLawyersLPL    from T_AS_SCHEDULE_INFO where T_AS_SCHEDULE_INFO.Policy_ID = 1000301378 and T_AS_SCHEDULE_INFO.schedule_info_code = 'SISCH2' UNION ALL 
select T_AS_SCHEDULE_INFO.Policy_ID, T_AS_SCHEDULE_INFO.FIELD200 as PriorActsYearscovered,T_AS_SCHEDULE_INFO.FIELD199 as NumofLawyers,T_AS_SCHEDULE_INFO.FIELD198 as NumofLawyersLPL    from T_AS_SCHEDULE_INFO where T_AS_SCHEDULE_INFO.Policy_ID = 1000301378 and T_AS_SCHEDULE_INFO.schedule_info_code = 'SISCH3' UNION ALL 
select T_AS_SCHEDULE_INFO.Policy_ID, T_AS_SCHEDULE_INFO.FIELD198 as NumofLawyersLPL,T_AS_SCHEDULE_INFO.FIELD200 as PriorActsYearscovered,T_AS_SCHEDULE_INFO.FIELD199 as NumofLawyers    from T_AS_SCHEDULE_INFO where T_AS_SCHEDULE_INFO.Policy_ID = 1000301378 and T_AS_SCHEDULE_INFO.schedule_info_code = 'SISCH4' UNION ALL 
select T_AS_SCHEDULE_INFO.Policy_ID, T_AS_SCHEDULE_INFO.FIELD199 as NumofLawyers,T_AS_SCHEDULE_INFO.FIELD198 as NumofLawyersLPL,T_AS_SCHEDULE_INFO.FIELD200 as PriorActsYearscovered    from T_AS_SCHEDULE_INFO where T_AS_SCHEDULE_INFO.Policy_ID = 1000301378 and T_AS_SCHEDULE_INFO.schedule_info_code = 'SISCH5' UNION ALL 
select T_AS_SCHEDULE_INFO.Policy_ID, T_AS_SCHEDULE_INFO.FIELD198 as NumofLawyersLPL,T_AS_SCHEDULE_INFO.FIELD199 as NumofLawyers,T_AS_SCHEDULE_INFO.FIELD200 as PriorActsYearscovered    from T_AS_SCHEDULE_INFO where T_AS_SCHEDULE_INFO.Policy_ID = 1000301378 and T_AS_SCHEDULE_INFO.schedule_info_code = 'SISCH6'  

我希望我的函数能够执行该代码并返回结果集,而不管查询可能返回了多少列。我并不担心数据类型,因为第一列将始终是ID,其他列都将存储为文本。

最后,我试图生成一个结果集。在上面的示例中,结果集将有4列:policy_id,numoflawyers,prioractsyearscovered和numoflawyerslpl。

0 个答案:

没有答案