Below is my complete code. It select the range of records from the base table and insert into a different table and then deletes those records from the base table but when i debuged the cursor i am still getting the value of time_encoede which should be deleted.
declare @pgname nvarchar(20)
declare @videorelease nvarchar(40)
declare @dateaired nvarchar(15)
declare @dateencoded nvarchar(15)
declare @timeencoded nvarchar(15)
declare @network nvarchar(12)
declare @count int
declare localshowcursor cursor for
SELECT video_release, programname, network, date_encoded, time_encoded, COUNT(*) AS Number
FROM SigmaStageTemp s
Where program_type = ¡®1¡¯
Group by video_release, programname, network, date_encoded, time_encoded
Having count(*) >= 10
Order by video_release, programname, network, date_encoded, time_encoded
OPEN localshowcursor
FETCH NEXT FROM localshowcursor INTO @videorelease, @pgname, @network, @dateencoded, @timeencoded, @count
WHILE @@FETCH_STATUS = 0
BEGIN
Insert into sigmtemp(Video_release, country, market_rank, Designated_mrkt_area, station, network, date_aired, day_of_week, day_part, half_hour_aired, programname, program_type, start_time, end_time, length_aired, date_encoded, time_encoded, rel_type, VR_CODE1, VR_CODE2, VR_CODE3, VR_CODE4, DMA_CODE, sid_code, station_code)
(Select top(1)
Video_release, country, market_rank, Designated_mrkt_area, station, network, date_aired, day_of_week, day_part, half_hour_aired, programname, program_type, start_time, end_time, length_aired, date_encoded, time_encoded, rel_type, VR_CODE1, VR_CODE2, VR_CODE3, VR_CODE4, DMA_CODE, sid_code, station_code
from sigmastagetemp s1
where s1.video_release = @videorelease and
s1.programname = @pgname and
s1.network = @network and
s1.date_encoded = @dateencoded and
convert(int,s1.time_encoded) between convert(int,@timeencoded) -2 and
convert(int,@timeencoded) +2
Order by video_release, programname, network, date_encoded, time_encoded )
Delete
from sigmastagetemp
where video_release = @videorelease and
programname = @pgname and
network = @network and
date_encoded = @dateencoded and
convert(int,time_encoded) between convert(int,@timeencoded) -2 and
convert(int,@timeencoded) +2
FETCH NEXT FROM localshowcursor INTO @videorelease, @pgname, @network, @dateencoded, @timeencoded, @count
END
CLOSE localshowcursor
DEALLOCATE localshowcursor