插入触发器SQL:缺少表的FROM子句条目

时间:2015-07-29 11:26:09

标签: sql postgresql triggers sql-insert

我正在使用Postgres,而我正在尝试创建一个触发器,用于在表格中插入或更新新值。

以下是触发器和功能:

create or replace function trigf1() returns trigger as $$
begin
if (ballotbox.totvoters>votes.nofvotes) then 
raise notice 'more voters than allowed';
return old;
else return new; 
end if;
end;
$$ language plpgsql;

create trigger T1 
Before insert or update on votes
for each row 
execute procedure trigf1();

当我试图更新表格"投票"和" ballotBox"我收到了错误:

ERROR:  missing FROM-clause entry for table "ballotbox"
LINE 1: SELECT (ballotbox.totvoters > votes.nofvotes)
                ^
QUERY:  SELECT (ballotbox.totvoters > votes.nofvotes)
CONTEXT:  PL/pgSQL function trigf1() line 3 at IF

我不知道是否需要它,但这里是创建表和插入值:

create table ballotBox
(bno integer,
cid numeric(4,0),
street varchar(20),
hno integer,
totvoters integer,
primary key (bno),
foreign key (cid) references city);

create table votes
(cid numeric(4,0),
bno integer,
pid numeric(3,0),
nofvotes integer,
foreign key (cid) references city,
foreign key (bno) references ballotBox,
foreign key (pid) references party,
check (nofvotes >= 0));

insert into ballotBox values
(1, 1, 'street1', 10, 1500),
(2, 1, 'street2', 15, 490),
(3, 1, 'street2', 15, 610),
(4, 1, 'street2', 15, 650),
(5, 2, 'street3', 10, 900),
(6, 2, 'street3', 55, 800),
(7, 2, 'street4', 67, 250),
(8, 2, 'street4', 67, 990),
(9, 2, 'street5', 5, 600),
(10, 3, 'street1', 72, 1000),
(11, 3, 'street6', 25, 610),
(12, 3, 'street6', 25, 600),
(13, 4, 'street2', 3, 550),
(14, 4, 'street7', 15, 500),
(15, 5, 'street8', 44, 1100),
(16, 5, 'street9', 7, 710),
(17, 5, 'street10', 13, 950);

insert into votes values
(1, 1, 200, 100),
(1, 1, 210, 220),
(1, 1, 220, 2),
(1, 1, 230, 400),
(1, 1, 240, 313),
(1, 1, 250, 99),
(2, 1, 200, 55),
(2, 1, 210, 150),
(2, 1, 220, 2),
(2, 1, 230, 16),
(2, 1, 240, 210);

1 个答案:

答案 0 :(得分:-1)

试试这个

if ((select totvoters from ballotbox) > (selecct nofvotes from votes))