| From: | Rodrigo De León <rdeleonp(at)gmail(dot)com> |
|---|---|
| To: | "Mag Gam" <magawake(at)gmail(dot)com> |
| Cc: | pgsql-novice(at)postgresql(dot)org |
| Subject: | Re: need trigger help |
| Date: | 2007-11-20 16:19:09 |
| Message-ID: | a55915760711200819u77b7a7d4y24a9e45cb9766625@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-novice |
On Nov 20, 2007 10:59 AM, Mag Gam <magawake(at)gmail(dot)com> wrote:
> I want to write a trigger, for UPDATE/INSERT, do a ltrim and rtrim() of name
> value. I want to remove all the beginning and ending blank spaces.
The PostgreSQL docs are really good:
http://www.postgresql.org/docs/8.2/static/plpgsql-trigger.html
Base on the examples from the docs, you could do:
CREATE TABLE T(
ID INTEGER,
NAME TEXT
);
CREATE OR REPLACE FUNCTION T_F() RETURNS TRIGGER AS $$
BEGIN
IF TG_OP IN ('UPDATE','INSERT') THEN
NEW.NAME = TRIM(NEW.NAME);
END IF;
RETURN NEW;
END;
$$ LANGUAGE PLPGSQL;
CREATE TRIGGER T_T
BEFORE INSERT OR UPDATE ON T
FOR EACH ROW EXECUTE PROCEDURE T_F();
-- TEST
INSERT INTO T VALUES (1, ' A ');
SELECT * FROM T;
Good luck.
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Robert Bernabe | 2007-11-21 08:14:40 | Using Execute with Dynamic Raise Commands |
| Previous Message | Mag Gam | 2007-11-20 15:59:19 | need trigger help |