Is there any difference between the 2

  • Hi,

    Any difference in results or performance between the 2 examples? Names of tables/columns are generic below:

    #1

    update customer t1

    set column_1 = x.value

    FROM

    (select id

    , case

    when column_2 is null and column_3 = 1681 then 10338

    when column_2 is not null and column_3 = 1681 then 10339

    else 10340

    end as value

    from customer

    ) x

    where t1.id = x.id

    ;

    #2

    update customer t1

    set column_1 = (select case

    when column_2 is null and column_3 = 1681 then 10338

    when column_2 is not null and column_3 = 1681 then 10339

    else 10340

    end as value

    from customer t2

    where t1.id = t2.id

    )

    ;

    thanks in advance!

  • mcampoam (10/12/2016)


    Hi,

    Any difference in results or performance between the 2 examples? Names of tables/columns are generic below:

    #1

    update customer t1

    set column_1 = x.value

    FROM

    (select id

    , case

    when column_2 is null and column_3 = 1681 then 10338

    when column_2 is not null and column_3 = 1681 then 10339

    else 10340

    end as value

    from customer

    ) x

    where t1.id = x.id

    ;

    #2

    update customer t1

    set column_1 = (select case

    when column_2 is null and column_3 = 1681 then 10338

    when column_2 is not null and column_3 = 1681 then 10339

    else 10340

    end as value

    from customer t2

    where t1.id = t2.id

    )

    ;

    thanks in advance!

    Both methods will fare poorly against the more natural way of writing this statement:

    -- read the table only once

    UPDATE Customer SET column_1 = CASE

    WHEN column_2 IS NULL AND column_3 = 1681 THEN 10338

    WHEN column_2 IS NOT NULL AND column_3 = 1681 THEN 10339

    ELSE 10340 END

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • ChrisM@Work (10/12/2016)


    Both methods will fare poorly against the more natural way of writing this statement:

    -- read the table only once

    UPDATE Customer SET column_1 = CASE

    WHEN column_2 IS NULL AND column_3 = 1681 THEN 10338

    WHEN column_2 IS NOT NULL AND column_3 = 1681 THEN 10339

    ELSE 10340 END

    I actually just wrote the same way. Appreciate it!

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

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