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 954D63A3B98 for ; Sun, 28 Nov 2004 13:45:51 +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 91526-06 for ; Sun, 28 Nov 2004 13:45:43 +0000 (GMT) Received: from md1.psixpress.com (unknown [154.32.105.205]) by svr1.postgresql.org (Postfix) with ESMTP id 8991A3A4FCF for ; Sun, 28 Nov 2004 13:45:44 +0000 (GMT) Received: from kaufbach.delug.de (23.pool1.dialup.cybernet-ag.de [195.143.124.23]) by md1.psixpress.com (MOS 3.4.4-GR) with ESMTP id CDK04530; Sun, 28 Nov 2004 13:45:40 GMT Received: from kretschmer by kaufbach.delug.de with local (Exim 3.35 #1) id 1CYPNG-0001Lb-00 for pgsql-sql@postgresql.org; Sun, 28 Nov 2004 14:45:58 +0100 Date: Sun, 28 Nov 2004 14:45:58 +0100 From: Kretschmer Andreas To: PostgreSQL SQL Subject: Re: [despammed] update/insert data Message-ID: <20041128134558.GA5142@kaufbach.delug.de> References: <20041127175540.M63399@narrowpathinc.com> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20041127175540.M63399@narrowpathinc.com> User-Agent: Mutt/1.3.28i X-OS: Debian/GNU Linux - weil ich es mir Wert bin! X-GPG-Fingerprint: EE16 3C01 7B9C 10F7 2C8B 3B86 4DB3 D9EE 7F45 84DA X-Message-Flag: "Windows" is not the answer. "Windows" is the question and the answer is "no"! X-Lugdd: Gerd Kube X-Info: My name is root. Just root. And I am licensed to kill -9 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/279 X-Sequence-Number: 19597 am Sat, dem 27.11.2004, um 12:55:40 -0500 mailte Keith Worthington folgendes: > Hi All, > > I have two tables in different schemas. The first table in the data_transfer > schema is loaded with a COPY command. I need to transfer the data to the > second schema inserting new records and updating existing records. What is > the best way to achieve this functionality? You can write a trigger-function. There you can do a insert/update for everey new records in the data_transfer - table. A simple example: I hava 2 tables: ,---- | test_db=# \d namen; | Tabelle »public.namen« | Spalte | Typ | Attribute | ----------+-------------------+------------------------------------------------------- | id | integer | not null default nextval('public.namen_id_seq'::text) | vorname | character varying | | nachname | character varying | | Trigger: | trig1 BEFORE INSERT OR UPDATE ON namen FOR EACH ROW EXECUTE PROCEDURE trigg1() | | test_db=# \d namen2; | Tabelle »public.namen2« | Spalte | Typ | Attribute | ----------+-------------------+----------- | id | integer | | vorname | character varying | | nachname | character varying | | `---- And this trigger-function: ,---- | create or replace function trigg1() returns trigger as' | begin | insert into namen2 values (NEW.id, NEW.vorname, NEW.nachname); | return NEW; | end; | ' language plpgsql; `---- There isn't a check for update, but this is also possible. ,---- | test_db=# select * from namen; | id | vorname | nachname | ----+---------+---------- | (0 Zeilen) | | test_db=# select * from namen2; | id | vorname | nachname | ----+---------+---------- | (0 Zeilen) `---- And i have a file: ,---- | kretschmer@kaufbach:~$ cat input.txt | copy "namen" from stdin; | 10 Magdalena Kretschmer | 11 Katharina Kretschmer | kretschmer@kaufbach:~$ `---- ,---- | test_db=# \i input.txt | test_db=# select * from namen; | id | vorname | nachname | ----+-----------+------------ | 10 | Magdalena | Kretschmer | 11 | Katharina | Kretschmer | (2 Zeilen) | | test_db=# select * from namen2; | id | vorname | nachname | ----+-----------+------------ | 10 | Magdalena | Kretschmer | 11 | Katharina | Kretschmer | (2 Zeilen) `---- sorry about my bad english. -- Diese Message wurde erstellt mit freundlicher Unterstützung eines freilau- fenden Pinguins aus artgerechter Freilandhaltung. Er ist garantiert frei von Micro$oft'schen Viren. (#97922 http://counter.li.org) GPG 7F4584DA Was, Sie wissen nicht, wo Kaufbach ist? Hier: N 51.05082°, E 13.56889° ;-)