R
R
ReD2016-07-24 13:54:00
PostgreSQL
ReD, 2016-07-24 13:54:00

Inserting data into PostgreSQL table ends with an error?

Understanding trigger functions.
For this, a simple table was first created and a function was added:

CREATE TABLE AplusB (A integer, B integer);
INSERT INTO AplusB VALUES (1,1);
INSERT INTO AplusB VALUES (2,2);
INSERT INTO AplusB VALUES (3,3);
CREATE FUNCTION plus(integer, integer) RETURNS integer
LANGUAGE SQL AS 'SELECT $1 + $2;';

After that, another table and a trigger function were created:
CREATE TABLE ABresult (result integer);
CREATE FUNCTION ABsumm() returns TRIGGER AS
'BEGIN
DELETE FROM ABresult;
INSERT INTO ABresult VALUES (AplusB.A+AplusB.B);
RETURN NULL;
END;'
LANGUAGE 'plpgsql';
CREATE TRIGGER makeABresult
AFTER INSERT OR UPDATE OR DELETE ON AplusB
FOR EACH STATEMENT execute procedure ABsumm();

After attempting to paste: Error occurs:
INSERT INTO AplusB VALUES (100,200);
ERROR: missing FROM-clause entry for table "aplusb"
LINE 1:INSERT INTO ABresult VALUES (AplusB.A+AplusB.B)
^ QUERY
: INSERT INTO ABresult VALUES (AplusB.A+AplusB.B)
CONTEXT: PL/pgSQL function "absumm" line 3 at SQL statement

What could be the problem?
(Source of examples: wiki.linuxformat.ru/wiki/LXF87-88:PostgreSQL)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
terrier, 2016-07-24
@trinitr0

INSERT INTO ABresult VALUES (AplusB.A+AplusB.B);
ERROR: missing FROM-clause entry for table "aplusb"
Quite rightly wants to see FROM:
INSERT INTO ABresult ( result )
SELECT A + B FROM AplusB;
Throw you this Linux format of yours ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question