From: | "Leonel Nunez" <lnunez(at)enelserver(dot)com> |
---|---|
To: | "pablo" <pabloste(at)gmail(dot)com> |
Cc: | pgsql-es-ayuda(at)postgresql(dot)org |
Subject: | Re: Como pasar datos de una tabla a otra internamente o por aplicacion pero sin muchas consultas |
Date: | 2006-12-09 19:14:16 |
Message-ID: | 1816.189.155.152.139.1165691656.squirrel@enelserver.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-es-ayuda |
> HOLA.
>
> Tengo una tabla "tabla1" y otra tabla "tabla2".
>
> Cargo datos en la tabla2, despues de validarlos deseo mandar todo lo que
> está en tabla2 a tabla1, pero teniendo cuidado en que si un registro ya
> existe en tabla1 hago un update a ese registro y si no existe hago un
> insert.
>
> Lo tengo hecho haciendo una consulta previa por registro para ver si
> existo
> y ahí ejecuto el insert o update.
>
> Pero son cientos de miles y estaría haciendo 2 consultas por cada registro
> que existe en tabla 1.
>
> existira alguna forma de hacerlo automaticamente??
>
> Alguien me puede orientar?
>
> gracias
>
>
algo asi ?
leonel=> create table tabla1 ( id int primary key unique , de varchar
);NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index
"tabla1_pkey" for table "tabla1"
CREATE TABLE
leonel=> create table tabla2 ( id int primary key unique , de varchar );
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index
"tabla2_pkey" for table "tabla2"
CREATE TABLE
leonel=> insert into tabla2 values ( 1 , 'aa');
INSERT 0 1
leonel=> insert into tabla2 values ( 2 , 'bb');
INSERT 0 1
leonel=> insert into tabla2 values ( 3 , 'cc');
INSERT 0 1
leonel=> insert into tabla1 ( select * from tabla2 );
INSERT 0 3
leonel=> delete from tabla2;
DELETE 3
leonel=> insert into tabla2 values ( 3 , 'fffffff');
INSERT 0 1
leonel=> update tabla1 set de=t2.de from tabla2 as t2 where tabla1.id
=t2.id;UPDATE 1
leonel=> select * from tabla1;
id | de
----+---------
1 | aa
2 | bb
3 | fffffff
(3 rows)
Leonel
From | Date | Subject | |
---|---|---|---|
Next Message | Oscar Daniel Molina Galindo | 2006-12-09 20:52:57 | problemas con la instalacion |
Previous Message | Mario | 2006-12-09 18:49:50 | Re: expresion regular e-mail |