Help with NOT EXISTS query

Results 1 to 3 of 3

Thread: Help with NOT EXISTS query

  1. #1
    Join Date
    Dec 1969

    Default Help with NOT EXISTS query

    Hi!<BR><BR>I have the following two tables:<BR>TABLE1<BR>anID(PK),<BR>Column2<BR>TABLE 2:<BR>anID<BR>SomeCol<BR><BR>What i want to do is an INSERT into TABLE2, SELECTING from TABLE1 based on a WHERE clause.<BR>However, i fail to write the proper where statement so that if TABLE1.anID = TABLE2.anID = <BR>don&#039;t insert anything...<BR><BR>so...INSERT INTO TABLE2 when not TABLE2.anID = TABLE1.anID, this way i will only insert new records from<BR>TABLE1.<BR><BR>Thanks in advance.<BR>/Anders

  2. #2
    Eddie Campbell Guest

    Default RE: Help with NOT EXISTS query

    Try this<BR><BR>"INSERT INTO Table2(AnId, SomeCol) SELECT Table1.AnId, Table1.Column2 FROM Table1 LEFT JOIN Table2 ON Table1.AnId=Table2.AnId WHERE Table2.AnId Is Null"<BR><BR>This should put any data in table2 into table1 if a row with the matching AnId does NOT exist in table2

  3. #3
    Join Date
    Dec 1969

    Default RE: Thanks

    Works great!<BR>Thanks<BR>(A)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts