且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何进行sql循环查询以检查值之间的差异?

更新时间:2022-11-27 20:33:33

DECLARE @idFrom as int,
        @idTo as int,
        @gpsDateFrom as datetime,
        @gpsDateTo as datetime
DECLARE VehicleCursor CURSOR FAST_FORWARD FOR 
SELECT  vehicle_gps_id, 
        datetimeCol
FROM    yourtable
ORDER BY vehicle_gps_id
OPEN VehicleCursor FETCH NEXT FROM VehicleCursor INTO @idFrom, @gpsDateFrom
    FETCH NEXT FROM VehicleCursor INTO @idTo, @gpsDateTo
    WHILE @@FETCH_STATUS = 0 BEGIN 
        IF DATEDIFF(MI,@gpsDateFrom,@gpsDateTo) >5
        BEGIN
            --Break (your code here)
        END
        SET @idFrom = @idTo
        SET @gpsDateFrom = @gpsDateTo
        FETCH NEXT FROM VehicleCursor INTO @idTo, @gpsDateTo
    END 
CLOSE VehicleCursor 
DEALLOCATE VehicleCursor

类似的事情应该对您有用.它是一个游标,仅遍历比较日期时间的所有列.您可以在if语句后的注释部分中输入要执行的操作.

Something like this should work for you. It is a cursor that just runs through all your columns comparing datetimes. You can enter in whatever you want to do into the commented section after the if statement.