SQL query to insert non existance data

Posted on 2014-08-12
Last Modified: 2014-09-02
I am exporting data from Google analytics to SQL database, while exporting data get duplicated if I the program runs many time in the same date range. luckily I can run the canvas of Google analytics data according to an SQL command. how can I write a query that input data from Google analytics if same  "date" was not existing because "date" is the only unique attribute in all the tables.
For instance when I run the program on 17-07-2014 it will go through the database tables check if the same date exist "17-07-2014" then nothing will happened but if it wasn't there new data will be inserted

Thank you
Question by:Rawan Jambi
    LVL 10

    Expert Comment

    You can use cursor with loop to check each record. Read
    While loop
    LVL 44

    Accepted Solution

    Not knowing the structure of the tables, your query should be something like:
    INSERT INTO TableName
    SELECT *
    FROM GoogleTable
    WHERE NOT EXISTS (SELECT 1 FROM TableName T2 WHERE T2.DateColName = GoogleTable.DateColName)

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
    The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
    This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
    Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now