stored procedure update

  • There is a stored procedure that updates some columns. I want the following:

    if the column value already exists it must not run update.

    How can this be done?

  • UPDATE t1
    SET t1.col1 = ISNULL(t1.col1,UpdateValue)

    The absence of evidence is not evidence of absence.
    Martin Rees

    You can lead a horse to water, but a pencil must be lead.
    Stan Laurel

  • Not much to go on, but, in general, just put the appropriate conditions in the WHERE clause:

    DECLARE @key_value ...
    DECLARE @col_value ...

    SET @key_value = ...
    SET @col_value = ...

    UPDATE dbo.table_name
    SET nonkey_col = @value1
    WHERE key_col1 = 'key_value' AND (nonkey_col IS NULL OR nonkey_col <> @col_value)

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

Viewing 3 posts - 1 through 2 (of 2 total)

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