提问人:QueenOfCode 提问时间:9/29/2022 最后编辑:QueenOfCode 更新时间:9/29/2022 访问量:616
在 SQL 查询中使用 While 循环 - 它不断插入记录而不会中断
Using While Loop in SQL Query -Its keep inserting the record without break
问:
在我将标识列从 INT 更改为 BigInt 后,我使用 while 循环将增量数据从旧表移动到新表。我已经恢复了 10 亿条记录。但是我仍然需要将增量数据移动到新表中。我目前正在将增量数据从旧表插入到新表。因此,我从暂存表中获取了 max(hist_id),并将其配置为参数表中的参数值,@hist_id_start。批处理将运行 100k 一次。因此,它将从旧表中查找hist_id并插入,直到所有新记录都插入到新表中。下面的查询似乎不起作用。例如hist_id开始是 6709,它应该将 1000 条记录插入批处理中,并在达到 13218 时停止,但不知何故它会继续循环并插入不需要的记录。 这是示例记录:它从 6709 开始,我添加 1000 条记录 7709,它应该插入直到达到 13218 hist_id_start:6709 hist_id_end:7709 记录数:1000 hist_id_max:13218
DECLARE @hist_id_start INT;
DECLARE @hist_id_end INT;
DECLARE @hist_id_max INT;
DECLARE @batchcount INT;
DECLARE @reccount INT;
SET @batchcount =1000
SELECT @hist_id_start = value_text from PARAMETER (NOLOCK) WHERE PARAMETER_NAME='DIST_PRODUCT_COST_HIST_ID_INCREMENTAL'
SET @hist_id_end= (@hist_id_start + @batchcount)
set @hist_id_max= (select max(hist_id) from DIST_PRODUCT_COST_HIST with(nolock))
SELECT @reccount = count(*) from dist_product_cost_hist (nolock) WHERE hist_id >=@hist_id_start and hist_id < (@hist_id_end)
PRINT 'hist_id_start: ' + convert(nvarchar(20), @hist_id_start)
PRINT 'hist_id_end: ' + convert(nvarchar(20), @hist_id_end)
PRINT 'Record count: ' + convert(nvarchar(20), @reccount)
PRINT 'hist_id_max: ' + convert(nvarchar(20), @hist_id_max)
while(@hist_id_end=<@hist_id_max)
--select IDENT_CURRENT('dist_product_cost_hist') - number need to always less than SELECT value_text from PARAMETER (NOLOCK) WHERE PARAMETER_NAME='DIST_PRODUCT_COST_HIST_ID'
begin
INSERT INTO Dist_Product_Cost_Hist_2022(dist_id,dist_sku_num,cost_type_code,cost_ref_num,cost,eff_date,end_date,manual_flag,currency_code,update_dts,update_uid,row_id)
SELECT dist_id,dist_sku_num,cost_type_code,cost_ref_num,cost,eff_date,end_date,manual_flag,currency_code,update_dts,update_uid,row_id
FROM Dist_Product_Cost_Hist (nolock)
WHERE hist_id >=@hist_id_start and hist_id < (@hist_id_end)
-- Update last hist_id in parameter table
UPDATE PARAMETER
set value_text=@hist_id_end, update_dts =getdate()
WHERE PARAMETER_NAME='DIST_PRODUCT_COST_HIST_ID_INCREMENTAL'
end
答:
0赞
iceblade
9/29/2022
#1
您需要在每次迭代后增加@hist_id_end:
while(@hist_id_end=<@hist_id_max)
--select IDENT_CURRENT('dist_product_cost_hist') - number need to always less than SELECT value_text from PARAMETER (NOLOCK) WHERE PARAMETER_NAME='DIST_PRODUCT_COST_HIST_ID'
begin
INSERT INTO Dist_Product_Cost_Hist_2022(dist_id,dist_sku_num,cost_type_code,cost_ref_num,cost,eff_date,end_date,manual_flag,currency_code,update_dts,update_uid,row_id)
SELECT dist_id,dist_sku_num,cost_type_code,cost_ref_num,cost,eff_date,end_date,manual_flag,currency_code,update_dts,update_uid,row_id
FROM Dist_Product_Cost_Hist (nolock)
WHERE hist_id >=@hist_id_start and hist_id < (@hist_id_end)
-- Update last hist_id in parameter table
UPDATE PARAMETER
set value_text=@hist_id_end, update_dts =getdate()
WHERE PARAMETER_NAME='DIST_PRODUCT_COST_HIST_ID_INCREMENTAL'
SET @hist_id_end = @hist_id_end + @batchcount
end
评论
0赞
QueenOfCode
10/3/2022
如果我添加hist_id结束它也保持循环,如下所示。
评论
@hist_id_end
@hist_id_start
SELECT
NOLOCK
Dist_Product_Cost_Hist
WHERE hist_id >=@hist_id_start and hist_id < (@hist_id_end)
DECLARE @i int = 1, @t int = 10; WHILE @i < @t BEGIN PRINT @i END;
PRINT
1