I am trying to use a named rowtype that is being returned in a stored procedure, and putting those columns into a view.
The end goal would be a view like this:
select * from view_tmp
table_f1 table_f1_val
table_f2 1
table_f3 2
f1 f1_val
f2 f2_val
f3 v3_val
The problem I am running in to is with the stored procedure being run once for every field I’m pulling back.
create view view_tmp (table_f1,table_f2,table_f3, f1, f2, f3) as (
select a.table_f1,a.table_f2,a.table_f3,
feat_proc_tmp(a.table_f1,a.table_f2).f1,
feat_proc_tmp(a.table_f1,a.table_f2).f2,
feat_proc_tmp(a.table_f1,a.table_f2).f3
from table_tmp a)
This runs feat_proc_tmp 3 times which is not good (data wise and performance wise)
My procedure returns a rowtype, so how can I access these differently to get the data into the view without running it 3 times? This is as close as I can get with my attempt but this won’t go into a view (maybe it can and I can’t figure it out?):
select *,feat_proc_tmp('1',1) from table_tmp
table_f1 table_f1_val
table_f2 1
table_f3 2
(expression) ROW('f1_val','f2_val','v3_val')
Here’s the SQL to recreate:
drop row type features_tmp restrict;
drop procedure feat_proc_tmp;
drop table table_tmp;
drop view view_tmp;
create row type features_tmp (
f1 varchar(255),
f2 varchar(255),
f3 varchar(255)
);
create procedure feat_proc_tmp (Ipass char(10), product_id int)
returning features_tmp;
RETURN ROW('f1_val','f2_val','v3_val')::features_tmp;
end procedure;
create table table_tmp (
table_f1 char(16),
table_f2 int,
table_f3 int
);
insert into table_tmp values ('table_f1_val',1,2);
create view view_tmp (table_f1,table_f2,table_f3, f1, f2, f3) as (
select a.table_f1,a.table_f2,a.table_f3,
feat_proc_tmp(a.table_f1,a.table_f2).f1,
feat_proc_tmp(a.table_f1,a.table_f2).f2,
feat_proc_tmp(a.table_f1,a.table_f2).f3
from table_tmp a)
This is on Informix v11. One thought I had would be to join the procedure as a table and then pull from that but I can’t find a format that will work here-:
select a.*,b.* from table_tmp a CROSS JOIN TABLE (feat_proc_tmp('1',1)) b
returns the b.* as a ROW() so if I try
select a.*,b.f1 from table_tmp a CROSS JOIN TABLE (feat_proc_tmp('1',1)) b
Then column f1 not found in any table in the query happens.
I appear to have stumbled on a bug in 11.5 FC6W2, if I do this on FC6 or FC4, the stored procedure only executes one time in my first example. So an upgrade solved my issue.