I may have "out-smart" myself this time! Most of the time I develop my SQL statements in Ops Nav to get the data I need and then either cut & paste the code into static SQL in my program or pull it apart to build dynamic SQL.
This time I pretty much left the SQL as-is and used an Aaron Bartell procedure to read the IFS .sql file into an RPG variable and use that in my prepare statement.
Works great. Had to do a find/replace on the :host variable, but still okay.
Here is the problem I'm facing. Normally when I do dynamic SQL and have a varying number of host variables I do something like this:
Notice :regionDesc is returned in one fetch, but not the other.
With my new SQL statement above regionDesc is either returns a real value or null if regionDesc is not part of the select and grouping clauses. I'm not sure how to handle this null in RPG. My DDS is defined as ALWNULL for the regionDesc field.
Here is how my data is returned if by State:
This time I pretty much left the SQL as-is and used an Aaron Bartell procedure to read the IFS .sql file into an RPG variable and use that in my prepare statement.
Works great. Had to do a find/replace on the :host variable, but still okay.
Code:
with x as ( select affiliate, regiondesc, state, gender, case when compensate between 1 and 30000 then 1 else 0 end as a, case when compensate between 30001 and 40000 then 1 else 0 end as b, case when compensate between 40001 and 50000 then 1 else 0 end as c, case when compensate > 50000 then 1 else 0 end as d from file ) select affiliate, case when :p_sort = 'R' then regiondesc else null end, state, gender, sum(a) as less_than_30k, sum(b) as between_30k_40k, sum(c) as between_40k_50k, sum(d) as greater_than_50k from x where case when :p_entity = 'ALL' then affiliate else :p_entity end = affiliate group by affiliate, case when :p_sort = 'R' then regiondesc else null end, state, gender order by affiliate, case when :p_sort = 'R' then regiondesc else null end, state, gender
Here is the problem I'm facing. Normally when I do dynamic SQL and have a varying number of host variables I do something like this:
Code:
if ( p_sort = 'R' );
exec sql fetch c1 into :affiliate, [B][I]:regionDesc,[/I][/B] :stateDesc, :gender,
:bucketA, :bucketB, :bucketC, :bucketD
;
else;
exec sql fetch c1 into :affiliate, :stateDesc, :gender,
:bucketA, :bucketB, :bucketC, :bucketD
;
endif;
Notice :regionDesc is returned in one fetch, but not the other.
With my new SQL statement above regionDesc is either returns a real value or null if regionDesc is not part of the select and grouping clauses. I'm not sure how to handle this null in RPG. My DDS is defined as ALWNULL for the regionDesc field.
Here is how my data is returned if by State:
Code:
MMB null AL F 37 8 2 1 MMB null AL M 25 6 6 5 MMB null AR F 1 0 0 0 MMB null AR M 4 0 0 1



_Sort='R'. (But, maybe that's not a problem in your application.)
Comment