可以使用session变量循环
   
    @@ROWCOUNT
   
   用一个
   
    TOP N
   
   关于你的
   
    DELETE
   
   是的。
  
  SELECT 1 -- Forces @@ROWCOUNT = 1
WHILE @@ROWCOUNT > 0
BEGIN
    delete TOP (500) I from 
        xxx.dbo.charges AS I
    where 
        exists (
            select 
                'to delete' 
            from 
                @chargeids c
                left join xxx.dbo.chargeitems ci on ci.Charge_Id = c.id
            where 
                ci.id is null AND
                c.id = I.Id)
END
  
   如果你不想用哨兵
   
    SELECT
   
   (如果有的话,它会将结果返回给客户机),您可以使用变量。
  
  DECLARE @ForceStart BIT = 1
WHILE @@ROWCOUNT > 0 OR @ForceStart = 1
BEGIN
    SET @ForceStart = 0
    delete TOP (500) I from 
        xxx.dbo.charges AS I
    where 
        exists (
            select 
                'to delete' 
            from 
                @chargeids c
                left join xxx.dbo.chargeitems ci on ci.Charge_Id = c.id
            where 
                ci.id is null AND
                c.id = I.Id)
END
  
   如果子查询需要很长时间才能处理,则可能需要创建一个临时表,其中包含要删除的ID,并在循环中与其联接。
  
  
   作为一个旁注,如果您正在检查记录的不存在
   
    xxx.dbo.chargeitems
   
   ,做一个
   
    NOT EXISTS
   
   会比
   
    LEFT JOIN
   
   具有
   
    IS NULL
   
   是的。
  
  
  
   
    编辑
   
   :使用临时表保存ID:
  
  -- Create temporary table
IF OBJECT_ID('tempdb..#ChargesToDelete') IS NOT NULL
    DROP TABLE #ChargesToDelete
SELECT DISTINCT
    c.id
INTO
    #ChargesToDelete
from 
    @chargeids c
    left join xxx.dbo.chargeitems ci on ci.Charge_Id = c.id
where 
    ci.id is null
CREATE CLUSTERED INDEX CI_ChargesToDelete ON #ChargesToDelete (id)
-- Delete rows in batches
SELECT 1 -- Forces @@ROWCOUNT = 1
WHILE @@ROWCOUNT > 0
BEGIN
    delete TOP (500) I from 
        xxx.dbo.charges AS I
    where 
        exists (select 'to delete' from #ChargesToDelete AS C where c.id = I.Id)
END