Fetch inside a loop is considered a complex pattern, this could degrade Snowflake performance
Severity
Low
Description
This warning is used to indicate that a FETCH statement was found inside a loop. This statement allows one to retrieve rows from a result set one at a time and perform some processing on each row.
This scenario could be considered a complex pattern when it involves processing a large number of rows or requires complex logic to handle the data. E.g. if the cursor fetch inside a loop construct requires joining multiple tables or performing complex calculations on the data, it can become difficult to manage and may not scale well for large datasets.
Code Example
Teradata
IN -> Teradata_01.sql
REPLACEPROCEDURE teradata_fetch_inside_loop()DYNAMIC RESULT SETS1BEGINDECLARE col_name VARCHAR(200);DECLARE col_int INTEGERDEFAULT0;DECLARE cursor_var CURSORFORSELECT some_column FROM tabla1;WHILE (col_int <>0) DO FETCH cursor_var INTO col_name;SET col_int = col_int +1;ENDWHILE;END;
OUT -> Teradata_01.sql
--** SSC-FDM-0007 - MISSING DEPENDENT OBJECT "tabla1" **CREATEORREPLACEPROCEDURE teradata_fetch_inside_loop ()RETURNSVARCHARLANGUAGESQLCOMMENT = '{ "origin": "sf_sc", "name": "snowconvert", "version": { "major": 0, "minor": 0, "patch": "0" }, "attributes": { "component": "teradata", "convertedOn": "07/04/2024" }}'
EXECUTEASCALLERAS$$DECLARE col_name VARCHAR(200); col_int INTEGERDEFAULT0;BEGIN LET cursor_var CURSORFORSELECT some_column FROM tabla1;WHILE (col_int <>0) LOOP --** SSC-PRF-0003 - FETCH INSIDE A LOOP IS CONSIDERED A COMPLEX PATTERN, THIS COULD DEGRADE SNOWFLAKE PERFORMANCE. **
FETCH cursor_var INTO col_name; col_int := col_int +1;ENDLOOP;END;$$;
Oracle
IN -> Oracle_01.sql
CREATEPROCEDURE oracle_fetch_inside_loopIS var1 table1.column1%TYPE;CURSOR cursor1 ISSELECT COLUMN_NAME FROM table1; BEGINWHILE true LOOPFETCH cursor1 INTO var1; EXIT WHEN cursor1%NOTFOUND;ENDLOOP; END;
OUT -> Oracle_01.sql
CREATEORREPLACEPROCEDURE oracle_fetch_inside_loop ()RETURNSVARCHARLANGUAGESQLCOMMENT = '{"origin":"sf_sc","name":"snowconvert","version":{"major":1, "minor":0},{"attributes":{"component":"oracle"}}'
EXECUTEASCALLERAS$$DECLARE var1 VARIANT !!!RESOLVE EWI!!! /*** SSC-EWI-OR0129 - TYPE ATTRIBUTE 'table1.column1%TYPE' COULD NOT BE RESOLVED, SO IT WAS TRANSFORMED TO VARIANT ***/!!!;
cursor1 CURSORFORSELECT COLUMN_NAME FROM table1;BEGINWHILE (true) LOOP --** SSC-PRF-0003 - FETCH INSIDE A LOOP IS CONSIDERED A COMPLEX PATTERN, THIS COULD DEGRADE SNOWFLAKE PERFORMANCE. **
FETCH cursor1 INTO :var1;IF (var1 ISNULL) THEN EXIT;ENDIF;ENDLOOP;END;$$;
SQL Server
IN -> SqlServer_01.sql
CREATEORALTERPROCEDURE transact_fetch_inside_loopASBEGINDECLARE cursor1 CURSORFORSELECT col1 FROM my_table;WHILE1=0BEGINFETCHNEXTFROM @cursor1 INTO @variable1;ENDEND;
OUT -> SqlServer_01.sql
CREATEORREPLACEPROCEDURE transact_fetch_inside_loop ()RETURNSVARCHARLANGUAGESQLCOMMENT = '{"origin":"sf_sc","name":"snowconvert","version":{"major":1, "minor":0},{"attributes":{"component":"transact"}}'
EXECUTEASCALLERAS$$DECLARE--** SSC-FDM-TS0013 - SNOWFLAKE SCRIPTING CURSOR ROWS ARE NOT MODIFIABLE ** cursor1 CURSOR