尝试在雪花中创建一个存储过程,该过程接受参数以使用来自推断架构的暂存文件的元数据为外部表生成“创建语句”并执行它。由于我对 Snowflake 存储过程还很陌生,真的很感谢有关如何实现这一点的输入吗?
CREATE OR REPLACE PROCEDURE DatabaseName.SchemaName.TEST_PROCEDURE(DB_NAME STRING, SCHEMA_NAME
STRING, TABLE_NAME STRING , FILEPATH STRING)
returns string
language javascript
as
$$
var cmd = `
SELECT
'CREATE OR REPLACE EXTERNAL TABLE ' || DB_NAME ||'.'||SCHEMA_NAME||'.'||TABLE_NAME|| ' ('
||
LISTAGG(
CLEAN_COLUMN || ' ' || TYPE || ' AS (' ||
'VALUE:'|| CLEAN_COLUMN || '::' || TYPE || ')', ', ') AS QUERY_TEXT
FROM (
SELECT *
,CASE WHEN COLUMN_NAME LIKE '%#%' THEN '"'||COLUMN_NAME||'"' ELSE COLUMN_NAME END AS CLEAN_COLUMN
from table(
INFER_SCHEMA(
LOCATION => '@db.src_xxx.stg_yyyy/2021-awdfg'
,FILE_FORMAT => '@db.src_xxx.parquet'
)
) A
)
`
var sql = snowflake.createStatement({sqlText: cmd});
var result = sql.execute();
return 'Success';
$$;