diff --git a/docs/cn/sql-reference/10-sql-commands/00-ddl/04-task/01-ddl-create_task.md b/docs/cn/sql-reference/10-sql-commands/00-ddl/04-task/01-ddl-create_task.md index 4dea8b8afc..567e3623ce 100644 --- a/docs/cn/sql-reference/10-sql-commands/00-ddl/04-task/01-ddl-create_task.md +++ b/docs/cn/sql-reference/10-sql-commands/00-ddl/04-task/01-ddl-create_task.md @@ -24,9 +24,16 @@ CREATE [ OR REPLACE ] TASK [ IF NOT EXISTS ] [ COMMENT = '' ] [ = [ , = ... ] ] AS - +{ +| BEGIN + ; + [ ; ... ] + END; +} ``` +若需执行多条 SQL 语句,请将其置于 `BEGIN ... END;` 块中作为脚本,以确保按顺序执行。 + | 参数 | 描述 | | ------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | IF NOT EXISTS | 可选。若指定,则仅当同名任务不存在时才创建任务。 | @@ -39,7 +46,7 @@ AS | [ERROR_INTEGRATION](../16-notification/index.md) | 可选。用于任务错误通知的集成名称,附带特定的[任务错误负载](./10-task-error-integration-payload.md)。 | | COMMENT | 可选。作为任务注释或描述的字符串字面量。 | | session_parameter | 可选。指定任务运行时的会话参数。注意:会话参数必须放在 CREATE TASK 语句中所有其他任务参数之后。 | -| sql | 任务将执行的 SQL 语句,可为单条语句或脚本。必填。 | +| sql | 任务将执行的 SQL 语句。可为单条语句,也可为置于 `BEGIN ... END;` 块中的脚本。必填。 | ### 使用须知 @@ -119,6 +126,21 @@ AS 本例创建名为 `my_daily_task` 的任务,使用 **compute_wh** 计算集群,将 source_table 的数据插入 summary_table,并按 **CRON 表达式**于**太平洋时间每天上午 9 点**执行。 +### 多语句脚本 + +```sql +CREATE TASK IF NOT EXISTS nightly_refresh + WAREHOUSE = 'etl' + SCHEDULE = USING CRON '0 0 2 * * *' 'UTC' +AS +BEGIN + DELETE FROM staging.events WHERE event_time < DATEADD(DAY, -1, CURRENT_TIMESTAMP()); + INSERT INTO mart.events SELECT * FROM staging.events; +END; +``` + +本例创建名为 `nightly_refresh` 的任务,脚本通过将多条语句置于 `BEGIN ... END;` 块中,确保每次执行时先删除过期数据,再插入最新数据。 + ### 自动挂起 ```sql diff --git a/docs/en/sql-reference/10-sql-commands/00-ddl/04-task/01-ddl-create_task.md b/docs/en/sql-reference/10-sql-commands/00-ddl/04-task/01-ddl-create_task.md index 5bf270efc3..e9f493ac17 100644 --- a/docs/en/sql-reference/10-sql-commands/00-ddl/04-task/01-ddl-create_task.md +++ b/docs/en/sql-reference/10-sql-commands/00-ddl/04-task/01-ddl-create_task.md @@ -24,9 +24,16 @@ CREATE [ OR REPLACE ] TASK [ IF NOT EXISTS ] [ COMMENT = '' ] [ = [ , = ... ] ] AS - +{ +| BEGIN + ; + [ ; ... ] + END; +} ``` +Wrap multiple SQL statements in a `BEGIN ... END;` block so the task executes them sequentially as a script. + | Parameter | Description | | ------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | IF NOT EXISTS | Optional. If specified, the task will only be created if a task of the same name does not already exist. | @@ -39,7 +46,7 @@ AS | [ERROR_INTEGRATION](../16-notification/index.md) | Optional. The name of the notification integration to use for the task error notification with specific [task error payload ](./10-task-error-integration-payload.md)applied | | COMMENT | Optional. A string literal that serves as a comment or description for the task. | | session_parameter | Optional. Specifies session parameters to use for the task during task run. Note that session parameters must be placed after all other task parameters in the CREATE TASK statement. | -| sql | The SQL statement that the task will execute, it could be a single statement or a script This is a mandatory field. | +| sql | The SQL statement that the task will execute. It can be a single statement or a script wrapped in `BEGIN ... END;`. This is a mandatory field. | ### Usage Notes @@ -119,6 +126,21 @@ AS In this example, a task named `my_daily_task` is created. It uses the **compute_wh** warehouse to run a SQL statement that inserts data into summary_table from source_table. The task is scheduled to run using a **CRON expression** that executes **daily at 9 AM Pacific Time**. +### Multiple Statements + +```sql +CREATE TASK IF NOT EXISTS nightly_refresh + WAREHOUSE = 'etl' + SCHEDULE = USING CRON '0 0 2 * * *' 'UTC' +AS +BEGIN + DELETE FROM staging.events WHERE event_time < DATEADD(DAY, -1, CURRENT_TIMESTAMP()); + INSERT INTO mart.events SELECT * FROM staging.events; +END; +``` + +This example creates a task named `nightly_refresh` that executes a script containing multiple statements. The script is wrapped in `BEGIN ... END;` so the DELETE runs before the INSERT each time the task executes. + ### Automatic Suspension ```sql