为什么会出现这个运行时错误?
错误:列引用“到达”不明确第 6 行:case when ((cast('05:00' as time) >=arrive)
详细信息: 它可以引用 PL/pgSQL 变量或表列。
问题是,case 语句在查询中,我正在从一个表中进行选择,该表确实有一个名为“arrive”的列。我还没有声明一个名为到达的变量。为什么 PG 不能简单地检查一下是否没有声明这样的变量,并得出结论必须是对列的引用?
我附上了下面的代码。我看到的唯一表面上的冲突是这个函数返回的传出表的定义,其中有一个名为“到达”的列,以及在最终选择中针对临时表 TT 使用列名。
CREATE or replace function GetHourlyView
(v_whichDate date)
returns TABLE
(
stagecoach,
arrive time,
depart time,
t5am int,t6am int,t7am int,t8am int,t9am int,t10am int, t11am int,
t12pm int, t1pm int, t2pm int t3pm int,t4pm int,t5pm int,t6pm int,
t7pm int, t8pm int, t9pm int, t10pm int, t11pm int
)
as $body$
declare v_dow int := date_part('dow',v_whichDate);
begin
drop table if exists TT;
create temp table TT
(stagecoach varchar(25),
arrive time,
depart time,
t5am int,t6am int,t7am int,t8am int,t9am int,t10am int, t11am int,
t12pm int, t1pm int, t2pm int t3pm int,t4pm int,t5pm int,t6pm int,
t7pm int, t8pm int, t9pm int, t10pm int, t11pm int
) without OIDS on commit drop;
insert into TT
select * from
GetDailySchedule( v_whichDate);
-- (arrive=depart) means 'cancelled'
delete from TT where TT.arrive=TT.depart;
return QUERY
select
TT.stagecoach,
arrive,
depart,
case when ( (cast('05:00' as time) >= arrive) and (cast('05:00' as time) < depart )) then 1 else 0 end as t5am,
case when ( (cast('06:00' as time) >= arrive) and (cast('06:00' as time) < depart )) then 1 else 0 end as t6am,
<snip>
.
.
.
case when ( (cast('23:00' as time) >= arrive) and (cast('23:00' as time) < depart )) then 1 else 0 end as t11pm
from TT
;
drop table TT;
end
$body$
LANGUAGE 'plpgsql'