psql过程函数无法识别列

时间:2015-03-19 06:07:50

标签: sql database psql

我有一张桌子" b2s"

 Column   |         Type          | Modifiers 
 -----------+-----------------------+-----------
id        | character varying(50) | not null
list      | character varying     | not null
issue     | date                  | not null
due       | date                  | 

1.每当我们创建一个新行时,我想比问题列多30天自动生成到期列

  

触发器:

b2sinsert 
BEFORE INSERT OR UPDATE ON b2s 
FOR EACH ROW EXECUTE PROCEDURE dueupdate()

以下是程序:

BEGIN NEW.due = (issue + INTERVAL '30 days'); NEW.issue = issue; NEW.list = list; NEW.id = id; RETURN NEW; END;

当我尝试在表中插入元素时,出现以下错误:

ERROR:  column "issue" does not exist
LINE 1: SELECT (issue + INTERVAL '30 days')
            ^
QUERY:  SELECT (issue + INTERVAL '30 days')
CONTEXT:  PL/pgSQL function dueupdate() line 1 at assignment

1 个答案:

答案 0 :(得分:1)

好吧 - 如果你在触发器中 - 你必须始终寻址(触发触发器的表格的列),为它们添加NEW.OLD.元组类型(情况无关紧要) 此外 - 您不必更改所有值。为了你的目的,这应该足够了:

BEGIN NEW.due = (NEW.issue + INTERVAL '30 days'); END;

请注意,影响NEW元组仅适用于BEFORE UPDATE触发器!