Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

226
Views
Using a procedure to update Column 2 in a Table A, by checking if column 1 value is present in a column in Table B [PostgreSQL]

I have two tables as follows:

Table A: Name, ID, value

Table B: ID, title

TableA.value is dependent on whether TableA.ID is present in TableB.ID.

I am trying to create a procedure and trigger so that whenever TableB is modified, the procedure is triggered to check if TableA.ID is in TableB.ID and sets TableA.value to 10.

I am using the following code and getting an error:

CREATE PROCEDURE update1
LANGUAGE SQL
AS $$
UPDATE tablA as a
SET a.value = 10
WHERE a.ID EXISTS ( SELECT b.ID 
                            FROM tableB as b
                           WHERE a.ID = b.ID)
$$;

I am an SQL noob, and this is the first time I am trying to use a procedure.

UPDATE

I was able to create a procedure which does the job when I manually run it using CALL

However, it does not have a RETURNS TRIGGER block within it. Adding that returns the error Procedure cannot return triggers.

If I create a trigger as follows

CREATE TRIGGER b_trigger
AFTER UPDATE OR INSERT ON b.ID
FOR EACH STATEMENT
EXECUTE PROCEDURE update1();

However, this returns the following

ERROR: function columbia_deli.manager_discount must return type trigger

SQL state: 42P17

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Try this using plpgsql Language:

Trigger Function:

CREATE OR REPLACE FUNCTION update1()
  RETURNS trigger AS
$BODY$
 
begin
update "TableA" set value= 10 where id =new.id;

return NEW;
end;

$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

Then create trigger on TableB for after update event

CREATE TRIGGER trig_tableb
  AFTER UPDATE
  ON "TableB"
  FOR EACH ROW
  EXECUTE PROCEDURE update1();
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!