Currently, A Task Can Execute A Single SQL Statement, Including A Call To A Stored Procedure

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

TASKS

-----------------------------------------------------------------------------------------------

https://docs.snowflake.com/en/user-guide/tasks-intro.html

https://docs.snowflake.com/en/user-guide/tasks-intro.html#task-scheduling-and-daylight-saving-time

Task is a small set of SQL/Procedure which execute on specified timeframe.

Currently, a task can execute a single SQL statement, including a call to a stored procedure
Create Task:

CREATE [ OR REPLACE ] TASK [ IF NOT EXISTS ] <name>


WAREHOUSE = <string>
[ SCHEDULE = '{ <num> MINUTE | USING CRON <expr> <time_zone> }' ]
[ <session_parameter> = <value> [ , <session_parameter> = <value> ... ] ]
[ USER_TASK_TIMEOUT_MS = <num> ]
[ COPY GRANTS ]
[ COMMENT = '<string_literal>' ]
[ AFTER <string> ]
[ WHEN <boolean_expr> ]
AS
<sql>
= 0 5,17,20 2,3,8 1-3 sun-tue
2,4,6,8

28 2 * *

21,22
21-25
21
*

03,04
03-05
03
*
JAN-FEB
JAN,FEB
JAN

CREATE TASK mytask_hour


WAREHOUSE = mywh
SCHEDULE = 'USING CRON 0 9-17 * * SUN America/Los_Angeles'
TIMESTAMP_INPUT_FORMAT = 'YYYY-MM-DD HH24'
AS
INSERT INTO mytable(ts) VALUES(CURRENT_TIMESTAMP); (DDL,DML,procedures)
 * * * * * timezone

CREATE TASK mytask_minute


WAREHOUSE = mywh
SCHEDULE = '2 MINUTE'
AS
INSERT INTO mytable(ts) VALUES(CURRENT_TIMESTAMP);

-- Create a stored procedure that unloads data from a table


-- The COPY statement in the stored procedure unloads data to files in a path identified by epoch time (using the
Date.now() method)
create or replace procedure my_unload_sp()
returns string not null
language javascript
as
$$
var my_sql_command = ""
var my_sql_command = my_sql_command.concat("copy into @mystage","/",Date.now(),"/"," from mytable
overwrite=true;");
var statement1 = snowflake.createStatement( {sqlText: my_sql_command} );
var result_set1 = statement1.execute();
return my_sql_command; // Statement returned for info/debug purposes
$$;

-- Create a task that calls the stored procedure every hour


create task my_copy_task
warehouse = mywh
schedule = '60 minute'
as
call my_unload_sp();

Viewing the Task History for Your Account


The following roles (or roles with the specified privileges) can use SQL to view the task history within
a specified date range:

 Account administrator (i.e. users with the ACCOUNTADMIN role).


 Task owner (i.e. role that has the OWNERSHIP privilege on a task).
 Any role that has the global MONITOR EXECUTION privilege.
Creating a Task Administrator Role

For ease of use, we recommend creating a custom role (e.g. TASKADMIN) and assigning the
EXECUTE TASK privilege to this role. Any role that can grant privileges (e.g. SECURITYADMIN or
any role with the MANAGE GRANTS privilege) can then grant this custom role to any task owner
role to allow altering their own tasks. To remove the ability for the task owner role to execute the
task, it is only necessary to revoke this custom role from the task owner role. Note that if you choose
not to create this custom role, an account administrator must revoke the EXECUTE TASK privilege
from the task owner role.

For example, create a custom role name TASKADMIN and grant that role the EXECUTE TASK
privilege. Assign the TASKADMIN role to a task owner role named MYROLE:

use role securityadmin;

create role taskadmin;

-- set the active role to ACCOUNTADMIN before granting the EXECUTE TASK privilege to the new role
use role accountadmin;

grant execute task on account to role taskadmin;

-- set the active role to SECURITYADMIN to show that this role can grant a role to another role
use role securityadmin;

grant role taskadmin to role myrole;

Trouble Shooting tasks :


https://docs.snowflake.com/en/user-guide/tasks-ts.html

You might also like