删除到SQL Server循环

时间:2014-12-05 18:22:23

标签: sql sql-server sql-server-2008 for-loop

如果是重复周期,需要删除第二次出现(15到15分钟),但查询总是返回全部或全部。

DECLARE @DatAteTemp DATE
DECLARE @HorAteTemp TIME

DECLARE cursor_i CURSOR FOR
SELECT DatAte, HorAte
FROM @TABLE

OPEN cursor_i
FETCH NEXT FROM cursor_i INTO @DatAteTemp, @HorAteTemp;

WHILE @@FETCH_STATUS = 0
    BEGIN

        IF  (select count(*) from @TABLE where HorAte = DATEADD(MINUTE, -15, @HorAteTemp)) > 0
        --DATEADD(MINUTE, -15, @HorAteTemp) = @HorAteTemp
        BEGIN
            DELETE FROM @TABLE WHERE CURRENT OF cursor_i
        END

        FETCH NEXT FROM cursor_i INTO @DatAteTemp, @HorAteTemp
    END

CLOSE cursor_i;
DEALLOCATE cursor_i;

另:

DECLARE @aux TIME = DATEADD(MINUTE, -15, @HorAteTemp)
IF  (select count(*) from @TABLE AS oito where oito.HorAte = @aux) > 0

1 个答案:

答案 0 :(得分:0)

永远不可能,因为我将其应用为:

SELECT
[...]
CROSS APPLY dbo.Function(Dat, Hor)
[...]