Need Help with insert and update using sql server 2005

  • Hi,

    I have two tables namely result and temp. Both have the same fields. The primary key is Emp_ID. Based on Emp_ID I need to insert or update result table from temp table. Please anyone could help me with the code in doing this? I am using SQL Server 2005.

    Thanks for your help.

    Regards,

    Hema

  • Two basic concepts I use:

    1) if you want to insert just one row:

    IF EXISTS (SELECT 1 FROM targetTable t WHERE t.ID = @varId)

    UPDATE

    ELSE

    INSERT

    2) If you want to upsert (update or insert) multiple rows from another table:

    UPDATE target

    SET columns

    FROM target t

    INNER JOIN source s

    ON t.id = s.id

    --followed by

    INSERT INTO target (column list)

    SELECT source list

    FROM source s

    LEFT OUTER JOIN target t

    ON s.id = t.id

    WHERE t.id IS NULL

    (Side note: don't do it the other way around -INSERT then UPDATE- since you would update the rows you just inserted...)

    With SS2K8 the MERGE statement seems to make that task more efficient since you won't have to use two statements...



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • i get an error at from with the update statment.

    can't we do this with a single query? stored procedure?

  • hemagkv (8/11/2010)


    i get an error at from with the update statment.

    can't we do this with a single query? stored procedure?

    You can do this with a single query only if you're on SQL 2008. Then you can use the MERGE statement.

    Until then, you need to do the UPDATE, followed by the INSERT, to do this. Lutz gave a great example; use it.

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

  • Thanks a lot!

    It worked. However I didn't understand the logic with insert. Why did you use left outer join and why did u compare with is null?

    Once again thank you!

    Regards,

    Hema

  • drop table Products --Target Table)

    drop table UpdatedProducts--Source Table --Update records

    --Create a target(Destination) table

    CREATE TABLE Products

    (

    ProductID INT PRIMARY KEY,

    ProductName VARCHAR(100),

    Rate MONEY

    )

    GO

    --Insert records into target(Destination) table

    INSERT INTO Products VALUES

    (1, 'Tea', 10.00),

    (2, 'Coffee', 20.00),

    (3, 'Muffin', 30.00),

    (4, 'Biscuit', 40.00)

    GO

    --Create source table

    CREATE TABLE UpdatedProducts

    (

    ProductID INT PRIMARY KEY,

    ProductName VARCHAR(100),

    Rate MONEY

    )

    GO

    --Insert records into source table

    INSERT INTO UpdatedProducts VALUES

    (1, 'Tea', 10.00),

    (2, 'Coffee', 25.00),

    (3, 'Muffin', 35.00),

    (5, 'Pizza', 60.00)

    GO

    SELECT * FROM Products--Destination table

    SELECT * FROM UpdatedProducts--Source Table

    SELECT * FROM Products left outer join UpdatedProducts

    on Products.ProductID=UpdatedProducts.ProductID

    GO

    /******************************************************************************/

    /********************************** Note:-*************************************/

    /******************************************************************************/

    /* After executing above tables and Records then unComment following Script */

    /*Update records in Destination table*/

    --UPDATE Products --Target (Destination table)

    --SET Products.ProductName =UpdatedProducts.ProductName,

    --Products.Rate=UpdatedProducts.Rate

    --FROM Products

    --INNER JOIN UpdatedProducts

    --ON Products.ProductID = UpdatedProducts.ProductID

    --/* Insert New records in destination table */

    --INSERT INTO Products (ProductID,ProductName,Rate)--New records

    --SELECT UpdatedProducts.ProductID,UpdatedProducts.ProductName,UpdatedProducts.Rate

    --FROM UpdatedProducts

    --LEFT OUTER JOIN Products

    --ON UpdatedProducts.ProductID = Products.ProductID

    --WHERE Products.ProductID IS NULL

    /******************************************************************************/

    /********************************** Note:-*************************************/

    /******************************************************************************/

    Thanks & Regards

    Ramesh Nagineni

    Infosys Technologies

    Hyderabad

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply