优化更新sql server

时间:2015-10-07 08:42:39

标签: sql sql-server

我有两个查询来更新表中包含3亿行

的两列

查询1

DECLARE @Counter INT=0 --This causes the @@rowcount to be > 0
while @@rowcount>0
BEGIN
    SET rowcount 10000

    update t 
    set [OC]=r.[OC]
    from [dbo].[Vente] t 
    INNER join [dbo].[OC] r on t.[Date Time] between r.[DATE DEBUT] and r.[DATE FIN]
    WHERE r.[DATE FIN] >= '2015-01-01'
    AND r.[DATE DEBUT] <= '2015-12-31'
    and t.[OC] is null
end

查询2

SET rowcount 0 

DECLARE @Counter INT=0 --This causes the @@rowcount to be > 0
while @@rowcount>0
BEGIN
  SET rowcount 10000
  update r
  set Comp=t.Comp
  FROM [dbo].[Vente]r 
  inner join [dbo].[temp Budget] t 
  on t.[Code Site]=r.[Code Site]
  and t.[Code Structure] =substring(r.[Code Structure],1,4)
  and t.[Date Time]=convert(date,r.[Date Time])
  WHERE convert(date,t.[Date Time]) >= '2015-01-01'
  AND convert(date,t.[Date Time]) <= '2015-12-31'
  and r.Comp is  null  
end
SET rowcount 0 

有没有办法合并两个查询?

0 个答案:

没有答案