高手请进,sql server 成批更新

COMPUTERZZZ 2009-08-17 09:19:09
我要成批更新数据库,每次更新100条,
这是我的程序,感觉效率不是很高.5万条数据4分钟都没更完,有没有更好的方法?

declare @x int
declare @y int
declare @pagecount int
declare @count int
set @count=1
set @pagecount=100
set @x=1
set @y=100

while 1=1
begin
begin tran
update a set sh_finished=1 ,sh_currstatus='结算'
from
(
select Row_Number() over(order by sh_noteno asc) as RowNumber,
sh_noteno,sh_lineno,sh_finished,sh_currstatus
from sh_repair_dtl with(nolock)
where sh_finished=0 and sh_transdate<'2009-05-01 00:00:00'
) a
where RowNumber between @x and @y
--select '@@RowCount='+Cast( @@RowCount as char(10) )
--select '@@Error='+Cast( @@Error as char(10) )
--if @@Error <> 0
--begin
-- Raiserror('Update error',16,1)
-- RollBack
-- Break
--end
--判断是否全部更新完
if @@RowCount = 0
Break
else
commit
set @count=@count+1
set @x=@x+@pagecount
set @y=@y+@pagecount
select 'X='+cast(@X as char(6) )
select 'Y='+cast(@Y as char(6) )
end

Select @count
...全文
72 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
COMPUTERZZZ 2009-08-17
  • 打赏
  • 举报
回复
sdhdy:这样吗?
declare @x int
declare @y int
declare @pagecount int
declare @count int
set @count=1
set @pagecount=100
set @x=1
set @y=100

select Row_Number() over(order by sh_noteno asc) as RowNumber,
sh_noteno,sh_lineno,sh_finished,sh_currstatus into #tmp1
from sh_repair_dtl with(nolock)
where sh_finished=0 and sh_transdate<'2009-05-01 00:00:00'

while 1=1
begin
begin tran
update a set sh_finished=1 ,sh_currstatus='结算'
--select *
from
(
select Row_Number() over(order by sh_noteno asc) as RowNumber,
sh_noteno,sh_lineno,sh_finished,sh_currstatus
from sh_repair_dtl with(nolock)
where sh_finished=0 and sh_transdate<'2009-05-01 00:00:00'
) a INNER JOIN #tmp1 b on a.Rownumber=b.rownumber
where a.RowNumber between @x and @y
--select '@@RowCount='+Cast( @@RowCount as char(10) )
--select '@@Error='+Cast( @@Error as char(10) )
--if @@Error <> 0
--begin
-- Raiserror('Update error',16,1)
-- RollBack
-- Break
--end
--判断是否全部更新完
if @@RowCount = 0
Break
else
commit
set @count=@count+1
set @x=@x+@pagecount
set @y=@y+@pagecount
select 'X='+cast(@X as char(6) )
select 'Y='+cast(@Y as char(6) )
end

Select @count

sdhdy 2009-08-17
  • 打赏
  • 举报
回复
把这块:
select Row_Number() over(order by sh_noteno asc) as RowNumber, 
sh_noteno,sh_lineno,sh_finished,sh_currstatus
from sh_repair_dtl with(nolock)
where sh_finished=0 and sh_transdate <'2009-05-01 00:00:00'

放进一个临时表,然后A表和临时表关联更新试试。
COMPUTERZZZ 2009-08-17
  • 打赏
  • 举报
回复
那有什么好方法!因为更新的数据比较多,只能分批更新的
不然数据库会锁死的
SQL77 2009-08-17
  • 打赏
  • 举报
回复
循环更新很慢的
--小F-- 2009-08-17
  • 打赏
  • 举报
回复
循环本来就比较慢

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