SET QUOTED_IDENTIFIER ON SET ANSI_NULLS ON GO CREATE PROCEDURE [dbo].[SP_ConfirmScaner] AS BEGIN --SET STATISTICS IO ON DECLARE @ScanerTime DATETIME , @InProvinceTimeDiffMinute INT , @InProvinceStockTimeDiffMinute INT , @UpdateState VARCHAR(20); SET @ScanerTime = GETDATE(); -- SET @ScanerTime = '2019-10-01'; SET @InProvinceTimeDiffMinute = 36 * 60 + 48 * 60; SET @InProvinceStockTimeDiffMinute = 72 * 60 + 48 * 60; SET @UpdateState = '000'; DECLARE @DistanceTable TABLE ( id INT IDENTITY(1, 1) , SendFlag BIT , DiffMinute INT , MinDistance INT , MaxDistance INT ); INSERT INTO @DistanceTable ( SendFlag, DiffMinute, MinDistance, MaxDistance ) SELECT 0, 48 * 60 + 48 * 60, -99999, 499 UNION SELECT 0, 72 * 60 + 48 * 60, 500, 999 UNION SELECT 0, 96 * 60 + 48 * 60, 1000, 1999 UNION SELECT 0, 168 * 60 + 48 * 60, 2000, 99999 UNION SELECT 1, 96 * 60 + 48 * 60, -99999, 499 UNION SELECT 1, 120 * 60 + 48 * 60, 500, 999 UNION SELECT 1, 144 * 60 + 48 * 60, 1000, 1999 UNION SELECT 1, 216 * 60 + 48 * 60, 2000, 99999; DECLARE @count INT; DECLARE @CurrentId INT; DECLARE @DiffMinute INT , @MinDistance INT , @MaxDistance INT , @SendFlag bit; SELECT @count = COUNT(0) FROM @DistanceTable WHILE ( @count > 0 ) BEGIN SELECT TOP 1 @CurrentId = id, @SendFlag = SendFlag, @DiffMinute = DiffMinute, @MinDistance = MinDistance, @MaxDistance = MaxDistance FROM @DistanceTable AS a; DELETE FROM @DistanceTable WHERE id = @CurrentId SELECT @count = COUNT(0) FROM @DistanceTable END RETURN; END GO
SQL server 不使用游标,使用临时表去获取数据,遍历处理数据