在CASE语句上获取错误,“,”附近的语法不正确

时间:2017-08-16 20:12:25

标签: sql sql-server

我已经查找过类似场景的CASE语法以及人们遇到的其他问题,并且无法专门找到这个问题。这在第一个case语句之后给出了逗号附近的语法错误,我无法弄清楚原因。

insert into usmas_Doors_HardwarePrepCodes(HWTypeMask, PrepTypeMask)  
select  
(  
    CASE HWType  
        WHEN NULL THEN 0  
        WHEN 'BT' THEN 1  
        WHEN 'CL' THEN 2  
        WHEN 'DB' THEN 3  
        WHEN 'ED' THEN 4  
        WHEN 'FB' THEN 5  
        WHEN 'HG' THEN 6  
        WHEN 'LK' THEN 7  
        WHEN 'SK' THEN 8  
        WHEN 'VW' THEN 9 END,

    CASE 
        WHEN PrepType IS NULL AND Cutout = 0 THEN 0
        WHEN PrepType = 'A' AND Cutout = 0 THEN 1
        WHEN PrepType = 'H' AND Cutout = 0 THEN 2
        WHEN PrepType = 'L' AND Cutout = 0 THEN 4
        WHEN PrepType IS NULL AND Cutout = 1 THEN 1024
        WHEN PrepType = 'A' AND Cutout = 1 THEN 1025
        WHEN PrepType = 'H' AND Cutout = 1 THEN 1026
        WHEN PrepType = 'L' AND Cutout = 1 THEN 1028 END
)
from HardwarePrepCodes

有什么想法吗?

编辑:尝试了“END AS [ColumnName]”并且只是在AS附近引发语法错误

1 个答案:

答案 0 :(得分:5)

问题正在发生,因为括号中都包含两个case语句。内部包含的所有内容(两个case语句)都将作为单个列返回。

只需删除它们即可解决问题。

insert into usmas_Doors_HardwarePrepCodes(HWTypeMask, PrepTypeMask)  
select   
    CASE HWType  
        WHEN NULL THEN 0  
        WHEN 'BT' THEN 1  
        WHEN 'CL' THEN 2  
        WHEN 'DB' THEN 3  
        WHEN 'ED' THEN 4  
        WHEN 'FB' THEN 5  
        WHEN 'HG' THEN 6  
        WHEN 'LK' THEN 7  
        WHEN 'SK' THEN 8  
        WHEN 'VW' THEN 9 END,

    CASE 
        WHEN PrepType IS NULL AND Cutout = 0 THEN 0
        WHEN PrepType = 'A' AND Cutout = 0 THEN 1
        WHEN PrepType = 'H' AND Cutout = 0 THEN 2
        WHEN PrepType = 'L' AND Cutout = 0 THEN 4
        WHEN PrepType IS NULL AND Cutout = 1 THEN 1024
        WHEN PrepType = 'A' AND Cutout = 1 THEN 1025
        WHEN PrepType = 'H' AND Cutout = 1 THEN 1026
        WHEN PrepType = 'L' AND Cutout = 1 THEN 1028 END
from HardwarePrepCodes