Snowflake - 过程已创建但执行时失败

问题描述 投票:0回答:1

我有一个相当长的存储过程,但我认为下一个存储过程反映了它的作用:

CREATE OR REPLACE PROCEDURE db.schema.sp_1(
    param_col_1 VARCHAR,
    param_col_2 VARCHAR,
    param_col_3 VARCHAR,
    param_col_7 VARCHAR,
    param_col_8 VARCHAR,
    param_col_9 VARCHAR,
    param_col_10 VARCHAR,
    param_col_11 INTEGER,
    param_col_12 INTEGER,
    param_json_info_1 VARCHAR,
    param_json_info_2 VARCHAR,
    param_json_info_3 VARCHAR,
    param_json_info_4 VARCHAR)
RETURNS TABLE(
    column1 VARCHAR(16777216),
    column2 DATE,
    column3 VARCHAR(16777216),
    column4 VARCHAR(16777216),
    column5 DATE,
    column6 DATE,
    column7 VARCHAR(16777216),
    column8 VARCHAR(16777216),
    column9 VARCHAR(16777216),
    column10 FLOAT,
    column11 FLOAT,
    column12 FLOAT)
LANGUAGE SQL 
AS
$$
DECLARE
    res RESULTSET;
    query VARCHAR;
BEGIN
    query := 'WITH staging_1 AS (SELECT col0, col1 ,col2 ,col3 ,(col4 = "P") AS col4, col5, col6 FROM db.schema.table_1 WHERE col1 = "' || param_col_1 || '" AND col2 = "' || param_col_2 || '"::DATE AND iff("' || param_col_3 || '" IS NOT NULL, col3 = "' || param_col_3 || '", true)), staging_2 AS (SELECT stg1.col1 ,stg1.col2, stg1.col3, stg1.col4, stg1.col5, stg1.col6, stg2.col7, stg2.col8, stg2.col9, stg2.col10, stg2.col11, stg2.col12, stg2.col13, stg2.col14, stg2.col15, stg2.col16, stg2.col17, stg.col18 FROM staging_1 stg1 LEFT JOIN db.schema.table_2 stg2 ON stg1.col0 = stg2.col0 WHERE iff("' || param_col_7 || '" IS NOT NULL, stg2.col7 = "' || param_col_7 || '", true) AND iff("' || param_col_8 || '" IS NOT NULL, stg2.col8 = "' || param_col_8 || '", true), last_filter AS (SELECT stg2.col1 ,stg2.col2, stg2.col3, stg2.col4, stg2.col5, stg2.col6, stg2.col7, stg2.col8, stg2.col9, stg2.col10, stg2.col11, stg2.col12, CASE  WHEN "' || param_col_9 || '" = "DATE" AND "' || param_col_10 || '" = "DESC" THEN ROW_NUMBER() OVER (ORDER BY stg2.col5 DESC, COALESCE(TIME(stg2.col5), TIME("00:00")) DESC) WHEN "' || param_col_9 || '" = "DATE" AND "' || param_col_10 || '" = "ASC" THEN ROW_NUMBER() OVER (ORDER BY stg2.col5 ASC, COALESCE(TIME(stg2.col5), TIME("00:00")) ASC) WHEN "' || param_col_9 || '" = "AMOUNT" AND "' || param_col_10 || '" = "DESC" THEN ROW_NUMBER() OVER (ORDER BY stg2.col10 DESC) WHEN "' || param_col_9 || '" = "AMOUNT" AND "' || param_col_10 || '" = "ASC" THEN ROW_NUMBER() OVER (ORDER BY stg2.col10 ASC) END row_num FROM staging_2 stg2 INNER JOIN (SELECT  DISTINCT VALUE:account::INT account,  VALUE:bankCode::VARCHAR bankCode,  VALUE:currency::VARCHAR currency FROM (SELECT  parse_json('' || param_json_info_1 || '') as src) src, lateral flatten(input => src) ) cts ON stg2.col13 = cts.account AND stg2.col14 = cts.bankCode AND stg2.col15 = cts.currency WHERE 1 = 1 and iff('' || param_json_info_2 || '' is not null, stg2.col16 in ( select VALUE:transactionType::INT from (  select parse_json('' || param_json_info_2 || '') as src ) src, lateral flatten(input => src) where value is not null ) , true) and iff('' || param_json_info_3 || '' is not null, stg2.col17 in ( select VALUE:storeCode::VARCHAR from ( select parse_json('' || param_json_info_3 || '') as src ) src, lateral flatten(input => src) where value is not null) , true) and stg2.col18 in ( select VALUE:paymentMethod::VARCHAR from ( select parse_json('' || param_json_info_4 || '') as src ) src, lateral flatten(input => src) where value is not null) QUALIFY row_num BETWEEN ' || param_col_11 || '::NUMBER(38,0) + 1 AND ' || param_col_11 || '::NUMBER(38,0) + ' || param_col_12 || '::NUMBER(38,0)) SELECT  col1 column1, col2 column2, col3 column3, col4 column4, col5 column5, col6 column6, col7 column7, col8 column8, col9 column9, col10 column10, col11 column11, col12 column12 FROM last_filter';
    res := (EXECUTE IMMEDIATE :query);
    RETURN TABLE(res);
END;
$$;

我用下一条语句调用此过程:

call db.schema.sp_1('XXXXXXXX','2024-05-06', null, null, null, 'DATE', 'DESC', 0, 200, '{"data":{"account":"11111111","bankCode":"99","currency":"840"}}', '{"data":{"transactionType":"100"}}', null, '{"data":{"paymentMethod":"C"}}');

但是出现这样的错误:

