简体   繁体   English

T-SQL 中的 CASE 表达式更改其他列值

[英]CASE Expression in T-SQL changing other column-value

I'm working with T-SQL and I want to write a CASE expression.我正在使用 T-SQL,我想编写一个CASE表达式。 I do have a table with columns: Username and Usertype .我确实有一个包含列的表: UsernameUsertype If the Username is for example 'Demo' then I want to have the Usertype filled with 'special' .例如,如果Username名是'Demo'那么我想让用户Usertype填充'special' Else the Usertype should be 'normal' .否则Usertype应该是'normal' I tried following:我试过以下:

SELECT Username, Usertype, 
    CASE Username WHEN 'Demo' THEN Usertype = 'Special' 
    END
    FROM Table1

maybe someone could help me as it doesn't work.也许有人可以帮助我,因为它不起作用。

You were on the right track:你走在正确的轨道上:

SELECT Username, Usertype, 
       CASE Username WHEN 'Demo'
                     THEN 'special' ELSE 'normal' END AS UsertypeNew
FROM Table1;

If you actually want to update your table, then use:如果你真的想更新你的表,那么使用:

UPDATE Table1
SET Usertype = CASE Username WHEN 'Demo' THEN 'special' ELSE 'normal' END;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM