ERROR: column "exists" does not exist in SQL insert command
ERROR: column "exists" does not exist in SQL insert command
我想稍微了解一下触发器,我想制作一个触发器来检查我是否在我的预测 table 中添加了由 (city,eventype,casualties)[= 组成的新行16=]
如果我的新行伤亡人数高于城市人口,我将得到一个错误,并且新行不会插入到我的预测中table
到目前为止,这是我的代码
正在创建我的触发器
create trigger T1
before insert on prediction
for each row
execute procedure trigf1();
正在创建 trigf1();
create or replace function trigf1() returns trigger as $$
declare bad_population record;
Begin
select city.cname INTO bad_population
from city
where new.casualties>city.population and new.cname=city.cname;
if exists bad_population then begin
raise notice 'bad population-more casualites than city population';
return null;
end;
else
return new;
end if;
end;
$$language plpgsql;
当我尝试输入这些值时出现错误
insert into prediction values ('Naples', 'Volcano', 3056)
这就是错误
ERROR: column "exists" does not exist
LINE 1: SELECT exists bad_population
^
QUERY: SELECT exists bad_population
CONTEXT: PL/pgSQL function trigf1() line 7 at IF
SQL state: 42703
我的城市Table
我的预测table
希望您能帮助我理解错误并解决我的问题
使用FOUND
:IF FOUND THEN
。
如果您的查询有任何结果,FOUND
将为 TRUE,否则为 FALSE。
EXISTS
根据查询是否 returns 任何结果接受 returns TRUE 或 FALSE 的查询,例如IF EXISTS (SELECT 1 FROM table WHERE condition) THEN...
所以主要区别是 FOUND
是一个变量,它设置在你的 query/statement returns 之后,并根据你的 [=35] 是否设置为 TRUE/FALSE =] 语句返回了任何结果,或者您的 UPDATE/DELETE 语句影响了任何行。
另一方面,EXISTS
是一个运算符 returns TRUE/FALSE 基于您当时提供给它的查询。
我想稍微了解一下触发器,我想制作一个触发器来检查我是否在我的预测 table 中添加了由 (city,eventype,casualties)[= 组成的新行16=]
如果我的新行伤亡人数高于城市人口,我将得到一个错误,并且新行不会插入到我的预测中table
到目前为止,这是我的代码
正在创建我的触发器
create trigger T1
before insert on prediction
for each row
execute procedure trigf1();
正在创建 trigf1();
create or replace function trigf1() returns trigger as $$
declare bad_population record;
Begin
select city.cname INTO bad_population
from city
where new.casualties>city.population and new.cname=city.cname;
if exists bad_population then begin
raise notice 'bad population-more casualites than city population';
return null;
end;
else
return new;
end if;
end;
$$language plpgsql;
当我尝试输入这些值时出现错误
insert into prediction values ('Naples', 'Volcano', 3056)
这就是错误
ERROR: column "exists" does not exist
LINE 1: SELECT exists bad_population
^
QUERY: SELECT exists bad_population
CONTEXT: PL/pgSQL function trigf1() line 7 at IF
SQL state: 42703
我的城市Table
我的预测table
希望您能帮助我理解错误并解决我的问题
使用FOUND
:IF FOUND THEN
。
如果您的查询有任何结果,FOUND
将为 TRUE,否则为 FALSE。
EXISTS
根据查询是否 returns 任何结果接受 returns TRUE 或 FALSE 的查询,例如IF EXISTS (SELECT 1 FROM table WHERE condition) THEN...
所以主要区别是 FOUND
是一个变量,它设置在你的 query/statement returns 之后,并根据你的 [=35] 是否设置为 TRUE/FALSE =] 语句返回了任何结果,或者您的 UPDATE/DELETE 语句影响了任何行。
EXISTS
是一个运算符 returns TRUE/FALSE 基于您当时提供给它的查询。