假设您不想修改现有的存储过程,而是想“装饰”存储过程的输出,则需要执行以下操作:
execute block
returns (
drink varchar(50),
"Drink for print" varchar(50),
name varchar(50))
as
declare drink_intermediate varchar(50);
declare name_intermediate varchar(50);
begin
for select drink, name
from drinks
order by drink
into drink_intermediate, name_intermediate
do
begin
if (drink is distinct from drink_intermediate) then
begin
drink = drink_intermediate;
"Drink for print" = drink_intermediate;
name = null;
suspend;
"Drink for print" = null;
end
name = name_intermediate;
suspend;
end
end
也就是说,每次更换饮料时,您都需要添加一个额外的行。
你也可以在 DSQL 中做这样的事情,但是当你有很多行时,这可能会更昂贵,因为distinct
:
select distinct
drink,
case a.type when 1 then drink end as "Drink for print",
case a.type when 2 then name end as name
from drinks
cross join (
select 1 as type from RDB$DATABASE
union all
select 2 as type from RDB$DATABASE
) a
order by drink, type
也就是说,对于存储过程的每一行,我们添加一个额外的行,其中填充的"Drinks for print"
列和name
未填充的列,然后我们取一个不同的(删除重复的行),并排序以获得一致的输出。