我正在使用触发器和函数将值插入另一个表中。
我有这张桌子 1)展
exhid exhname description strtdate enddate status
101 The Famous BLAH BLAH 2013-07-15 2013-10-13 SOON
102 York Exhibition BLAH BLAH 2013-08-07 2014-01-19 End
103 Fine ART BLAH BLAH 2014-09-26 2015-03-21 SOON
2)Works_Exhibitions
alphid numberid exhid
SCFI 1007 101
SCBU 1008 101
PAHF 1002 103
PAHE 1003 103
PASP 1004 103
和第三个空表,它与Works_Exhibitions具有相同的属性,并将其命名为Temp_works_exhibitions
alphid numberid exhid
我确实创建了这个功能并触发,所以一旦我将展览桌上的任何展览的状态更新为'结束',我想将该展览的所有作品复制到新表Temp_works_exhibitions中然后删除它来自Works_exhibition表
CREATE OR REPLACE FUNCTION add2Temp_works_exhibitions() returns TRIGGER AS $updExhibStat$
BEGIN
IF(NEW.Status = 'End') THEN
INSERT INTO temp_works_exhibitions
select
Works_Exhibitions.alphID,
Works_Exhibitions.numberID,
Works_Exhibitions.exhID
from
Works_Exhibitions
where
Works_Exhibitions.exhID = (select Exhibitions.exhID from Exhibitions where Exhibitions.Status = 'End');
delete from Works_Exhibitions where Works_Exhibitions.exhID = (select Exhibitions.exhID from Exhibitions where Exhibitions.Status = 'End');
RETURN NEW;
END IF;
RETURN NULL;
END;
$updExhibStat$ LANGUAGE plpgsql;
--CREATE TRIGGER updExhibStat AFTER UPDATE ON Exhibitions FOR EACH ROW EXECUTE PROCEDURE add2Temp_works_exhibitions();
当我第一次测试时它确实有效但当我试图更新新的展览时我得到了这个错误
ERROR: more than one row returned by a subquery used as an expression
CONTEXT: SQL statement "INSERT INTO temp_works_exhibitions
select
Works_Exhibitions.alphID,
Works_Exhibitions.numberID,
Works_Exhibitions.exhID
from
Works_Exhibitions
where
Works_Exhibitions.exhID = (select Exhibitions.exhID from Exhibitions where Exhibitions.Status = 'End')"
PL/pgSQL function add2temp_works_exhibitions() line 5 at SQL statement
任何想法如何解决此问题
答案 0 :(得分:0)
我想我确实解决了这个问题
我确实在函数的末尾添加了这个命令
UPDATE Exhibitions set Status = 'Expired' where Exhibitions.Status = 'End';
我想这不是一个好的设计,但我没有其他解决方案