这个非常简单的例子在 Snowflake 中失败了:
CREATE OR REPLACE PROCEDURE example_procedure()
RETURNS TABLE (example_col STRING)
LANGUAGE SQL
AS
$$
DECLARE
res resultset;
BEGIN
WITH sample_cte AS (
SELECT 'data' AS example_col
)
res := SELECT example_col FROM sample_cte;
return table(res);
END;
$$;
错误令人困惑:
Syntax error: unexpected 'sample_cte'. (line 5)
syntax error line 5 at position 20 unexpected 'AS'. (line 5)
我真的很想避免构建查询字符串,然后
execute immediate :query
,因为它看起来像这样应该工作。
尝试一下,您可以做的一种方法是使用 CTE SQL 语句创建视图并在过程中使用该视图。
CREATE OR REPLACE PROCEDURE example_procedure()
RETURNS TABLE (example_col varchar2(1000))
LANGUAGE SQL
AS
DECLARE
res resultset;
BEGIN
WITH sample_cte AS (
SELECT 'data' AS example_col
)SELECT example_col FROM sample_cte;
res := (select * from table(result_scan(last_query_id())) );
return table(res);
END;