CREATE DYNAMIC TABLE¶
根据指定的查询创建 动态表。
This command supports the following variants:
CREATE OR ALTER DYNAMIC TABLE: Creates a dynamic table if it doesn't exist or alters an existing dynamic table.
CREATE DYNAMIC TABLE FROM SNAPSHOT SET: Restores a dynamic table from a back up.
CREATE DYNAMIC TABLE ...CLONE: Creates a clone of an existing dynamic table.
CREATE DYNAMIC ICEBERG TABLE: Creates a dynamic Apache Iceberg™ table.
- 另请参阅:
ALTER DYNAMIC TABLE, DESCRIBE DYNAMIC TABLE, DROP DYNAMIC TABLE , SHOW DYNAMIC TABLES, CREATE OR ALTER <对象>
本主题内容:
语法¶
CREATE [ OR REPLACE ] [ TRANSIENT ] DYNAMIC TABLE [ IF NOT EXISTS ] <name> (
-- Column definition
<col_name> <col_type>
[ [ WITH ] MASKING POLICY <policy_name> [ USING ( <col_name> , <cond_col1> , ... ) ] ]
[ [ WITH ] PROJECTION POLICY <policy_name> ]
[ [ WITH ] TAG ( <tag_name> = '<tag_value>' [ , <tag_name> = '<tag_value>' , ... ] ) ]
[ WITH CONTACT ( <purpose> = <contact_name> [ , <purpose> = <contact_name> ... ] ) ]
[ COMMENT '<string_literal>' ]
-- Additional column definitions
[ , <col_name> <col_type> [ ... ] ]
)
TARGET_LAG = { '<num> { seconds | minutes | hours | days }' | DOWNSTREAM }
WAREHOUSE = <warehouse_name>
[ REFRESH_MODE = { AUTO | FULL | INCREMENTAL } ]
[ INITIALIZE = { ON_CREATE | ON_SCHEDULE } ]
[ CLUSTER BY ( <expr> [ , <expr> , ... ] ) ]
[ DATA_RETENTION_TIME_IN_DAYS = <integer> ]
[ MAX_DATA_EXTENSION_TIME_IN_DAYS = <integer> ]
[ COMMENT = '<string_literal>' ]
[ COPY GRANTS ]
[ [ WITH ] ROW ACCESS POLICY <policy_name> ON ( <col_name> [ , <col_name> ... ] ) ]
[ [ WITH ] AGGREGATION POLICY <policy_name> [ ENTITY KEY ( <col_name> [ , <col_name> ... ] ) ] ]
[ [ WITH ] TAG ( <tag_name> = '<tag_value>' [ , <tag_name> = '<tag_value>' , ... ] ) ]
[ REQUIRE USER ]
[ IMMUTABLE WHERE ( <expr> ) ]
[ BACKFILL FROM ]
AS <query>
变体语法¶
CREATE OR ALTER DYNAMIC TABLE¶
CREATE OR ALTER DYNAMIC TABLE <name> (
-- Column definition
<col_name> <col_type>
[ COLLATE '<collation_specification>' ]
[ COMMENT '<string_literal>' ]
-- Additional column definitions
[ , <col_name> <col_type> [ ... ] ]
)
TARGET_LAG = { '<num> { seconds | minutes | hours | days }' | DOWNSTREAM }
WAREHOUSE = <warehouse_name>
[ REFRESH_MODE = FULL | INCREMENTAL | AUTO ]
[ IMMUTABLE WHERE ( <expr> ) ]
[ CLUSTER BY ( <expr> [ , <expr> , ... ] ) ]
[ DATA_RETENTION_TIME_IN_DAYS = <integer> ]
[ MAX_DATA_EXTENSION_TIME_IN_DAYS = <integer> ]
[ COMMENT = '<string_literal>' ]
Creates a dynamic table if it doesn’t exist, or alters it according to the dynamic table definition. The CREATE OR ALTER DYNAMIC TABLE syntax follows the rules of a CREATE DYNAMIC TABLE statement and has the same limitations as an ALTER DYNAMIC TABLE statement.
For more information, see CREATE OR ALTER <对象>.
Changes to the following dynamic table properties and parameters preserve data:
TARGET_LAG
WAREHOUSE
CLUSTER BY
DATA_RETENTION_TIME_IN_DAYS
MAX_DATA_EXTENSION_TIME_IN_DAYS
COMMENT
IMMUTABLE WHERE
When specified, only the mutable region is reinitialized and data in the immutable region is preserved. For more information, see 使用不可变性约束创建动态表.
Changes to the following dynamic table properties and parameters trigger a reinitialization:
REFRESH_MODE
Changes to the query or column list:
Dropping existing columns is supported.
Adding new columns is supported, but they can only be added at the end of existing columns.
Dropping columns that are used in an IMMUTABLE WHERE predicate or as clustering keys isn't supported.
For more information, see CREATE OR ALTER TABLE 使用说明.
CREATE DYNAMIC TABLE FROM SNAPSHOT SET¶
CREATE DYNAMIC TABLE <name> FROM SNAPSHOT SET <snapshot_set> IDENTIFIER '<snapshot_id>'
The FROM SNAPSHOT SET clause restores a dynamic table from a backup. You don't specify other table properties because they're all the same as in the backed-up table.
This form doesn't have a CREATE OR REPLACE clause. You typically either restore the dynamic table under a new name and recover any data or other objects from this new table, or rename the original table and then restore the table under the original name.
备注
The snapshot set is associated with the internal table ID of the original table. Any more snapshots you add to the snapshot set use the original table, even if you changed its name. If you want to make backups of the newly restored table, create a new snapshot set for it.
When you restore a dynamic table from a snapshot, Snowflake automatically initializes the new table during its first refresh.
For more information about snapshots, see Snapshots for backups and immutable storage.
snapshot_set
Specifies the name of a snapshot set created for a specific dynamic table. You can use the SHOW SNAPSHOT SETS command to locate the right snapshot set.
snapshot_id
Specifies the identifier of a specific snapshot within that snapshot set. You can use the SHOW SNAPSHOTS IN SNAPSHOT SET command to locate the right identifier within the snapshot set, based on the creation date and time for the snapshot.
CREATE DYNAMIC TABLE ...CLONE¶
创建具有相同的列定义的新动态表,并包含源动态表中的全部现有数据,而不会实际复制数据。克隆的动态表继承源的计划状态。
您还可以克隆过去特定时刻存在的动态表。有关更多信息,请参阅 克隆注意事项。
CREATE [ OR REPLACE ] [ TRANSIENT ] DYNAMIC TABLE <name>
CLONE <source_dynamic_table>
[ { AT | BEFORE } ( { TIMESTAMP => <timestamp> | OFFSET => <time_difference> | STATEMENT => <id> } ) ]
[
COPY GRANTS
TARGET_LAG = { '<num> { seconds | minutes | hours | days }' | DOWNSTREAM }
WAREHOUSE = <warehouse_name>
]
如果源动态表具有群集密钥,则克隆的动态表具有群集密钥。默认情况下,即使来源表的自动聚类未暂停,新表的自动聚类也会暂停。
有关克隆的更多详细信息,请参阅 CREATE <object> ... CLONE。
CREATE DYNAMIC ICEBERG TABLE¶
创建一个新的动态 Apache Iceberg™ 表。有关 Iceberg 表的信息,请参阅 Apache Iceberg™ 表 和:doc:/sql-reference/sql/create-iceberg-table-snowflake
。
CREATE [ OR REPLACE ] DYNAMIC ICEBERG TABLE <name> (
-- Column definition
<col_name> <col_type>
[ [ WITH ] MASKING POLICY <policy_name> [ USING ( <col_name> , <cond_col1> , ... ) ] ]
[ [ WITH ] TAG ( <tag_name> = '<tag_value>' [ , <tag_name> = '<tag_value>' , ... ] ) ]
[ COMMENT '<string_literal>' ]
-- Additional column definitions
[ , <col_name> <col_type> [ ... ] ]
)
TARGET_LAG = { '<num> { seconds | minutes | hours | days }' | DOWNSTREAM }
WAREHOUSE = <warehouse_name>
[ EXTERNAL_VOLUME = '<external_volume_name>' ]
[ CATALOG = 'SNOWFLAKE' ]
[ BASE_LOCATION = '<optional_directory_for_table_files>' ]
[ REFRESH_MODE = { AUTO | FULL | INCREMENTAL } ]
[ INITIALIZE = { ON_CREATE | ON_SCHEDULE } ]
[ CLUSTER BY ( <expr> [ , <expr> , ... ] ) ]
[ DATA_RETENTION_TIME_IN_DAYS = <integer> ]
[ MAX_DATA_EXTENSION_TIME_IN_DAYS = <integer> ]
[ COMMENT = '<string_literal>' ]
[ COPY GRANTS ]
[ [ WITH ] ROW ACCESS POLICY <policy_name> ON ( <col_name> [ , <col_name> ... ] ) ]
[ [ WITH ] TAG ( <tag_name> = '<tag_value>' [ , <tag_name> = '<tag_value>' , ... ] ) ]
[ REQUIRE USER ]
AS <query>
有关使用和限制的详细信息,请参阅 创建动态 Apache Iceberg™ 表。
必填参数¶
name
指定动态表的标识符(即名称);对于在其中创建动态表的架构必须唯一。
此外,标识符必须以字母字符开头,且不能包含空格或特殊字符,除非整个标识符字符串放在双引号内(例如,
"My object"
)。放在双引号内的标识符也区分大小写。有关更多详细信息,请参阅 标识符要求。
TARGET_LAG = { num { seconds | minutes | hours | days } | DOWNSTREAM }
为动态表指定滞后时间:
'num seconds | minutes | hours | days'
指定动态表内容应滞后于源表更新的最长时间。
例如:
如果动态表中数据的滞后时间不超过 5 分钟,请指定
5 minutes
。如果动态表中数据的滞后时间不超过 5 小时,请指定
5 hours
。
必须至少为 60 秒。如果该动态表依赖于另一个动态表,则最小目标滞后必须大于或等于所依赖动态表的目标滞后。
DOWNSTREAM
指定动态表只有在依赖于它的动态表被刷新时才被刷新。
WAREHOUSE = warehouse_name
指定提供计算资源以刷新动态表的仓库的名称。
您必须使用对此仓库具有 USAGE 权限的角色,才能创建动态表。有关限制和更多信息,请参阅 创建动态表的权限。
AS query
指定动态表应包含其结果的查询。
可选参数¶
TRANSIENT
指定表为瞬态。
与永久动态表一样,瞬态 动态表在显式删除之前一直存在,任何具有相应权限的用户都可以使用。瞬态动态表不会将数据保留在故障安全存储中,这有助于降低存储成本,特别是对于频繁刷新的表。由于耐用性降低,瞬态动态表最适合存储瞬态数据,即不需要永久表所提供的相同级别的数据保护和恢复的数据。
默认:无值。如果动态表未声明为
TRANSIENT
,则该表为永久表。
REFRESH_MODE = { AUTO | FULL | INCREMENTAL }
为动态表指定 刷新模式。
创建动态表后无法更改此属性。要修改属性,请使用 CREATE OR REPLACE DYNAMIC TABLE 命令重新创建动态表。
AUTO
当刷新模式为
AUTO
时,系统默认尝试应用增量刷新。但是,当增量刷新不受支持或预计无法良好运行时,动态表会自动选择完全刷新。有关更多信息,请参阅 动态表刷新模式 和 选择动态表刷新模式的最佳实践。要确定适合您用例的最佳模式,请尝试不同的刷新模式和自动建议。要在不同的 Snowflake 版本中保持一致的行为,可在所有动态表上显式设置刷新模式。
要验证动态表的刷新模式,请参阅 查看动态表刷新模式。
FULL
强制动态表完全刷新,即使动态表可以增量刷新。
INCREMENTAL
强制动态表的增量刷新。如果作为动态表基础的查询无法执行增量刷新,则动态表创建失败并显示错误消息。
默认:
AUTO
INITIALIZE
指定动态表的 初始刷新 的行为。创建动态表后无法更改此属性。要修改属性,请使用 CREATE OR REPLACE DYNAMIC TABLE 命令替换动态表。
ON_CREATE
创建时同步刷新动态表。如果此刷新失败,则动态表创建失败并显示错误消息。
ON_SCHEDULE
在下次计划刷新时刷新动态表。
刷新计划过程运行时将填充动态表。创建动态表时不填充数据。如果您尝试使用
SELECT * FROM DYNAMIC TABLE
查询表,可能会看到以下错误,因为第一次计划刷新尚未发生。Dynamic Table is not initialized. Please run a manual refresh or wait for a scheduled refresh before querying.
默认:
ON_CREATE
COMMENT 'string_literal'
指定列的注释。
(请注意,可以在列级别或表级别指定注释。相应的语法略有不同。)
MASKING POLICY = policy_name
指定要在列上设置的 掩码策略。
PROJECTION POLICY policy_name
指定要在列上设置的 投影策略。
column_list
如果您希望在动态表中更改列的名称或向列添加注释,请包含一个列列表,该列表指定列名和(如果需要)关于列的注释。无需指定列的数据类型。
如果动态表中的任何列都基于表达式(例如,不仅是简单的列名),则必须为动态表中的每个列提供列名。例如,在以下情况下,列名是必需的:
CREATE DYNAMIC TABLE my_dynamic_table (pre_tax_profit, taxes, after_tax_profit) TARGET_LAG = '20 minutes' WAREHOUSE = mywh AS SELECT revenue - cost, (revenue - cost) * tax_rate, (revenue - cost) * (1.0 - tax_rate) FROM staging_table;
您可以为每列指定可选注释。例如:
CREATE DYNAMIC TABLE my_dynamic_table (pre_tax_profit COMMENT 'revenue minus cost', taxes COMMENT 'assumes taxes are a fixed percentage of profit', after_tax_profit) TARGET_LAG = '20 minutes' WAREHOUSE = mywh AS SELECT revenue - cost, (revenue - cost) * tax_rate, (revenue - cost) * (1.0 - tax_rate) FROM staging_table;
WITH CONTACT ( purpose = contact [ , purpose = contact ...] )
将新对象与一个或多个 联系人 关联。
CLUSTER BY ( expr [ , expr , ... ] )
将动态表中的一个或多个列或列表达式指定为群集密钥。在为动态表指定群集密钥之前,应当对微分区有所了解。有关更多信息,请参阅 了解 Snowflake 表结构。
将群集密钥与动态表一起使用时请注意以下几点:
列定义是必需的,必须在语句中明确指定。
默认情况下,即使来源表的自动聚类已暂停,新动态表的自动聚类也不会暂停。
群集密钥 并非 旨在或建议用于所有表;它们通常有利于非常大(例如多 TB)的表。
指定 CLUSTER BY 不会在创建时对数据进行聚类;相反,CLUSTER BY 依赖于自动聚类来随着时间的推移重聚类数据。
有关更多信息,请参阅 群集密钥和聚类表。
默认:无值(未为表定义群集密钥)
DATA_RETENTION_TIME_IN_DAYS = integer
指定动态表的保留期,以便可以对动态表中的历史数据执行 Time Travel 操作(SELECT、CLONE)。Time Travel 对动态表的行为方式与对传统表的行为方式相同。有关更多信息,请参阅 了解和使用 Time Travel。
有关此对象级参数的详细说明以及有关对象参数的详细信息,请参阅 参数。
值:
Standard Edition:
0
或1
Enterprise Edition:
0
至90
用于永久表0
或1
用于临时表和瞬态表
默认:
Standard Edition:
1
Enterprise Edition(或更高版本):
1
(除非在架构、数据库或账户级别指定了不同的默认值)
备注
0
值实际上会为表禁用 Time Travel。MAX_DATA_EXTENSION_TIME_IN_DAYS = integer
一个对象参数,用于设置 Snowflake 可延长数据保留期的最长天数,以防止动态表上的数据流过时。
有关此参数的详细说明,请参阅 MAX_DATA_EXTENSION_TIME_IN_DAYS。
COMMENT = 'string_literal'
指定动态表的注释。
(请注意,可以在列级别或表级别指定注释。相应的语法略有不同。)
默认:无值。
COPY GRANTS
指定在使用以下 CREATE DYNAMIC TABLE 变体创建新动态表时保留原始表的访问权限:
CREATE OR REPLACE DYNAMIC TABLE
CREATE OR REPLACE DYNAMIC ICEBERG TABLE
CREATE OR REPLACE DYNAMIC TABLE ... CLONE
此参数将 除 OWNERSHIP 之外的所有权限从现有动态表复制到新动态表。新动态表 不会 继承为架构中的对象类型定义的任何未来授权。默认情况下,执行 CREATE DYNAMIC TABLE 语句的角色拥有新动态表。
如果该参数未包含在 CREATE DYNAMIC TABLE 语句中,则新表 不会 继承在原始动态表上授予的任何显式访问权限,但会继承为架构中的对象类型定义的任何未来授权。
如果语句要替换同名的现有表,则从要替换的表中复制授权。如果没有该名称的现有表,则复制授权。
例如,以下语句创建了一个克隆自
dt0
的动态表dt1
,并从dt0
复制了所有授权。首次运行命令时,dt1
会复制dt0
中的所有授权。如果您再次运行相同的命令,dt1
将复制dt1``(而不是 ``dt0
)中的所有授权。CREATE OR REPLACE DYNAMIC TABLE dt1 CLONE dt0 COPY GRANTS;
请注意以下事项:
借助 数据共享:
如果现有动态表已共享到另一个账户,则替换动态表也会共享。
如果现有动态表已作为数据使用者与您的账户共享,并且进一步授予了对账户中其他角色的访问权限(在父数据库上使用
GRANT IMPORTED PRIVILEGES
),则还会授予对替换动态表的访问权限。
替换动态表的 SHOW GRANTS 输出会将复制权限的获得者列为执行 CREATE TABLE 语句的角色,并附带执行语句时的当前时间戳。
替换动态表的 SHOW GRANTS 输出会将复制权限的获得者列为执行 CREATE TABLE 语句的角色,并附带执行语句时的当前时间戳。
复制授权的操作在 CREATE DYNAMIC TABLE 命令中会以原子方式发生(即在同一事务中)。
重要
COPY GRANTS 参数可以放在 CREATE [ OR REPLACE ] DYNAMIC TABLE 命令中的任何地方,查询定义之后除外。
例如,以下动态表将无法创建:
CREATE OR REPLACE DYNAMIC TABLE my_dynamic_table TARGET_LAG = DOWNSTREAM WAREHOUSE = mywh AS SELECT * FROM staging_table COPY GRANTS;
ROW ACCESS POLICY policy_name ON ( col_name [ , col_name ... ] )
指定要在动态表上设置的 行访问策略。
TAG ( tag_name = 'tag_value' [ , tag_name = 'tag_value' , ... ] )
指定 标签 名称和标签字符串值。
标签值始终为字符串,标签值的最大字符数为 256。
有关在语句中指定标签的信息,请参阅 对象的标签配额。
AGGREGATION POLICY policy_name [ ENTITY KEY ( col_name [ , col_name ... ] ) ]
指定要在动态表上设置的 聚合策略。您可以对表应用一个或多个聚合策略。
使用可选的 ENTITY KEY 参数来定义动态表中哪些列可以唯一地标识实体。有关更多信息,请参阅 通过聚合策略实施实体级隐私。可以为聚合策略指定一个或多个实体键。
REQUIRE USER
指定后,除非指定用户,否则动态表无法运行。除非使用指定的 COPY SESSION 参数将用户设置为手动刷新,否则动态表无法刷新。
如果启用此选项,则必须使用
INITIALIZE
的 ON_SCHEDULE 参数创建动态表。IMMUTABLE WHERE
指定一个条件来定义动态表的不可变部分。有关更多信息,请参阅 使用不可变性约束创建动态表。
BACKFILL FROM <name>
指定要从中回填数据的表。
只能回填由 IMMUTABLE WHERE 不可变性约束 定义的数据,因为即使回填数据与上游来源不同,也必须保持不变。
有关更多信息,请参阅 使用回填创建动态表。
访问控制要求¶
权限 |
对象 |
备注 |
---|---|---|
CREATE DYNAMIC TABLE |
计划在其中创建动态表的架构。 |
|
SELECT |
计划查询新动态表的表、视图和动态表。 |
|
USAGE |
计划用于刷新表的仓库。 |
The USAGE privilege on the parent database and schema are required to perform operations on any object in a schema. Note that a role granted any privilege on a schema allows that role to resolve the schema. For example, a role granted CREATE privilege on a schema can create objects on that schema without also having USAGE granted on that schema.
有关创建具有指定权限集的自定义角色的说明,请参阅 创建自定义角色。
使用说明¶
执行 CREATE DYNAMIC TABLE 命令时,当前正在使用的角色将成为动态表的所有者。此角色用于在后台执行动态表的刷新。
创建动态表后不能对架构进行变更。
动态表会随着基础数据库对象的变更而更新。必须对动态表使用的所有基础对象启用变更跟踪。请参阅 启用更改跟踪。
如果要替换现有的动态表并需要查看其当前定义,请调用 GET_DDL 函数。
在动态表的定义中使用 ORDER BY 可能会产生按意外顺序排序的结果。您可以在查询动态表时使用 ORDER BY,以确保所选行按特定顺序返回。
Snowflake 不支持使用 ORDER BY 创建从动态表中选择的视图。
动态表中现在不支持某些表达式、子句和函数。有关完整列表,请参阅 动态表限制。
客户应确保在使用 Snowflake 服务时,不会将个人数据(用户对象除外)、敏感数据、出口管制数据或其他受监管数据作为元数据输入。有关更多信息,请参阅 Snowflake 中的元数据字段。
The OR REPLACE and IF NOT EXISTS clauses are mutually exclusive. They can't both be used in the same statement.
CREATE OR REPLACE <object> 语句是原子的。也就是说,当对象被替换时,旧对象将被删除,新对象将在单个事务中创建。
CREATE OR ALTER DYNAMIC TABLE usage notes¶
All limitations of the ALTER DYNAMIC TABLE command apply.
Limitations¶
The following actions aren't supported:
Swapping dynamic tables by using the SWAP WITH parameter.
Renaming a dynamic table by using the RENAME TO parameter.
Creating a clone of a dynamic table by using the CLONE parameter.
Suspending or resuming by using the SUSPEND and RESUME parameters.
Converting a TRANSIENT dynamic table into a non-TRANSIENT dynamic table, or vice versa.
Adding or changing tags and policies. Any existing tags and policies are preserved, and other statements might still add or remove tags and policies.
Creating or altering dynamic Apache Iceberg™ tables.
Time Travel clone for times that are before the latest definition or refresh mode change.
Additionally, modifying the values for the REFRESH_MODE and INITIALIZE properties after
the dynamic table has been created isn't supported. You can switch between the AUTO
refresh mode and the specific INCREMENTAL
and FULL
refresh modes, but doing so
doesn't change the actual physical refresh mode of the dynamic table.
例如:
If you create a dynamic table with
AUTO
refresh mode, the system immediately assigns a concrete mode (INCREMENTAL
orFULL
). When you run a subsequent CREATE OR ALTER DYNAMIC TABLE statement, you can specifyAUTO
or the concrete refresh mode that is chosen by the engine at creation. However, this doesn't alter the assigned refresh mode; it remains the same.If you create a dynamic table with a specific refresh mode (
INCREMENTAL
orFULL
), you can later specifyAUTO
in a CREATE OR ALTER DYNAMIC TABLE statement to enable forward compatibility. For example, if your dynamic table was created withFULL
mode and is version-controlled, specifyingAUTO
in a CREATE OR ALTER DYNAMIC TABLE statement enables new tables to useAUTO
, while existing tables remain inFULL
mode without breaking compatibility.
No implicit refreshes¶
If you change an existing dynamic table by using the CREATE OR ALTER DYNAMIC TABLE command, the command doesn't trigger a refresh of the dynamic table. The dynamic table is refreshes according to its normal schedule.
However, if you create a new dynamic table by using the CREATE OR ALTER DYNAMIC TABLE
command and you specify INITIALIZE = ON_CREATE
, the command triggers a refresh of the
dynamic table.
Atomicity¶
The CREATE OR ALTER DYNAMIC TABLE command doesn't guarantee atomicity. This means that if a CREATE OR ALTER DYNAMIC TABLE statement fails during execution, it's possible that a subset of changes might have been applied to the table. If there's a possibility of partial changes, in most cases, the error message includes the following text:
CREATE OR ALTER execution failed. Partial updates may have been applied.
For example, suppose that you wanted to change the TARGET_LAG
property and add a
clustering key for a dynamic table, but you change your mind and terminate the statement. In
this case, the TARGET_LAG
property might still change while the clustering key isn't
applied.
When changes are partially applied, the resulting table is in a valid state. In the previous example, you can use additional ALTER DYNAMIC TABLE statements to complete the original set of changes.
To recover from partial updates, try the following recovery methods:
Fix forward: Re-execute the CREATE OR ALTER DYNAMIC TABLE statement. If the statement succeeds on the second attempt, the target state is achieved.
If the statement doesn't succeed, investigate the error message. If possible, fix the error and re-execute the CREATE OR ALTER DYNAMIC TABLE statement.
Roll back: If it isn't possible to fix forward, manually roll back the partial changes:
Investigate the state of the table by using the DESCRIBE DYNAMIC TABLE and SHOW DYNAMIC TABLES commands. Determine which partial changes were applied, if any.
If partial changes were applied, execute the appropriate ALTER DYNAMIC TABLE statements to transform the dynamic table back to its original statement.
For additional help, contact Snowflake Support.
示例¶
创建名为 my_dynamic_table
的动态表:
CREATE OR REPLACE DYNAMIC TABLE my_dynamic_table
TARGET_LAG = '20 minutes'
WAREHOUSE = mywh
AS
SELECT product_id, product_name FROM staging_table;
在上面的例子中:
动态表可具体化对
staging_table
表的product_id
列和product_name
列的查询结果。目标滞后时间为 20 分钟,这意味着理想情况下,动态表中的数据不应比
staging_table
中的数据早 20 分钟。自动刷新过程使用仓库
mywh
中的计算资源来刷新动态表中的数据。
创建一个名为 my_dynamic_table
并从 my_iceberg_table
中读取数据的动态 Iceberg 表:
CREATE DYNAMIC ICEBERG TABLE my_dynamic_table (date TIMESTAMP_NTZ, id NUMBER, content STRING)
TARGET_LAG = '20 minutes'
WAREHOUSE = mywh
EXTERNAL_VOLUME = 'my_external_volume'
CATALOG = 'SNOWFLAKE'
BASE_LOCATION = 'my_iceberg_table'
AS
SELECT product_id, product_name FROM staging_table;
使用多列群集密钥来创建动态表:
CREATE DYNAMIC TABLE my_dynamic_table (date TIMESTAMP_NTZ, id NUMBER, content VARIANT)
TARGET_LAG = '20 minutes'
WAREHOUSE = mywh
CLUSTER BY (date, id)
AS
SELECT product_id, product_name FROM staging_table;
克隆在指定时间戳的日期和时间存在的动态表:
CREATE DYNAMIC TABLE my_cloned_dynamic_table CLONE my_dynamic_table AT (TIMESTAMP => TO_TIMESTAMP_TZ('04/05/2013 01:02:03', 'mm/dd/yyyy hh24:mi:ss'));
将动态表配置为要求用户刷新,然后刷新动态表:
CREATE DYNAMIC TABLE my_dynamic_table
TARGET_LAG = 'DOWNSTREAM'
WAREHOUSE = mywh
INITIALIZE = on_schedule
REQUIRE USER
AS
SELECT product_id, product_name FROM staging_table;
ALTER DYNAMIC TABLE my_dynamic_table REFRESH COPY SESSION;
Create a dynamic table by using the CREATE OR ALTER DYNAMIC TABLE command:
CREATE OR ALTER DYNAMIC TABLE my_dynamic_table
TARGET_LAG = DOWNSTREAM
WAREHOUSE = mywh
AS
SELECT a, b FROM t;
备注
CREATE OR ALTER TABLE statements for existing tables can only be executed by a role with the OWNERSHIP privilege on my_dynamic_table
.
Alter a dynamic table to set the DATA_RETENTION_TIME_IN_DAYS parameter and add a clustering key:
CREATE OR ALTER DYNAMIC TABLE my_dynamic_table
TARGET_LAG = DOWNSTREAM
WAREHOUSE = mywh
DATA_RETENTION_TIME_IN_DAYS = 2
CLUSTER BY (a)
AS
SELECT a, b FROM t;
Modify the target lag and change the warehouse:
CREATE OR ALTER DYNAMIC TABLE my_dynamic_table
TARGET_LAG = '5 minutes'
WAREHOUSE = my_other_wh
DATA_RETENTION_TIME_IN_DAYS = 2
CLUSTER BY (a)
AS
SELECT a, b FROM t;
Unset the DATA_RETENTION_TIME_IN_DAYS parameter. The absence of a parameter in the modified CREATE OR ALTER DYNAMIC TABLE statement results in unsetting it. In this case, unsetting the DATA_RETENTION_TIME_IN_DAYS parameter for the dynamic table resets it to the default value of 1:
CREATE OR ALTER DYNAMIC TABLE my_dynamic_table
TARGET_LAG = '5 minutes'
WAREHOUSE = my_other_wh
CLUSTER BY (a)
AS
SELECT a, b FROM t;