从Azure Function App调用雪花程序

时间:2020-03-11 09:18:35

标签: snowflake-cloud-data-platform azure-function-app snowflake-schema

我在Snowflake中有一个过程,想从我的计时器触发的Azure函数应用程序中调用它。
该过程需要一个字符串类型的参数。以下是我的代码段,用于连接到Snowflake并使用参数调用该过程。

using (IDbConnection conn = new SnowflakeDbConnection())
{
    //Connect to Snowflake
    conn.ConnectionString = Environment.GetEnvironmentVariable("SnowflakeConnection");
    conn.Open();
    using (IDbCommand cmd = conn.CreateCommand())
    {
        if (conn.State == ConnectionState.Open)
        {
            cmd.CommandText = "SP_Snowflake_Procedure";
            //cmd.CommandType = CommandType.StoredProcedure;
            var date = cmd.CreateParameter();
            date.ParameterName = "RUNDATE";
            date.DbType = DbType.String;
            date.Value = "2018-01-01";
            cmd.Parameters.Add(date);
            using (IDataReader dr = cmd.ExecuteReader())
            {
                /****************
                 Logic to work on data 
                 received from SP
                *****************/
            }
        }
    }
}

当控件进入cmd.ExecuteReader()时,它将失败并显示错误:
Snowflake.Data:SQL编译错误:位置0处的语法错误第1行出现意外的“ SP_Snowflake_Procedure”。

我不了解这个雪花,如何调用过程。我想到了,它与MS SQL相似。但是我错了。我什至找不到合适的相关文件。
我可以使用相同的代码,而无需过程调用,但可以使用简单的 SELECT 语句,并且运行良好。
建议我在这里进行任何更改。

1 个答案:

答案 0 :(得分:1)

我无法从代码中得知您是使用Snowflake的ODBC驱动程序还是使用Snowflake的.NET驱动程序。 ODBC驱动程序比.NET驱动程序支持更多功能,但是我认为两者都应支持执行SP。

您将需要使用执行查询的SQL语句进行调用(与执行非查询的方法相对)。它将返回一个带有单行的表,其中包含SP的返回值。它将包含一个带有SP名称和SP标量值的列(基本上是在Web UI中运行时返回到SQL工作表的值)。

如果需要一个简单的SP,这里有一个示例SP:

create or replace procedure EchoString(stringValue string)
returns VARCHAR
language JavaScript
as
  $$  

  // Note that variables passed to Snowflake stored procedures
  // muat be all CAPITAL letters when used in the body of the 
  // procedure code. 
  return STRINGVALUE

  $$;

--Run the stored procedure to echo the value.
call EchoString('Echo this string.');

以下是使用ODBC连接从C#项目中调用SP的方法:

OdbcConnection DbConnection = new OdbcConnection("DSN=Snowflake;pwd=******");
OdbcCommand DbCommandSetup = DbConnection.CreateCommand();
DbConnection.Open();

// These two lines are only required if you get a message about no running warehouse.
// It will depend on how your calling user is set up in Snowflake.
DbCommandSetup.CommandText = "use warehouse TEST;";
DbCommandSetup.ExecuteNonQuery();

OdbcCommand DbCommand = DbConnection.CreateCommand();
DbCommand.CommandText = "call TEST.PUBLIC.ECHOSTRING('Echo this string.')";
OdbcDataReader DbReader = DbCommand.ExecuteReader();

// Note: If you define a Snowflake SP, DB, or schema in mixed case without double quoting
// the name, Snowflake will uppercase it in the catalog. You can call it from here without
// converting to upper case as long as it's not double quoted (escaped \") in the string.