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 2FB173A3D39 for ; Wed, 3 Nov 2004 03:26:10 +0000 (GMT) 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 92826-05 for ; Wed, 3 Nov 2004 03:25:59 +0000 (GMT) Received: from out014.verizon.net (out014pub.verizon.net [206.46.170.46]) by svr1.postgresql.org (Postfix) with ESMTP id B76DA3A2B4A for ; Wed, 3 Nov 2004 03:26:01 +0000 (GMT) Received: from [192.168.1.2] ([68.239.122.175]) by out014.verizon.net (InterMail vM.5.01.06.06 201-253-122-130-106-20030910) with ESMTP id <20041103032602.VFRY25088.out014.verizon.net@[192.168.1.2]> for ; Tue, 2 Nov 2004 21:26:02 -0600 Subject: Insert/Update Perl Function involving two tables needing to by 'in sync' From: Ferindo Middleton Jr Reply-To: fmiddleton@verizon.net To: pgsql-sql@postgresql.org Content-Type: text/plain Organization: Sleek Collar Message-Id: <1099452361.1738.27.camel@scnmaster.sleekcollar.com> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.4.6 (1.4.6-2) Date: Tue, 02 Nov 2004 22:26:01 -0500 Content-Transfer-Encoding: 7bit X-Authentication-Info: Submitted using SMTP AUTH at out014.verizon.net from [68.239.122.175] at Tue, 2 Nov 2004 21:26:01 -0600 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200411/22 X-Sequence-Number: 19341 I am trying to write a Perl Function for one of the databases I'm building a web application for. This function is triggered to occur BEFORE INSERT OR UPDATE. This function is complex in that it involves fields in two different tables which need to be updated, where the updates one receives depend on and must actually also reflect the same data entered into one of the tables explicitly by the user. I basically want to make sure that certain Same fields in two tables are automatically updated with the exact Same information. This is redundant but it is the design our engineers came up with and management is forcing me to work with this redundant design. I've been trying to I've been trying to tweak this function using "new." & "old." prefixes for the fields that I'm manipulating but it doesn't work. I've attached my function below. Any input on how this logic is wrong is appreciated. See the code below. CREATE FUNCTION classdata_scheduleid_sync() -- the purpose of this function is to make sure that when a schedule_id -- is updated in the registration_and_attendance table, that the -- class_id start_date, & end date fields in this table are -- automatically updated with the corresponding class_id, start_date, -- end_date from the schedules table RETURNS trigger AS 'DECLARE schedule_info RECORD; BEGIN IF length(new.schedule_id) = 0 -- IS THIS AN INSERT OR UPDATE? THEN -- IS AN UPDATE FOR existing schedule_id IS NULL SELECT INTO schedule_info * FROM schedules WHERE id = old.schedule_id; UPDATE registration_and_attendance SET class_id = schedule_info.class_id, start_date = schedule_info.start_date, end_date = schedule_info.end_date WHERE id = old.id; ELSE -- must have been an INSERT SELECT INTO schedule_info * FROM schedules WHERE id = new.schedule_id; INSERT INTO registration_and_attendance(class_id, start_date, end_date) VALUES (schedule_info.class_id, schedule_info.start_date, schedule_info.end_date) WHERE schedule_id = new.schedule_id; END IF; RETURN new; END;' LANGUAGE 'plpgsql';