雪花存储过程复制到临时表中

时间:2020-07-13 20:16:43

标签: snowflake-cloud-data-platform java-stored-procedures snowsql

问题与Snowflake和Snowsql有关。也就是说,我正在尝试在存储的proc中创建一个临时表,然后从天蓝色的blob存储中“复制”到该临时表中。 我手动执行了snow sql语句,它们工作正常。 陈述1:CREATE TEMPORARY TABLE DB.TABLES.LINE_DETAILS_INCREMENTAL LIKE DB.TABLES.LINE_DETAILS;

声明2:

COPY INTO DB.TABLES.LINE_DETAILS_INCREMENTAL FROM (SELECT * FROM @DB.BASE.Azure/S/LINE_DETAILS_INCREMENTAL )
force = false file_format = (type = csv field_delimiter = '|' encoding = 'Windows 1252' skip_header = 0);

但是当我将其封装到一个存储的proc中并尝试运行它时,会出现错误: “ JavaScript编译错误:未捕获的语法错误:在'var insert_clause ='COPY INTO DB.TABLES.LINE_DETAILS_INCREMENTAL FROM(SELECT * FROM @ Feeds_DB.BASE.Azure / S / LINE_DETAILS_INCREMENTAL)中的SP_DELETE_LINE_DETAILS中的意外标识符=类型= true field_delimiter ='|'编码='Windows1252'skip_header = 0)On_error =继续;“位置288”。 存储的proc的代码是:-

CREATE or replace procedure  "DB"."TABLES"."SP_DELETE_INSERT_3DAYS_INTO_LINE_DETAILS"()
returns varchar(1000)
language javascript
as
$$
try{
var create_clause = 'CREATE TEMPORARY TABLE DB.TABLES.LINE_DETAILS_INCREMENTAL LIKE DB.TABLES.PS_TRANSACTION_LINE_DETAILS;'
        var create_stmt  = snowflake.createStatement({sqlText: create_clause});   
        var create_res = create_stmt.execute();
        
        var insert_clause = 'COPY INTO DB.TABLES.LINE_DETAILS_INCREMENTAL FROM (SELECT * FROM @Tableau_Feeds_DB.BASE_TABLES.JDAStagingAzure/POS/PS_TRANSACTION_LINE_DETAILS_INCREMENTAL ) force = true file_format = (type = csv field_delimiter = '|' encoding = 'Windows1252' skip_header = 0) On_error = continue;'
        var insert_stmt = snowflake.createStatement({sqlText: insert_clause});  
        var insert_res = insert_stmt.execute();
        var select_clause = 'select distinct TO_CHAR(TO_DATE(CREATE_DATE)) as CREATE_DATE from DB.TABLES.LINE_DETAILS_INCREMENTAL order by CREATE_DATE';
        var select_stmt  = snowflake.createStatement({sqlText: select_clause});
        var select_res   = select_stmt.execute();
 while (select_res.next()) 
            {
            date_ip = select_res.getColumnValue(1);
            var desc_user_sql  = `delete from DB.TABLES.LINE_DETAILS where TO_DATE(CREATE_DATE) = :1;`
            var desc_user_stmt  = snowflake.createStatement({sqlText: desc_user_sql, binds: [date_ip]});

            var desc_user_sql2  = `INSERT INTO DB.TABLES.PS_TRANSACTION_LINE_DETAILS select * from DB.TABLES.PS_TRANSACTION_LINE_DETAILS_INCREMENTAL where TO_DATE(CREATE_DATE) = :1;`
            var desc_user_stmt2  = snowflake.createStatement({sqlText: desc_user_sql2, binds: [date_ip]});

            try{
                       desc_user_stmt.execute();
                       desc_user_stmt2.execute();
                  }
               catch(err)
                  {
                   return "Error inserting records: " +err;
                  }              
            }
            return "Data has been insert in success!";
        }
        
        catch(err){
               return "Error whileselecting Roles : " +err;
        }
return 0;
$$

2 个答案:

答案 0 :(得分:1)

我认为您遇到的问题是,您正在使用单引号来开始/结束字符串以及字符串本身。

例如,您的字符串中包含以下短语:

encoding = 'Windows1252'

我建议使用反斜杠将其他引号引起来,例如:

encoding = \'Windows1252\'

对所有其他引号执行此操作,就可以了。

让我知道您之后是否仍然遇到问题!

答案 1 :(得分:1)

尝试如下更改单引号

var insert_clause = `COPY INTO DB.TABLES.LINE_DETAILS_INCREMENTAL FROM (SELECT * FROM @Tableau_Feeds_DB.BASE_TABLES.JDAStagingAzure/POS/PS_TRANSACTION_LINE_DETAILS_INCREMENTAL ) force = true file_format = (type = csv field_delimiter = '|' encoding = 'Windows1252' skip_header = 0) On_error = continue;`

https://docs.snowflake.com/en/sql-reference/stored-procedures-usage.html#line-continuation