Home Forums SQL Server 7,2000 T-SQL Condional Update/insert from one table to other. RE: Condional Update/insert from one table to other.

  • I believe you have to use a two step procedure

    1. Update existing values:

    IF EXISTS(SELECT p.ProductName,p.StoreName FROM Products p, temptable t

    WHERE p.ProductName = t.productname AND p.storename = t.storename)

    BEGIN

    UPDATE Dbo.Products

    SET Price = t.price

    FROM Dbo.Products AS p

    JOIN Dbo.Temptable AS t

    ON p.ProductName = t.ProductName

    AND p.storename = t.storename

    END

    2. Add items where the combination of productname and storename are not in the products table

    INSERT INTO products (productname, storename, price)

    SELECT t.ProductName,t.storename,t.price

    FROM temptable t

    LEFT OUTER JOIN products p

    ON p.productname = t.productname AND p.storename = t.storename

    WHERE p.productname IS NULL OR p.storename IS NULL

    I hope this helps.

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]