在SQL Server中插入整列的值

问题描述 投票:-2回答:3

有没有办法为特定列插入不同的值,而不是SQL Server中的整行。 Classic UPDATE SET会将all设置为相同的值。

enter image description here

例如,我想为3行中的每一行设置一些int值到Ranking列,而不删除它们并完成新的插入。

sql sql-server
3个回答
0
投票

使用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

0
投票

做更新

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

0
投票

你想将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;
© www.soinside.com 2019 - 2024. All rights reserved.