将execute语句从azure sql转换为snowflake

kt06eoxx  于 2021-09-23  发布在  Java
关注(0)|答案(2)|浏览(359)

我正在尝试将下面的azure sql存储过程转换为snowflake。但我还没有找到exec语句的替代方案:

CREATE PROC SAMPLE_PROC
AS  
BEGIN  
 DECLARE @BusinessUnitKey INT=(SELECT BusinessUnitKey FROM BusinessUnit WHERE BusinessUnitName='ABC')  ;
 DECLARE @LoadDate DATETIME=  (SELECT Cast(GETUTCDATE() as Date))  ;
 DECLARE @DataLoadLogKey INT = (  
   SELECT MAX(DataLoadLogKey)  
   FROM DataLoadLog  
   WHERE BusinessUnitKey = @BusinessUnitKey  
   )  
  ,@TableName VARCHAR(100) = 'ProductType'  
  ,@StoredProcName VARCHAR(100) = (object_name(@@procid))  
  ,@StarDateTime DATETIME = @LoadDate  
  ,@EndDateTime DATETIME = NULL  
  ,@Status VARCHAR(100) = 'In Progress'  
  ,@LoadDescription VARCHAR(1000) = 'Loading Data'  ;

 EXEC dbo.usp_procedure @DataLoadLogKey = @DataLoadLogKey  
  ,@TableName = @TableName  
  ,@StoredProcName = @StoredProcName  
  ,@StarDateTime = @StarDateTime  
  ,@EndDateTime = NULL  
  ,@Status = @Status  
  ,@LoadDescription = @LoadDescription ; 
END;

有人能在snowflake中提供相应的execute语句吗?

f5emj3cl

f5emj3cl1#

雪花存储过程是用javascript编写的,它可以通过调用javascript api来执行sql语句。此api类似于雪花连接器和驱动程序(node.js、jdbc、python等)中的api,但不完全相同。
细节:https://docs.snowflake.com/en/sql-reference/stored-procedures-usage.html#implementation-及空气污染指数
您可以找到几个代码示例:https://docs.snowflake.com/en/sql-reference/stored-procedures-usage.html#examples

t0ybt7op

t0ybt7op2#

请看一下文档中使用存储过程的步骤。
这是一个应该编译的粗略存根。我无法测试代码,因为我没有用于验证表或数据的ddl,但这应该给您一个好的开始。
顺致敬意,

CREATE OR REPLACE PROCEDURE SAMPLE_PROC()
returns VARCHAR not null
language javascript
as 
$$

var businessUnitKey , loadDate, dataLoadLogKey, sql,  statement, rs, message ;

message= "FAILED";

// get business unit key
sql = `SELECT BusinessUnitKey FROM BusinessUnit WHERE BusinessUnitName='ABC'`;
statement = snowflake.createStatement({sqlText: sql});
result_set = statement.execute();

while (result_set.next())  {
    businessUnitKey = result_set.getColumnValue(1);
}
// get load date 
sql = `SELECT Cast(SYSDATE() as Date)`;
statement = snowflake.createStatement({sqlText: sql});
result_set = statement.execute();
while (result_set.next())  {
loadDate = result_set.getColumnValue(1);
}

//return loadDate
sql = `SELECT MAX(DataLoadLogKey)  
   FROM DataLoadLog  
   WHERE BusinessUnitKey = ` + businessUnitKey + `;` 
statement = snowflake.createStatement({sqlText: sql});
result_set = statement.execute();
while (result_set.next())  {
dataLoadLogKey = result_set.getColumnValue(1);
}

var TableName       = 'ProductType'  
  ,StoredProcName   = 'SAMPLE_PROC()'
  ,StarDateTime     = LoadDate  
  ,EndDateTime      = NULL  
  ,Status           = 'In Progress'  
  ,LoadDescription  = 'Loading Data'  ;

sql = `CALL dbo.usp_procedure(:1,:2,:3,:4,:5,:6,:7); `
statement = snowflake.createStatement({
        sqlText: sql,
        binds: [dataLoadLogKey,TableName,StoredProcName,StarDateTime,EndDateTime,Status,LoadDescription]
    });

message= "SUCCESSFUL";
return message;

$$;

call SAMPLE_PROC();

相关问题