有没有办法为特定列插入不同的值,而不是SQL Server中的整行。 Classic UPDATE SET会将all设置为相同的值。
例如,我想为3行中的每一行设置一些int值到Ranking列,而不删除它们并完成新的插入。
使用row_number()
update a set ranking =rn from tablename a join
(select id,name, row_number() over(order by id) as rn
from tablename) b on a.id=b.id
做更新
update tabale
set ranking= your_va
with cte as
(
select * ,row_number() over(order by id) rn
) update cte set ranking=rn where id<=3
你想将ranking
设置为什么价值?
任何价值:
update mytable set ranking = id;
某些值:
update mytable set ranking =
case name
when 'Positive' then 1
when 'Neutral' then 2
when 'Negative' then 3
else 4
end;