dotConnect for Snowflake Documentation
On this page
    Using Stored Procedures and Functions
    On this page

    Learn how to call Snowflake stored procedures, execute Snowflake Scripting blocks, and use user-defined functions with dotConnect for Snowflake.

    Call Stored Procedures

    To call a Snowflake stored procedure, use the CALL statement with the CommandType property set to Text (the default).

    The CommandType.StoredProcedure command type isn't supported. Setting this command type throws a NotSupportedException with the message "Stored procedures are not supported in Snowflake."

    Create the following sample procedure to test the examples in this section:

    CREATE OR REPLACE PROCEDURE ADD_TWO(A INT, B INT)
    RETURNS INT
    LANGUAGE SQL
    AS
    $$
    BEGIN
      RETURN A + B;
    END;
    $$;

    Pass the procedure arguments as command parameters. To retrieve the return value, use the ExecuteScalar method:

    using (SnowflakeCommand command = new SnowflakeCommand("CALL ADD_TWO(:a, :b)", connection)) {
      command.Parameters.Add("a", DbType.Int64).Value = 2;
      command.Parameters.Add("b", DbType.Int64).Value = 3;
      long sum = (long)command.ExecuteScalar();   // 5
      Console.WriteLine(sum);
    }
    
    Using command As New SnowflakeCommand("CALL ADD_TWO(:a, :b)", connection)
      command.Parameters.Add("a", DbType.Int64).Value = 2
      command.Parameters.Add("b", DbType.Int64).Value = 3
      Dim sum As Long = CLng(command.ExecuteScalar())   ' 5
      Console.WriteLine(sum)
    End Using
    

    You can pass arguments using named parameters (:a), positional parameters (?), or named procedure arguments in any order:

    CALL ADD_TWO(:a, :b)
    CALL ADD_TWO(?, ?)
    CALL ADD_TWO(B => :y, A => :x)

    For more information about parameters, see Using Parameters.

    Call Procedures That Return a Table

    A stored procedure declared with RETURNS TABLE(...) returns a result set. To retrieve the result set, use the ExecuteReader method.

    The following example assumes that an ORDERS table with ID and STATUS columns exists in the current database and schema. Create the following procedure:

    CREATE OR REPLACE PROCEDURE GET_ORDERS(MINID INT)
    RETURNS TABLE(ID INT, STATUS STRING)
    LANGUAGE SQL
    AS
    $$
    DECLARE
      res RESULTSET DEFAULT (SELECT ID, STATUS FROM ORDERS WHERE ID >= :minid ORDER BY ID);
    BEGIN
      RETURN TABLE(res);
    END;
    $$;
    using (SnowflakeCommand command = new SnowflakeCommand("CALL GET_ORDERS(:minId)", connection)) {
      command.Parameters.Add("minId", DbType.Int64).Value = 2;
      using (SnowflakeDataReader reader = command.ExecuteReader()) {
        while (reader.Read())
          Console.WriteLine("{0}: {1}", reader.GetInt64(0), reader.IsDBNull(1) ? "" : reader.GetString(1));
      }
    }
    
    Using command As New SnowflakeCommand("CALL GET_ORDERS(:minId)", connection)
      command.Parameters.Add("minId", DbType.Int64).Value = 2
      Using reader As SnowflakeDataReader = command.ExecuteReader()
        While reader.Read()
          Console.WriteLine("{0}: {1}", reader.GetInt64(0), If(reader.IsDBNull(1), "", reader.GetString(1)))
        End While
      End Using
    End Using
    

    To pass command parameters to a stored procedure that returns a table, use CALL. Snowflake doesn't support parameterized procedure arguments in the FROM clause. For example, SELECT * FROM TABLE(GET_ORDERS(:minId)) results in the "Invalid argument :minId for procedure in FROM clause" error. To use a procedure in the FROM clause, pass literal argument values instead, for example, SELECT * FROM TABLE(GET_ORDERS(2)).

    Return Values and Output Parameters

    Use the ExecuteScalar method to retrieve a single value returned by a stored procedure, or the ExecuteReader method to retrieve a result set.

    A procedure that returns a single value produces a result set with one row and one column named after the procedure. A procedure that returns a table produces a tabular result set. The provider returns VARIANT, OBJECT, and ARRAY values as JSON text. For more information, see Working with Semi-Structured Data.

    The provider doesn't support output parameters. Setting the Direction property of a parameter to Output, InputOutput, or ReturnValue throws NotSupportedException.

    Create Stored Procedures

    To create a stored procedure, execute a CREATE PROCEDURE statement using a SnowflakeCommand object.

    The provider passes the Snowflake Scripting procedure body to Snowflake without modification. Snowflake processes variable and argument references prefixed with a colon, such as :minid in the previous example. Don't define command parameters for these references.

    Execute Anonymous Blocks

    You can execute a Snowflake Scripting block without creating a stored procedure, either directly or using EXECUTE IMMEDIATE. To retrieve the value returned by the RETURN statement, use the ExecuteScalar method:

    DECLARE x INT DEFAULT 5; BEGIN RETURN x * 2; END;
    EXECUTE IMMEDIATE $$ DECLARE x INT DEFAULT 5; BEGIN RETURN x * 2; END; $$

    To pass values to Snowflake Scripting code, create a stored procedure and call it using CALL. Command parameters aren't supported inside a scripting block. Snowflake interprets references such as :name as block variables, which results in an "invalid identifier" error.

    Call User-Defined Functions

    Call user-defined functions in SQL statements in the same way as built-in functions. You can pass function arguments as command parameters:

    CREATE OR REPLACE FUNCTION AREA(R FLOAT) RETURNS FLOAT AS 'PI() * R * R';
    SELECT AREA(:r);

    Retrieve Procedure and Function Metadata

    To retrieve metadata about stored procedures and functions, query Snowflake directly. The GetSchema method doesn't provide metadata collections for these objects. For example, use the following statements to retrieve procedure information:

    SHOW USER PROCEDURES IN SCHEMA your_database.your_schema;
    SELECT * FROM your_database.INFORMATION_SCHEMA.PROCEDURES;

    For more information about retrieving metadata, see Using GetSchema Method.