SQL Server:使用一个条件更新五列
在表中,我有一列表示货币。我必须从两列(例如:a、b)计算一个值,并根据货币更新五列(例如:e、f、g、h、I、j)中的一列 我试过用CASE-likeSQL Server:使用一个条件更新五列,sql,sql-server,case,Sql,Sql Server,Case,在表中,我有一列表示货币。我必须从两列(例如:a、b)计算一个值,并根据货币更新五列(例如:e、f、g、h、I、j)中的一列 我试过用CASE-like CASE when currency = 'INR' then a+b else 0 end as e CASE when currency = 'USD' then a+b else 0 end as f CASE when currency = 'CAN' then a+b else 0 end as g CASE when currenc
CASE when currency = 'INR'
then a+b
else 0
end as e
CASE when currency = 'USD'
then a+b
else 0
end as f
CASE when currency = 'CAN'
then a+b
else 0
end as g
CASE when currency = 'EUR'
then a+b
else 0
end as h
CASE when currency = 'AUD'
then a+b
else 0
end as i
还有其他方法吗?伪代码如下所示。
Update myTable
set e = CASE when currency = 'INR' then a+b ELSE 0 end,
f = CASE when currency = 'USD' then a+b ELSE 0 end,
g = CASE when currency = 'CAN' then a+b ELSE 0 end,
h = CASE when currency = 'EUR' then a+b ELSE 0 end,
i = CASE when currency = 'AUD' then a+b ELSE 0 end;
不包括NULL/0值
var result = a+b;
var columnName;
CASE
WHEN currency = 'INR' THEN columnName = e,
WHEN currency = 'USD' THEN columnName = f,
.
.
WHEN currency = 'AUD' THEN columnName = j
END
**your update statement goes here with 'columnName' variable..**
伪代码如下所示。 不包括NULL/0值
var result = a+b;
var columnName;
CASE
WHEN currency = 'INR' THEN columnName = e,
WHEN currency = 'USD' THEN columnName = f,
.
.
WHEN currency = 'AUD' THEN columnName = j
END
**your update statement goes here with 'columnName' variable..**
除了案件陈述之外,还有可能做其他事情吗?@VigneshVelusamy,是的。您可以使用“if-else”梯形图来形成sql查询。除了CASE语句之外,还可以执行其他操作吗?@VigneshVelusamy,是的。您可以使用“if-else”梯形图来形成sql查询。