Home Forums SQL Server 2008 T-SQL (SS2K8) Change Set clause of Update Statement dynamically based on some condition. RE: Change Set clause of Update Statement dynamically based on some condition.

  • Having 2 very similar updates like

    UPDATE t SET

    t.AODYD = <exp1>

    FROM <table expression>

    WHERE <common predicates> AND <predicate1>

    UPDATE t SET

    t.DAODYD = <exp2>

    FROM <table expression>

    WHERE <common predicates> AND <predicate2>

    you logically may generalize them like this

    UPDATE t SET

    t.AODYD = CASE WHEN<predicate1> THEN <exp1> ELSE t.AODYD END,

    t.DAODYD =CASE WHEN<predicate2> THEN <exp2> ELSE t.DAODYD END

    FROM <table expression>

    WHERE <common predicates> AND (<predicate1> OR <predicate2>)

    But i second Grant Fritchey's warning about execution plans.