How to run cursor in snowflake?

烈酒焚心 提交于 2021-02-11 15:13:56

问题


I have written below cursor in SQL and working file. But I am not able to run the same cursor on snowflake, please help.

DECLARE @CurrentMonth NVARCHAR(100)
DECLARE @CurrentMonth1 NVARCHAR(100)
DECLARE MYDateCURSOR CURSOR
DYNAMIC 
FOR
SELECT Collections_COE FROM [CollectionsAgeing_OTCN024_028_029]
OPEN MYDateCURSOR
FETCH LAST FROM MYDateCURSOR INTO @CurrentMonth
CLOSE MYDateCURSOR
DEALLOCATE MYDateCURSOR
--select  value from STRING_SPLIT(@CurrentMonth,'-') ;
select @CurrentMonth1=LEFT(@CurrentMonth,4)+cast(cast(RIGHT(@CurrentMonth,2) as int)-1 as varchar(2))
select Date, x1,y1,x1/y1 as DividedValue, round((x1/y1)-1,5) as Actual from(
SELECT  X.Past_Due_0 as x1,Y.Past_Due_0 as y1,X.Collections_COE as Date FROM [CollectionsAgeing_OTCN024_028_029] X
CROSS JOIN [CollectionsAgeing_OTCN024_028_029] Y
WHERE X.Collections_COE=@CurrentMonth and y.Collections_COE=@CurrentMonth1
)z

回答1:


Snowflake SQL does not support native SQL cursors. I believe you'll want to leverage a Javascript UDF, UDTF, or Stored Procedure (depending on what you want your output to be). Based on your example above, I believe a Javascript UDTF is what you need: https://docs.snowflake.net/manuals/sql-reference/udf-js-table-functions.html




回答2:


Snowflake SQL does not support native SQL cursors there are options which can be leveraged for your scenario. Please take a look at below links. Also please be informed that Snowflake's real processing power in terms of performance is when data is processed in bulk instead of processing data row by row.

https://community.snowflake.com/s/question/0D50Z00009f7StWSAU/i-have-written-below-cursor-in-sql-and-working-file-but-i-am-not-able-to-run-the-same-cursor-on-snowflake-please-help

https://docs.snowflake.com/en/user-guide/python-connector-example.html

https://docs.snowflake.com/en/sql-reference/udf-js-table-functions.html



来源:https://stackoverflow.com/questions/58482326/how-to-run-cursor-in-snowflake

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!