How Can I Skip a row(an iteration) in MSSQL Cursor based on some condition, I have a DTS which migrates thousands of records and based on some criteria, some records need not be migrated as they are duplicates and want to skip these records.
Any idea how I can accomplish this in MSSQL Cursor?
I guess the simplest way is to write IF statement inside the cursor. If the condition will be false you will skip records.
DECLARE @ID INT
DECLARE Curs CURSOR FAST_FORWARD
DECLARE @Cnt INT
CREATE TABLE ##Duplicates (ID INT, CarColor VARCHAR (50) )
FOR
SELECT DISTINCT CarID
FROM dbo.CarPark
WHERE CarColour <> 'red'
ORDER BY CarID
OPEN Curs
FETCH NEXT FROM Curs INTO @ID
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT INTO ##Duplicates
SELECT CarID, CarColor
FROM dbo.CarPark
WHERE ID = @ID;
SET @Cnt = (SELECT Count(*) FROM ##Duplicates WHERE ID = @ID) ;
IF @Cnt < 2
THEN /* Migrate */
ELSE PRINT 'Duplicate'
END
FETCH NEXT FROM Curs INTO @ID
END
CLOSE Curs
DEALLOCATE Curs;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With