Wrox Home  
Search P2P Archive for: Go

  Return to Index  

sql_language thread: Insert Table1 Rows Into Table2 if Table1 Rows Don't Exist In Table2


Message #1 by rg1@h... on Tue, 2 Oct 2001 18:19:45
Thanks Luis. This way seems more efficient than mine with 2 selects.

-----Original Message-----
From: Luis Alvarez [mailto:luis_alvarez@y...]
Sent: Tuesday, October 02, 2001 2:34 PM
To: sql language
Subject: [sql_language] RE: Insert Table1 Rows Into Table2 if Table1
Rows Don't Exist In Table2


Rita:

Sorry, I believe is then in this way:

INSERT INTO Table2
>  SELECT Table1.Field1, 
>         Table1.Field2, 
>         Table1.Field3,
>  FROM Table1 INNER JOIN Table2 ON 
>         (Table2.Field1 = Table1.Field1 AND 
>          Table2.Field2 = Table1.Field2 AND 
>          Table2.Field3 = Table1.Field3)
> WHERE         Table1.Field1 = null AND 
>          Table1.Field2 = null AND 
>          Table1.Field3 = null

If it doesn't work try left join instead of inner.
I don't have the book with me but I am sure that it
works in this way.


--- Rita Greenberg <rg1@h...> wrote:
> Hi Luis. 
> 
> Thanks for your response. I tried that but nothing
> was inserted. 
> 
> Somebody mentioned using "NOT EXISTS" and the
> following code worked for me:
> INSERT INTO Table2
>  SELECT Table1.Field1,
>    Table1.Field2
>    Table1.Field3
>  FROM Table1
>  WHERE NOT EXISTS
>   (SELECT  Field1, Field2, Field3 FROM Table1 INNER
> JOIN
> Table2 ON
>          Table2.Field1 = Table1.Field1 AND 
>          Table2.Field2 = Table1.Field2 AND 
>          Table2.Field3 = Table1.Field3)
> Rita
> 
> -----Original Message-----
> From: Luis Alvarez [mailto:luis_alvarez@y...]
> Sent: Tuesday, October 02, 2001 12:08 PM
> To: sql language
> Subject: [sql_language] Re: Insert Table1 Rows Into
> Table2 if Table1
> Rows Don't Exist In Table2
> 
> 
> I had this problem once and believe it was solved
> like
> this. See the changes in the code.
> 
> --- rg1@h... wrote:
> > Hi.
> > 
> > I have Table1 rows I want to insert into Table2
> but
> > only those rows from 
> > Table1 that don't already exist in Table2.
> > 
> > I tried the following code but nothing got
> inserted
> > from Table1 into 
> > Table2:
> > INSERT INTO Table2
> >  SELECT Table1.Field1, 
> >         Table1.Field2, 
> >         Table1.Field3,
> >  FROM Table1 INNER JOIN Table2 ON 
> >         (Table2.Field1 = Table1.Field1 AND 
> >          Table2.Field2 = Table1.Field2 AND 
> >          Table2.Field3 = Table1.Field3)
> > WHERE         Table2.Field1 = null AND 
> >          Table2.Field2 = null AND 
> >          Table2.Field3 = null
> > I'm using SQL 7.0.
> > 
> > Any help would be greatly appreciated!
> > 
> > Rita
> > 
> > 



  Return to Index