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
)
convert if-else statement inside a cursor in a set-based approach
Question
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
OTHER TIPS
Count
is highly ineffective when checking if record exists, you would be much better with not exists
IF NOT EXISTS (
SELECT *
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
)
If this is till to slow show your full query and maybe it will be possible to make it set-based.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow