Results 1 to 3 of 3

Thread: Comparing fields in tables

  1. #1
    Join Date
    Aug 2005
    Posts
    2

    Comparing fields in tables

    I am working with the article that MAK wrote on SecurityLogs http://www.databasejournal.com/featu...le.php/3515886

    I have completed this, but I have made some changes to the database (for normalization to 3NF purposes). I now have problems with a query.

    I am trying to "Insert a new record in a table if it does not already exist in the table". To try to clarify I perform the following query:

    INSERT INTO Tmp_Event
    SELECT DISTINCT EventID, EventType, EventTypeName from Tmp

    Which gives me the Tmp_Event table consisting of EventID's etc. (no duplicates). What I then want to do, is compare the 'Tmp_event' table and an already existing 'Event' table. These two tables are in fact identical. I would like to insert any records from 'Tmp_Event' into 'Event' if they do not already exist in 'Event'.

    This query gives me all records that do not exist in 'Event'

    SELECT EventID, EventType, EventTYpeName from Tmp_Event
    WHERE EventID NOT IN (SELECT EventID from Event)

    How can I change this query into performing an INSERT INTO Event as well?

    Hope this makes sense

    -Silia

  2. #2
    Join Date
    Nov 2002
    Location
    New Jersey, USA
    Posts
    3,932
    Simple


    INSERT INTO Tmp_Event
    SELECT EventID, EventType, EventTYpeName from Tmp_Event
    WHERE EventID NOT IN (SELECT EventID from Event)

  3. #3
    Join Date
    Aug 2005
    Posts
    2
    Thanks! That of course worked... However it should be

    'INSERT INTO Event' but hey, minor details...

    Silia

Posting Permissions

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