tags:

views:

36

answers:

1

Hi,

Iam new to postgres,i wrote a trigger which is not working kindly look into it and suggest what went wrong.

In this trigger my intention was to update the order column if the new record was inserted in between the order already existed.

CREATE OR REPLACE FUNCTION client_template.check_attribute_order() RETURNS trigger AS $BODY$
DECLARE
    att_data_row client_template.attribute_base;
    max_id bigint;
    aid bigint;

BEGIN
    SELECT INTO max_id MAX("order") from client_template.attribute_base WHERE  entity_type_id = NEW.entity_type_id;

    IF NEW.order > 0 THEN 
             UPDATE client_template.attribute_base 
             SET "order" = "order"+ 1
             WHERE "order" BETWEEN  1 and 5 and entity_type_id = NEW.entity_type_id;
             INSERT INTO client_template.attribute_base values(NEW.*);
    END IF;

    IF (TG_OP = 'INSERT') THEN
          IF NEW.order = 0 THEN 
             NEW.order := max_id+1;
             INSERT INTO client_template.attribute_base values(NEW.*);
             RETURN NEW;
          END IF;
     END IF;    



     RETURN NEW;
END;
$BODY$ LANGUAGE 'plpgsql' VOLATILE COST 100;


ALTER FUNCTION client_template.check_attribute_order() OWNER TO postgres;
+1  A: 

What is the specific error you get?

Joshua D. Drake