• 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]