错误:第 7 行位置 10 处的“STATMENT_ERROR”:SQL 编译错误:(第 99 行) 解析错误行 1 位于 '' 附近的位置 11,021。 第 1 行语法错误位于位置 10,191 处出现意外的“AS”。 (第 99 行)

当然,如果我不放置实际的脚本,这个错误就没有任何意义,但是,我遵循了 sp 最终版本中当前拥有的所有内容(这个创建时没有错误)。

谢谢

stored-procedures snowflake-cloud-data-platform
1个回答
0
投票

看起来您的某些参数为 Null,这使得字符串“query”= Null,尝试执行以下过程,您将看到您正在执行的 SQL 为空。

call sp_1('XXXXXXXX','2024-05-06', null, null, null, 'DATE', 'DESC', 0, 200, '{"data":{"account":"11111111","bankCode":"99","currency":"840"}}', '{"data":{"transactionType":"100"}}', null, '{"data":{"paymentMethod":"C"}}');


CREATE OR REPLACE PROCEDURE sp_1(
    param_col_1 VARCHAR,
    param_col_2 VARCHAR,
    param_col_3 VARCHAR,
    param_col_7 VARCHAR,
    param_col_8 VARCHAR,
    param_col_9 VARCHAR,
    param_col_10 VARCHAR,
    param_col_11 INTEGER,
    param_col_12 INTEGER,
    param_json_info_1 VARCHAR,
    param_json_info_2 VARCHAR,
    param_json_info_3 VARCHAR,
    param_json_info_4 VARCHAR)
returns varchar(200)
LANGUAGE SQL 
AS
$$
DECLARE
    res RESULTSET;
    query VARCHAR;
    err  varchar;
BEGIN
    query := 'WITH staging_1 AS (SELECT col0, col1 ,col2 ,col3 ,(col4 = "P") AS col4, col5, col6 FROM db.schema.table_1 WHERE col1 = "' || param_col_1 || '" AND col2 = "' || param_col_2 || '"::DATE AND iff("' || param_col_3 || '" IS NOT NULL, col3 = "' || param_col_3 || '", true)), staging_2 AS (SELECT stg1.col1 ,stg1.col2, stg1.col3, stg1.col4, stg1.col5, stg1.col6, stg2.col7, stg2.col8, stg2.col9, stg2.col10, stg2.col11, stg2.col12, stg2.col13, stg2.col14, stg2.col15, stg2.col16, stg2.col17, stg.col18 FROM staging_1 stg1 LEFT JOIN db.schema.table_2 stg2 ON stg1.col0 = stg2.col0 WHERE iff("' || param_col_7 || '" IS NOT NULL, stg2.col7 = "' || param_col_7 || '", true) AND iff("' || param_col_8 || '" IS NOT NULL, stg2.col8 = "' || param_col_8 || '", true), last_filter AS (SELECT stg2.col1 ,stg2.col2, stg2.col3, stg2.col4, stg2.col5, stg2.col6, stg2.col7, stg2.col8, stg2.col9, stg2.col10, stg2.col11, stg2.col12, CASE  WHEN "' || param_col_9 || '" = "DATE" AND "' || param_col_10 || '" = "DESC" THEN ROW_NUMBER() OVER (ORDER BY stg2.col5 DESC, COALESCE(TIME(stg2.col5), TIME("00:00")) DESC) WHEN "' || param_col_9 || '" = "DATE" AND "' || param_col_10 || '" = "ASC" THEN ROW_NUMBER() OVER (ORDER BY stg2.col5 ASC, COALESCE(TIME(stg2.col5), TIME("00:00")) ASC) WHEN "' || param_col_9 || '" = "AMOUNT" AND "' || param_col_10 || '" = "DESC" THEN ROW_NUMBER() OVER (ORDER BY stg2.col10 DESC) WHEN "' || param_col_9 || '" = "AMOUNT" AND "' || param_col_10 || '" = "ASC" THEN ROW_NUMBER() OVER (ORDER BY stg2.col10 ASC) END row_num FROM staging_2 stg2 INNER JOIN (SELECT  DISTINCT VALUE:account::INT account,  VALUE:bankCode::VARCHAR bankCode,  VALUE:currency::VARCHAR currency FROM (SELECT  parse_json('' || param_json_info_1 || '') as src) src, lateral flatten(input => src) ) cts ON stg2.col13 = cts.account AND stg2.col14 = cts.bankCode AND stg2.col15 = cts.currency WHERE 1 = 1 and iff('' || param_json_info_2 || '' is not null, stg2.col16 in ( select VALUE:transactionType::INT from (  select parse_json('' || param_json_info_2 || '') as src ) src, lateral flatten(input => src) where value is not null ) , true) and iff('' || param_json_info_3 || '' is not null, stg2.col17 in ( select VALUE:storeCode::VARCHAR from ( select parse_json('' || param_json_info_3 || '') as src ) src, lateral flatten(input => src) where value is not null) , true) and stg2.col18 in ( select VALUE:paymentMethod::VARCHAR from ( select parse_json('' || param_json_info_4 || '') as src ) src, lateral flatten(input => src) where value is not null) QUALIFY row_num BETWEEN ' || param_col_11 || '::NUMBER(38,0) + 1 AND ' || param_col_11 || '::NUMBER(38,0) + ' || param_col_12 || '::NUMBER(38,0)) SELECT  col1 column1, col2 column2, col3 column3, col4 column4, col5 column5, col6 column6, col7 column7, col8 column8, col9 column9, col10 column10, col11 column11, col12 column12 FROM last_filter';
    res := (EXECUTE IMMEDIATE :query);
     return 'Sucess';
    exception
    when other then
    err := sqlerrm;
     RETURN (query);
END;
$$;
© www.soinside.com 2019 - 2024. All rights reserved.