我在更新MSSQL中的列时遇到了问题,所以基本上我有一个这样的数据库
no U1 U2 U3 U4
1 12 -1 10 -1
2 12 15 -1 11
3 -1 17 10 11
4 12 -1 10 11
我的目标是更新在-1 \f25 1 -1 \f6上有值的列,它在-1 \f25 U2 - 1 \f6和-1\f25 U4 -1\f6上有两个-1\f25-1\f6值,我想要的是用-1\f25 I-1\f6声明的值-1\f6更新-1\f25 U2 -1\f6,而不是更新-4\f25 U2 -1\f6。
如何做到这一点呢?
发布于 2014-12-18 19:23:17
使用UPDATE语句的where子句,例如:
update table1
set U1 = case U1 when -1 then 123 else U1 end,
U2 = case when U1 != -1 and U2 = -1 then 123 else U2 end,
U3 = case when -1 not in (U1, U2) and U3 = -1 then 123 else U3 end,
U4 = case when -1 not in (U1, U2, U3) and U4 = -1 then 123 else U4 end
where [no] = (select top 1 [no] from table1 where -1 in (U1, U2, U3, U4))
https://stackoverflow.com/questions/27553981
复制相似问题