带有2个列值SQL Server的T-SQL Case语句

时间:2015-05-15 14:53:09

标签: sql sql-server tsql case case-statement

我正在尝试使用Case语句检查一列是否具有某个值,并根据该值设置另一列等于某个值。任何有关如何做到这一点的帮助将不胜感激。

像这样:

    SELECT
        td=isnull(a.[Instance_Name],''), '',
        td=isnull
            (CASE 
                WHEN a.[Instance_Name] LIKE 'ZZZ%'
                    THEN 'ZZZ'
                ELSE MAX(dbl.[Mnemonic]) 
            END AS dbl.[Mnemonic]), '',
        td=isnull(dbl.[Env_Type],''), '',
FROM #FailedCRITs a
    INNER JOIN [Util].[DBLIST] dbl ON a.[Instance_Name] = dbl.[Instance_Name]

3 个答案:

答案 0 :(得分:2)

如果这是update声明:

update table t
    set column2 = (case when column1 LIKE 'value1' then 'value2' else value3 end)

答案 1 :(得分:1)

SELECT column1, CASE
    WHEN column1 = 50 THEN 100
    ELSE 150
END AS column2
FROM Table1

答案 2 :(得分:0)

这取决于您是要尝试永久更新值,还是仅在select语句中生成它们。

如果更新,它将类似于:

UPDATE tbl
SET c2 = CASE WHEN c1 LIKE 'foo' THEN 'bar' ELSE 'baz' END

其他,

SELECT c1
, CASE WHEN c1 LIKE 'foo' THEN 'bar' ELSE 'baz' END AS c2
FROM TBL