You procedure has 2 major problems. In order to return values a procedure must include an OUT or INOUT parameter. Secondly a Select
inside a plpgsql block must include the into clause. Further this parameter must match number of columns and data type of each column. For what you have this means creating a user defined data type (UDT). The results then becomes: (Assumed a definition of table routes
since it was not provided.)
create type routes_category as (flight_num integer
,distance_miles integer
,from_city text
,to_city text
,flight_time interval
,category text
);
create or replace procedure distances(in flight_num1 integer
,out flight_dtl routes_category
)
language 'plpgsql'
as $body$
begin
select
*,
case
when distance_miles >= 0 and distance_miles <= 2000 then 'short distance travel (SDT)'
when distance_miles >= 2000 and distance_miles <= 6500 then 'Intermediate distance travel (IDT)'
when distance_miles > 6500 then 'Long distance travel (LDT)'
end as category
into flight_dtl
from
routes
where
flight_num = flight_num1;
end;
$body$;
But why are you using a Procedure
at all, it does nothing but retrieve data. If this is actually the case you should use a function. You still need to define the appropriate return type, either as above or as the return type
as table. If it contains just SQL then define it as a SQL function.
create or replace function distances_fun(in flight_num1 integer)
returns table(flight_num integer
,distance_miles integer
,from_city text
,to_city text
,flight_time interval
,category text
)
language sql
as $body$
select
*,
case
when distance_miles >= 0 and distance_miles <= 2000 then 'short distance travel (SDT)'
when distance_miles >= 2000 and distance_miles <= 6500 then 'Intermediate distance travel (IDT)'
when distance_miles > 6500 then 'Long distance travel (LDT)'
end as category
from
routes
where
flight_num = flight_num1;
$body$;
Demo for each here.
Unfortunately, db<>fiddle does not show results from raise notice ...
. That would have been:
Flight 1111 is short distance travel (SDT)
Flight 1122 is short distance travel (SDT)