X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 2121E52889 for ; Tue, 19 Jul 2005 06:17:54 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 73654-09 for ; Tue, 19 Jul 2005 09:17:50 +0000 (GMT) Received: from ar-sd.net (unknown [82.77.155.72]) by svr1.postgresql.org (Postfix) with ESMTP id 106595288D for ; Tue, 19 Jul 2005 06:17:49 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by ar-sd.net (Postfix) with ESMTP id 2896928C79; Tue, 19 Jul 2005 12:09:25 +0300 (EEST) Received: from ar-sd.net ([127.0.0.1]) by localhost (linz [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 16010-02; Tue, 19 Jul 2005 12:09:24 +0300 (EEST) Received: from forge (unknown [192.168.0.11]) by ar-sd.net (Postfix) with SMTP id 3FC2B28B92; Tue, 19 Jul 2005 12:09:24 +0300 (EEST) Message-ID: <01b101c58c43$08405b20$0b00a8c0@forge> From: "Andy" To: "daq16" Cc: References: <00e001c58b93$ad30b130$0b00a8c0@forge> <1285936906.20050718153208@ugyvitelszolgaltato.hu> Subject: Re: Create trigger for auto update function >> SOLVED!!! Date: Tue, 19 Jul 2005 12:19:57 +0300 MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=original Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2900.2180 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 X-Virus-Scanned: by amavisd-new at ar-sd.net X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.655 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO, PLING_PLING, UPPERCASE_25_50 X-Spam-Level: X-Archive-Number: 200507/160 X-Sequence-Number: 22138 CREATE OR REPLACE FUNCTION u9() RETURNS TRIGGER AS' BEGIN NEW.pass_md5=md5(NEW.pass); return NEW; END 'language plpgsql; CREATE TRIGGER t8 BEFORE INSERT OR UPDATE ON hoy FOR EACH ROW EXECUTE PROCEDURE u9(); Ok. This is the solution. It works well, for inserts and updates. Took some time to figure it out. (ignore the function names --- test functions) Best regards, Andy. ----- Original Message ----- From: "daq" To: "Andrei Bintintan" Cc: Sent: Monday, July 18, 2005 4:32 PM Subject: Re: [SQL] Create trigger for auto update function > Hello Andrei, > > Monday, July 18, 2005, 2:24:41 PM, you wrote: > > AB> Hi to all, > > AB> I have a table: > AB> create table hoy( > AB> id serial, > AB> pass varchar(40), > AB> pass_md5 varchar(40); > > AB> Now, I want to write a trigger function that automatically updates the > pass_md5 with the md5 function of the pass. > > AB> I tried this: > > AB> CREATE FUNCTION update_pass(integer) RETURNS integer AS $$ > AB> UPDATE hoy SET pass_md5=md5(pass) WHERE id=$1; > AB> SELECT 1; > AB> $$ LANGUAGE SQL; > > AB> and > > AB> CREATE TRIGGER triger_users_pass_md5 > AB> AFTER INSERT OR UPDATE > AB> ON hoy > AB> EXECUTE PROCEDURE update_pass(integer); > > What will be the param of the trigger procedure? > > Try this way: > > CREATE FUNCTION update_pass() RETURNS integer AS $$ > UPDATE hoy SET pass_md5=md5(pass) WHERE id=new.id; > SELECT 1; > $$ LANGUAGE SQL; > > CREATE TRIGGER triger_users_pass_md5 > AFTER INSERT OR UPDATE > ON hoy FOR EACH ROW > EXECUTE PROCEDURE update_pass; > > > DAQ > > > ---------------------------(end of broadcast)--------------------------- > TIP 9: In versions below 8.0, the planner will ignore your desire to > choose an index scan if your joining column's datatypes do not > match > >