我尝试了两个查询来更新列
更新查询#1:
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].tempBudgeT t with (index (index_Budget ))
on t.[Code Site] = r.[Code Site]
and t.[Code Structure] = r.[Code Structure]
and t.[Date Time] = convert(date, r.[Date Time])
where r.[Date Time] >= '2015-01-01 00:0:00.000'
and r.Comp is null
end
SET rowcount 0
更新查询#2:
DECLARE @Counter INT=0 --This causes the @@rowcount to be > 0
while @@rowcount > 0
begin
set rowcount 10000
update [dbo].[Vente]
set Comp = (select top 1 t.comp
from [dbo].[Budget] t with (index (index_Budget))
where t.[Code Site] = [dbo].[Vente].[Code Site]
and t.[Code Rayon] = substring([dbo].[Vente].[Code Structure], 1, 4)
and t.[Date Time] = convert(date, [dbo].[Vente].[Date Time])
and [dbo].[Vente].[Date Time] >= '2015-01-01 00:0:00.000'
and [dbo].[Vente].Comp is null)
end
SET rowcount 0
我的问题是第二个查询比第一个查询更快但它没有工作,comp没有用第二个查询更新?问题在哪里?
答案 0 :(得分:0)
您的[dbo].[Vente]
和[dbo].[Budget]
尚未加入,并在子查询中更新条件:
DECLARE @Counter INT = 0 --This causes the @@rowcount to be > 0
WHILE @@rowcount > 0
BEGIN
SET ROWCOUNT 10000
UPDATE v
SET Comp = (
SELECT TOP 1 t.comp
FROM [dbo].[Vente] v
INNER JOIN [dbo].[Budget] t WITH (INDEX (index_Budget)) t.[Code Site] = v.[Code Site]
WHERE t.[Code Site] = v.[Code Site]
AND t.[Code Rayon] = substring([dbo].[Vente].[Code Structure], 1, 4)
AND t.[Date Time] = convert(DATE, [dbo].[Vente].[Date Time])
AND [dbo].[Vente].[Date Time] >= '2015-01-01 00:0:00.000'
AND [dbo].[Vente].Comp IS NULL
)
END
SET ROWCOUNT 0