sqlt-sqlcursorset-based

convert if-else statement inside a cursor in a set-based approach


I have a script containing a cursor with if-else statement, but it takes too much times to browse the table. (a table with 79000 rows takes 1h). So i need to convert it in a set-based approach.

The if statement is

IF  ( 
        SELECT  count (b.key) 
          FROM general..ean a,
               general..mainframe b,
               general..hope c 
        WHERE a.ean = @ean
        AND a.c_suppression = '0' 
        AND a.key = b.key           
        AND b.key = c.key
        AND c.canal = @canal
    ) = 0

where @ean and @canal are value retrieved in each row with the cursor. The table browsed is tmp_day_house_info_corporate. So i need to retrieve all rows from tmp_day_house_info_corporate, for which @info and @canal in the if statement retrieve 0.

Thank you for any help.


Solution

  •     SELECT  *
          FROM tmp_day_house_info_corporate
        WHERE not exists(
            SELECT  b.key
              FROM general..ean a,
                   general..mainframe b,
                   general..hope c 
            WHERE a.ean = tmp_day_house_info_corporate.ean
            AND a.c_suppression = '0' 
            AND a.key = b.key           
            AND b.key = c.key
            AND c.canal = tmp_day_house_info_corporate.canal
        )