Some parts of the output code are omitted for clarity reasons.
Severity
None
Description
This warning is used to indicate that the statement has usages of nested cursors. A cursor is a database object that allows traversing and manipulating result sets in a database. When nested cursors are used, it means that one cursor is nested inside the loop of another cursor.
The use of nested cursors can have a significant impact on performance, especially with large result sets. Each cursor operation requires a round trip to the database server, which can increase overhead and slow down execution.
Code examples
SQL Server
IN -> SqlServer_01.sql
CREATEORALTERPROCEDURE procedureSampleASBEGINDECLARE @outer_category_id INT, @outer_category_name NVARCHAR(50), @inner_product_name NVARCHAR(50);-- Define the outer cursorDECLARE outer_cursor CURSORFORSELECT category_id, category_name FROM categories;-- Open the outer cursorOPEN @outer_cursor;-- Fetch the first row from the outer cursorFETCHNEXTFROM outer_cursor INTO @outer_category_id, @outer_category_name;-- Start the outer loopWHILE @@FETCH_STATUS =0BEGINPRINT'Category: '+ @outer_category_name;-- Define the inner cursorDECLARE inner_cursor CURSORFORSELECT product_name FROM products WHERE category_id = @outer_category_id;-- Open the inner cursorOPEN inner_cursor;FETCHNEXTFROM inner_cursor INTO @inner_product_name;WHILE @@FETCH_STATUS =0BEGINPRINT'Product: '+ @inner_product_name +' Category: '+CAST(@outer_category_id ASNVARCHAR(10));-- Fetch the next row from the inner cursorFETCHNEXTFROM inner_cursor INTO @inner_product_name;END;-- Close the inner cursorCLOSE inner_cursor;DEALLOCATE inner_cursor;-- Fetch the next row from the outer cursorFETCHNEXTFROM outer_cursor INTO @outer_category_id, @outer_category_name;END;-- Close the outer cursorCLOSE outer_cursor;DEALLOCATE outer_cursor;END;
OUT -> SqlServer_01.sql
CREATEORREPLACEPROCEDURE procedureSample ()RETURNSVARCHARLANGUAGESQLCOMMENT = '{"origin":"sf_sc","name":"snowconvert","version":{"major":1, "minor":0},"attributes":{"component":"transact"}}'
EXECUTEASCALLERAS$$DECLARE OUTER_CATEGORY_ID INT; OUTER_CATEGORY_NAME VARCHAR(50); INNER_PRODUCT_NAME VARCHAR(50);-- Define the outer cursor--** SSC-FDM-TS0013 - SNOWFLAKE SCRIPTING CURSOR ROWS ARE NOT MODIFIABLE ** outer_cursor CURSORFORSELECT category_id, category_nameFROM categories;-- Define the inner cursor--** SSC-FDM-TS0013 - SNOWFLAKE SCRIPTING CURSOR ROWS ARE NOT MODIFIABLE ** inner_cursor CURSORFORSELECT product_nameFROM productsWHERE category_id = :OUTER_CATEGORY_ID;BEGIN-- Open the outer cursor--** SSC-PRF-0005 - THE STATEMENT BELOW HAS USAGES OF NESTED CURSORS. **OPEN OUTER_CURSOR;-- Fetch the first row from the outer cursorFETCH outer_cursorINTO :OUTER_CATEGORY_ID, :OUTER_CATEGORY_NAME;-- Start the outer loop-- Define the inner cursorWHILE (:FETCH_STATUS =0) LOOP !!!RESOLVE EWI!!! /*** SSC-EWI-0073 - PENDING FUNCTIONAL EQUIVALENCE REVIEW FOR 'PRINT' NODE ***/!!!PRINT'Category: '+ @outer_category_name;