变量首先计算变量吗?

时间:2016-09-14 21:01:33

标签: sql-server variables sql-update

我有一个查询似乎是先计算变量。我创建了两个临时变量来跟踪它,即使我在变量更新之前和之后有一个变量,它们总是相等的:

DECLARE @swapTracker int
    SET @swapTracker = 0
DECLARE @currentStatus int
    SET @currentStatus = 0
update a
    set @swapTracker = (case
            when a.opt_out_ranking = 1
                then 0
            else (case 
            when a.do_not_email_ap = @currentStatus
                then @swapTracker
            else @swapTracker + 1
            end)
        end)
        ,a.swap_tracker = @swapTracker
        ,a.ap_var_test = @currentStatus
        -- Going to try to mark when someone Opted in
        ,opt_in_date = (case
            when opt_out_ranking = 1
                then (case 
                when a.do_not_email_ap = 0
                    then a.create_date
                else null
                end)
            else (case 
            when a.do_not_email_ap = @currentStatus
                then null
                else (case 
                when a.do_not_email_ap = 0
                    then a.create_date
                else null 
                end)
            end)
        end)
        -- Going to try to mark when someone Opted out
        ,opt_out_date = (case
            when opt_out_ranking = 1
                then (case 
                when a.do_not_email_ap = 1
                    then a.create_date
                else null
                end)
            else (case 
            when a.do_not_email_ap = a.ap_var_test
                then '2050-01-01 01:00:00.001'
                else (case 
                when a.do_not_email_ap = 1
                    then a.create_date
                else '2090-01-01 01:00:00.001'
                end)
            end)
        end)
        ,@currentStatus = (case
            when @currentStatus <> a.do_not_email_ap
                then a.do_not_email_ap
            else @currentStatus end)
        ,a.current_status = @currentStatus
FROM ##temp_email_opt_inout_date a

这是设计还是我忽略了什么? @currentStatus变量正在发生变化,所以我知道这不是问题所在,它似乎只是将这个变化带到a.ap_var_test和a.current_status字段中,无论我把它们放在哪里与@currentStatus有关

0 个答案:

没有答案