我有一些使用replace函数运行更新的查询。例如:
Update my_table set mycolumn=replace(my_column,@OldServer,@NewServer)
我想要完成的是尝试一次更新这么多行。我只想说我想一次做两行。这是我试过的
use mydb
DECLARE @OldServer1 varchar(255)
DECLARE @OldServer2 varchar(255)
DECLARE @NewServer1 varchar(255)
DECLARE @NewServer1 varchar(255)
--Edit these values
SET @OldServer = 'abc'
SET @NewServer = 'ghi'
print 'My Table'
set rowcount 2
update my_table
set my_column=replace(my_column, @OldServer, @NewServer)
while @@rowcount>0
Begin
Set Rowcount 2
update my_table
set my_column=replace(my_column, @OldServer, @NewServer)
END
Set Rowcount 0
会发生的情况是,该表仅由两行更新,并且一遍又一遍地循环。我猜测替换假设一切。我也尝试了一个最高计数,它做了同样的事情。关于如何实现目标的任何想法?
答案 0 :(得分:0)
SQL表中没有自然行号,因此您需要创建一个
声明@StartNo INT; 声明@EndNo INT; 声明@LastNo INT;
set @StartNo = 1;
set @EndNo = 2;
set @LastNo = (select count(*) from My_Table);
while @StartNo <= @LastNo
begin
with CTE as
(
select M1.*, row_number() over (order by OrderColumn) as Row_Ord
from My_Table
)
update CTE
set My_Column = replace(my_column, @OldServer, @NewServer)
where Row_Ord between @StartNo and @EndNo;
set @StartNo = @EndNo + 2;
set @EndNo = @EndNo + 2;
end