CASE语句SQL Server的执行顺序

时间:2017-10-08 00:48:23

标签: sql-server insert case

具有属于CASE语句一部分的多个部分的INSERT语句是否按顺序执行并执行'规则'因为缺少一个更好的词,即使在下一行之后仍然存在?在下面的查询中,PO_TYPE分配是否会覆盖下一个命令 - 例如查看文章列表?因此即使该文章在声明的第二部分的列表中,如果它是05或07类型,它仍将分配给安德鲁?

感谢。

/*INSERT values into the table using SELECT making sure to exclude vendor 20800 - (see last line of code)*/

INSERT INTO SCM_PO_EMPLOYEE_NAME (PO_NUMBER, PO_ITEM_NUMBER, MATERIAL, BUSINESS_UNIT_CODE,PO_TYPE,TEAM_MEMBER_NAME)

                                  SELECT I.PO_NUMBER, 
                                         I.PO_ITEM_NUMBER, 
                                         I.MATERIAL,
                                         B.BU_CODE, 
                                         H.PO_TYPE,
                                         CASE WHEN H.PO_TYPE IN ('05','07') -- Promo PO type - should be on both po type and stock category
                                              AND  I.STOCK_CATEGORY LIKE ('A60383%') -- stock category is second part of the check
                                              THEN 'AZ'
                                              WHEN H.PO_TYPE = '02' -- ma PO type
                                              THEN 'MB'
                                              WHEN I.MATERIAL IN ( SELECT ARTICLE
                                                                   FROM   ADI_USER_MAINTAINED.dbo.SCM_EMPLOYEE_ARTICLE A ) -- Check the Employee to article table next
                                              THEN A.TEAM_MEMBER_NAME -- If the PO number matches that conditions then assign the employee from the employee article table
                                              WHEN M.BUSINESS_UNIT_CODE = B.BU_CODE -- if not use then go to the BU assignment (below)
                                              THEN B.TEAM_MEMBER_NAME  --- Use the team member name from the Employee_BU table
                                         END  AS   [TEAM_MEMBER_NAME]

                                  FROM   PDX_SAP_USER.dbo.VW_PO_HEADER H
                                  JOIN   PDX_SAP_USER.dbo.VW_PO_ITEM I ON H.PO_NUMBER = I.PO_NUMBER 
                                  JOIN   PDX_SAP_USER.dbo.VW_MM_MATERIAL M ON I.MATERIAL = M.MATERIAL 
                                  JOIN   ADI_USER_MAINTAINED.dbo.SCM_EMPLOYEE_ARTICLE A ON I.MATERIAL = A.ARTICLE
                                  JOIN   ADI_USER_MAINTAINED.dbo.SCM_EMPLOYEE_BU B ON B.BU_CODE = M.BUSINESS_UNIT_CODE


                                  WHERE H.VENDOR_NO <> '20800'; --Exclude '20800' as a vendor!!

1 个答案:

答案 0 :(得分:0)

按顺序评估case 表达式

因此,第二个then仅在第一个then未返回true时进行评估。作为一个极端的例子,考虑:

select (case when 1=1 then 'true'
             when 1/0 = 0 then 'error'
        end)

这会返回'true'而非错误输出。