2

I have two tables A and B. A has the following columns : a_name, a_num, a_addr. B also has these columns.

Whenever I insert a record in A, I want that record to be inserted in B as well by the use of a trigger.

1 Answer 1

1
CREATE OR REPLACE FUNCTION trigger()
  RETURNS trigger AS
$BODY$
begin
if tg_op='INSERT' then
insert into b values (new.a_name ,new.a_num ,new.a_addr);
return new;
end if;
return null;
end;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

---------------------------
CREATE TRIGGER trigger_a
  AFTER INSERT OR UPDATE
  ON a
  FOR EACH ROW
  EXECUTE PROCEDURE trigger();
Sign up to request clarification or add additional context in comments.

3 Comments

Hi Anand. Thank your very much for your response. It makes total sense now. But, i get an error when creating the trigger function as here ERROR: syntax error at or near "begin" LINE 10: begin
i have edited answer again .kindly go through again.
Great. it's working.. But, there's another slight mistake in your insert query, which is to separate values by comma. Thanks @Anand

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.