在 Snowflake 存储过程中,
示例
CREATE OR REPLACE PROCEDURE DBNAME.SCHEMANAME.PROCEDURENAME(
FIELD1 DATE,
FIELD2 DATE
)
RETURNS ARRAY
LANGUAGE SQL
EXECUTE AS CALLER
AS
DECLARE
COUNTER := 0;
START_DT DATE;
END_DT DATE;
WORKING_DT DATE;
WORKING_DT_ARRAY := [];
WITHIN_FORLOOP_ARRAY := [];
ELEMENTS_FOR_FORLOOP_ARRAY := [1,2,3,4,5];
BEGIN
WORKING_DT := $FIELD1;
START_DT := $FIELD1;
END_DT := $FIELD2;
WHILE (WORKING_DT <= END_DT) DO
FOR ELEMENT IN ELEMENTS_FOR_FORLOOP_ARRAY DO
WITHIN_FORLOOP_ARRAY := ARRAY_APPEND(WITHIN_FORLOOP_ARRAY, WORKING_DT);
END FOR;
-- UPDATE WORKING_DT FOR NEXT WHILE LOOP ITERATION
WORKING_DT_ARRAY := ARRAY_APPEND(WORKING_DT_ARRAY, WORKING_DT);
WORKING_DT := DATEADD(DAY, 1, WORKING_DT);
COUNTER := COUNTER+1;
END WHILE;
RETURN WITHIN_FORLOOP_ARRAY;
-- RETURN WORKING_DT_ARRAY;
END;
-- Calling this procedure below returns
-- ['2024-01-01','2024-01-01','2024-01-01','2024-01-01','2024-01-01']
CALL DBNAME.SCHEMANAME.PROCEDURENAME('2024-01-01', '2024-01-02');
-- If I comment out 'RETURN WITHIN_FORLOOP_ARRAY;'
-- and uncomment 'RETURN WORKING_DT_ARRAY;'
-- Calling this procedure below returns
-- ['2024-01-01','2024-01-02']
CALL DBNAME.SCHEMANAME.PROCEDURENAME('2024-01-01', '2024-01-02');
所以问题是,
如果我获取代码并将其作为块运行,并对输入进行硬编码:
DECLARE
COUNTER number := 0;
START_DT DATE;
END_DT DATE;
WORKING_DT DATE;
WORKING_DT_ARRAY := [];
WITHIN_FORLOOP_ARRAY := [];
ELEMENTS_FOR_FORLOOP_ARRAY := [1,2,3,4,5];
BEGIN
WORKING_DT := '2024-01-01'::date;
START_DT := '2024-01-01'::date;
END_DT := '2024-01-02'::date;
WHILE (WORKING_DT <= END_DT) DO
FOR ELEMENT IN ELEMENTS_FOR_FORLOOP_ARRAY DO
WITHIN_FORLOOP_ARRAY := ARRAY_APPEND(WITHIN_FORLOOP_ARRAY, WORKING_DT);
END FOR;
-- UPDATE WORKING_DT FOR NEXT WHILE LOOP ITERATION
WORKING_DT_ARRAY := ARRAY_APPEND(WORKING_DT_ARRAY, WORKING_DT);
WORKING_DT := DATEADD(DAY, 1, WORKING_DT);
COUNTER := COUNTER+1;
END WHILE;
RETURN WITHIN_FORLOOP_ARRAY;
--RETURN WORKING_DT_ARRAY;
END;
我得到了你期望的结果:
如果我将它们更改为变量:
set FIELD1 = '2024-01-01'::date;
set FIELD2 = '2024-01-02'::date;
DECLARE
COUNTER number := 0;
START_DT DATE;
END_DT DATE;
WORKING_DT DATE;
WORKING_DT_ARRAY := [];
WITHIN_FORLOOP_ARRAY := [];
ELEMENTS_FOR_FORLOOP_ARRAY := [1,2,3,4,5];
BEGIN
WORKING_DT := $FIELD1;
START_DT := $FIELD1;
END_DT := $FIELD2;
WHILE (WORKING_DT <= END_DT) DO
FOR ELEMENT IN ELEMENTS_FOR_FORLOOP_ARRAY DO
WITHIN_FORLOOP_ARRAY := ARRAY_APPEND(WITHIN_FORLOOP_ARRAY, WORKING_DT);
END FOR;
-- UPDATE WORKING_DT FOR NEXT WHILE LOOP ITERATION
WORKING_DT_ARRAY := ARRAY_APPEND(WORKING_DT_ARRAY, WORKING_DT);
WORKING_DT := DATEADD(DAY, 1, WORKING_DT);
COUNTER := COUNTER+1;
END WHILE;
RETURN WITHIN_FORLOOP_ARRAY;
--RETURN WORKING_DT_ARRAY;
END;
按预期工作...让我们尝试一下该功能:
它也如您所期望的那样工作: