X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 6E9DE9DC8A7 for ; Tue, 13 Dec 2005 19:06:15 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 57423-09 for ; Tue, 13 Dec 2005 19:06:16 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from mout.perfora.net (mout.perfora.net [217.160.230.41]) by postgresql.org (Postfix) with ESMTP id 9AFD89DC804 for ; Tue, 13 Dec 2005 19:06:12 -0400 (AST) Received: from [68.146.21.5] (helo=[192.168.0.100]) by mrelay.perfora.net (node=mrelayus1) with ESMTP (Nemesis), id 0MKp2t-1EmJ8y33oq-0002pI; Tue, 13 Dec 2005 18:01:14 -0500 Message-ID: <439F527F.4050204@shaw.ca> Date: Tue, 13 Dec 2005 16:00:15 -0700 From: Daniel Hertz Reply-To: danielhertz@shaw.ca User-Agent: Mail/News 1.5 (Windows/20051206) MIME-Version: 1.0 To: "postgreSQL (SQL)" Subject: Multi-row update w. plpgsql function Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: perfora.net abuse@perfora.net login:cf1ae69d888cbe19591da5d2fe7fee64 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0 required=5 tests=[none] X-Spam-Score: 0 X-Spam-Level: X-Archive-Number: 200512/106 X-Sequence-Number: 23648 Given a set of checkbox values that are submitted through an html form, how do you loop through the submitted values to update more than one row in a table? Imagine a table called 'message_table': mid | message | status ----+---------+------- 1 | Text1 | H 2 | Text2 | H 3 | Text3 | H 4 | Text4 | H A web page presents the user with all messages flagged with 'H'. User checks messages 1,3 and 4 and submits form. (i.e. approved=1&approved=3&approved=4) After performing postgreSQL update, rows 1, 3 and 4 would be updated to: mid | message | status ----+---------+------- 1 | Text1 | A 2 | Text2 | H 3 | Text3 | A 4 | Text4 | A I have never written a plpgsql function, but tried: CREATE OR REPLACE FUNCTION update_messages(approved integer) RETURNS integer AS $body$ DECLARE new_status varchar; new_sample record; BEGIN new_status := 'A'; FOR new_sample IN SELECT * FROM message_table WHERE status='H' ORDER BY mid LOOP UPDATE message_table SET status = new_status WHERE mid = approved; END LOOP; RETURN 1; END; $body$ LANGUAGE 'plpgsql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER; I call the function with: SELECT update_messages(); I'm using apache cocoon, which is why you see the variable placeholder: ); Unfortunately, the function only updates the first value submitted (mid 1), and doesn't loop through the other two values submitted. Can someone help this novice from getting ulcers? Thanks for your help! Daniel