Sql 两个更新查询
为了更新列,我尝试了两个查询 更新查询1:Sql 两个更新查询,sql,sql-server,sql-update,Sql,Sql Server,Sql Update,为了更新列,我尝试了两个查询 更新查询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_B
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没有用第二个查询更新吗?问题出在哪里?您的[dbo].[Vente]和[dbo].[Budget]尚未加入,请在subquery中更新条件:
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
通过将更新更改为select并查看返回的内容来进行故障排除。如果没什么,开始一个接一个地移除过滤器,直到有东西返回。一旦您确定了字段,请选择它并将实际结果与您期望的结果进行比较。因为第二个查询是错误的,@Backs查询似乎还可以,但我认为它并不比第一个查询好。现在在第一个查询中,我不明白为什么要使用while循环和rowCount。当它只循环一次时。您真正的问题是什么。