Learn how to call Snowflake stored procedures, execute Snowflake Scripting blocks, and use user-defined functions with dotConnect for Snowflake.
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:
You can pass arguments using named parameters (:a), positional parameters (?), or named procedure arguments in any order:
For more information about parameters, see Using Parameters.
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:
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)).
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.
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.
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 in SQL statements in the same way as built-in functions. You can pass function arguments as command parameters:
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.