2

对于以下查询结果中的以下人物,UniqueIDSuffix 值应使用前导 0 填充,字符长度 = 6。UniqueIDSuffix 修改后,UniqueID 值应通过 UniqueIDPrefix + UniqueIDSuffix = UniqueID 更新为字符长度 = 9

select * from cph..cppat (nolock) where 
UniqueIDPrefix is not null and 
UniqueIDPrefix <> 'VIS' and len(UniqueIDSuffix) < 6

按 UniqueIDPrefix asc 排序

我的声明是:

SET UniqueIDsuffix  =
        (CASE
           WHEN UniqueIDsuffix = 3 THEN '000' + UniqueIDsuffix  ELSE
           When UniqueIDsuffix = 4 THEN '00' + UniqueIDsuffix ELSE
           WHEN UniqueIDsuffix = 5 Then '0' + UniqueIDsuffix ELSE
           WHEN UniqueIDsuffix = 6 THEN UniqueIDsuffix ELSE
           )
where UniqueIDPrefix is not null and UniqueIDPrefix <> 'VIS' 
and len(UniqueIDSuffix) < 6
4

1 回答 1

3

您缺少更新语句。. . 你有无关的else陈述。. . 并且您缺少len()比较中的功能:

update cph..cppat
    SET UniqueIDsuffix  =
            (CASE WHEN len(UniqueIDsuffix) = 3 THEN '000' + UniqueIDsuffix 
                  When len(UniqueIDsuffix) = 4 THEN '00' + UniqueIDsuffix 
                  WHEN len(UniqueIDsuffix) = 5 Then '0' + UniqueIDsuffix 
                  WHEN len(UniqueIDsuffix) = 6 THEN UniqueIDsuffix 
            )
    where UniqueIDPrefix is not null and UniqueIDPrefix <> 'VIS' and len(UniqueIDSuffix) < 6

顺便说一句,您可以更简单地表示为:

update cph..cppat
    SET UniqueIDsuffix  = right('0000000'+UniqueIDSuffix, 6)
    where UniqueIDPrefix is not null and UniqueIDPrefix <> 'VIS' and len(UniqueIDSuffix) < 6
于 2013-01-03T16:12:18.313 回答