Sql server 当字段更新时,是否有方法更改列的其他字段?

Sql server 当字段更新时,是否有方法更改列的其他字段?,sql-server,Sql Server,当我将id为5的状态列更新为“是”时 我想要这样的结果 id status ------------- 1 no 2 yes 3 no 4 yes 5 -- 有可能吗?我的建议是根据您的需要在一个事务中使用多个更新语句,对于上面的示例,您将拥有: id status ------------- 1 no 2 no 3 no 4 no 5 Yes updatetablename set status='Yes',其中i

当我将id为5的状态列更新为“是”时

我想要这样的结果

id status
-------------
1   no
2   yes
3   no
4   yes
5   --

有可能吗?

我的建议是根据您的需要在一个事务中使用多个更新语句,对于上面的示例,您将拥有:

id    status
-------------
1      no
2      no
3      no
4      no
5      Yes
updatetablename set status='Yes',其中id=5;
更新tableName集状态='no',其中id为5;

在一个事务中同时使用两个更新查询来完成这两个查询(成功或失败)

您可以通过
CASE
语句或
IIF
函数来实现

查询将是:

update tableName set status = 'Yes' where id = 5;

update tableName set status = 'no' where id <> 5;

UPDATE SampleTable SET [Status] = CASE WHEN Id = @InputValue THEN 'yes' ELSE 'no' END;
UPDATE SampleTable SET [Status] = IIF(Id = @InputValue , 'yes', 'no');