共用方式為


ALTER VIEW

適用於:核取記號為「是」Databricks SQL 核取記號為「是」Databricks Runtime

改變與檢視相關聯的元數據。 它可以變更檢視的定義、將檢視的名稱變更為不同的名稱、設定或移除TBLPROPERTIES之檢視的元數據。

若要在視圖上新增或變更註解,請使用 COMMENT ON

如果快取檢視,命令會清除檢視的快取數據,以及參考該檢視的所有相依專案。 下次存取檢視時,檢視的快取將會延遲填滿。 命令會將檢視的依賴項保留為未緩存。

語法

ALTER VIEW view_name
  { rename |
    SET TBLPROPERTIES clause |
    UNSET TBLPROPERTIES clause |
    alter_body |
    schema_binding |
    owner_to |
    SET TAGS clause |
    UNSET TAGS clause }

rename
  RENAME TO to_view_name

alter_body
AS { query | yaml_definition }

yaml_definition
  $$
    yaml_string
  $$

schema_binding
  WITH SCHEMA { BINDING | [ TYPE ] EVOLUTION | COMPENSATION }

property_key
  { idenitifier [. ...] | string_literal }

owner_to
  [ SET ] OWNER TO principal

參數

  • view_name

    識別要改變的檢視。 如果找不到檢視,Azure Databricks 就會 引發TABLE_OR_VIEW_NOT_FOUND 錯誤。

  • 重新命名為 to_view_name

    將現有的檢視重新命名為 to_view_name

    針對 Unity 目錄檢視, to_view_name 必須在與 view_name相同的目錄中。 對於其他檢視, to_view_name 必須在與 view_name相同的架構內。

    如果 to_view_name 未限定,則會隱含以當前結構限定。

    具體化檢視無法重新命名。

    設定或重設一個或多個使用者定義的屬性。

  • UNSET TBL屬性

    移除一個或多個使用者定義的屬性。

  • AS 查詢

    從基表或其他視圖建構檢視的查詢。

    AS query 不支援計量檢視功能。

    這個子句相當於 現有檢視上的 CREATE OR REPLACE VIEW 語句,但檢視上授與的許可權會保留。

  • 作為yaml_definition

    適用於:標記為 是 Databricks SQL 標記為 是 Databricks Runtime 16.4 和更高版本 標記為 是 Unity Catalog 仅适用

    計量檢視的yaml_definition。

    這個子句相當於 現有檢視上的 CREATE OR REPLACE VIEW 語句,但檢視上授與的許可權會保留。

  • schema_binding

    適用於:勾選為是 Databricks SQL 勾選為是 Databricks Runtime 15.3 和更新版本

    指定後續查詢如何適應由於基礎物件定義變更而對檢視架構所做的更改。 請參閱 CREATE VIEW...WITH SCHEMA 如需架構系結模式的詳細數據。

    此子句不適用於計量檢視。

  • [ SET ] 擁有者 至 主要

    將檢視的擁有權轉移至 principal。 除非檢視是在 中 hive_metastore 定義,否則您只能將擁有權轉移至您所屬的群組。

    適用於:核取記號為「是」 Databricks SQL 核取記號為「是」 Databricks Runtime 11.3 LTS 和更新版本

    允許 SET 作為可選關鍵字。

  • SET TAGS ( { tag_name = tag_value } [, ...] )

    將標籤套用至檢視。 您必須具有 APPLY TAG 將標籤新增至檢視的許可權。

    適用於:核取記號為「是」 Databricks SQL 核取記號為「是」 Databricks Runtime 13.3 LTS 和更新版本

  • UNSET TAGS (tag_name [, ...] )

    從資料表中移除標籤。 您需要有 APPLY TAG 許可權才能從檢視中移除標籤。

    適用於:核取記號為「是」 Databricks SQL 核取記號為「是」 Databricks Runtime 13.3 LTS 和更新版本

  • tag_name

    常值 STRING。 在 tag_name 檢視中必須是唯一的。

  • tag_value

    常值 STRING

範例

-- Rename only changes the view name.
-- The source and target schemas of the view have to be the same.
-- Use qualified or unqualified name for the source and target view.
> ALTER VIEW tempsc1.v1 RENAME TO tempsc1.v2;

-- Verify that the new view is created.
> DESCRIBE TABLE EXTENDED tempsc1.v2;
                            c1       int   NULL
                            c2    string   NULL

  # Detailed Table Information
                      Database   tempsc1
                         Table        v2

-- Before ALTER VIEW SET TBLPROPERTIES
> DESCRIBE TABLE EXTENDED tempsc1.v2;
                            c1       int   null
                            c2    string   null

  # Detailed Table Information
                      Database   tempsc1
                         Table        v2
              Table Properties    [....]

-- Set properties in TBLPROPERTIES
> ALTER VIEW tempsc1.v2 SET TBLPROPERTIES ('created.by.user' = "John", 'created.date' = '01-01-2001' );

-- Use `DESCRIBE TABLE EXTENDED tempsc1.v2` to verify
> DESCRIBE TABLE EXTENDED tempsc1.v2;
                            c1                                                   int   NULL
                            c2                                                string   NULL

  # Detailed Table Information
                      Database                                               tempsc1
                         Table                                                    v2
              Table Properties [created.by.user=John, created.date=01-01-2001, ....]

-- Remove the key created.by.user and created.date from `TBLPROPERTIES`
> ALTER VIEW tempsc1.v2 UNSET TBLPROPERTIES (`created`.`by`.`user`, created.date);

-- Use `DESCRIBE TABLE EXTENDED tempsc1.v2` to verify the changes
> DESCRIBE TABLE EXTENDED tempsc1.v2;
                            c1       int   NULL
                            c2    string   NULL

  # Detailed Table Information
                      Database   tempsc1
                         Table        v2
              Table Properties    [....]

-- Change the view definition
> ALTER VIEW tempsc1.v2 AS SELECT * FROM tempsc1.v1;

-- Use `DESCRIBE TABLE EXTENDED` to verify
> DESCRIBE TABLE EXTENDED tempsc1.v2;
                            c1                        int   NULL
                            c2                     string   NULL

  # Detailed Table Information
                      Database                    tempsc1
                         Table                         v2
                          Type                       VIEW
                     View Text   select * from tempsc1.v1
            View Original Text   select * from tempsc1.v1

-- Transfer ownership of a view to another user
> ALTER VIEW v1 OWNER TO `alf@melmak.et`

-- Change the view schema binding to adopt type evolution
> ALTER VIEW v1 WITH SCHEMA TYPE EVOLUTION;

-- Applies three tags to the view named `test`.
> ALTER VIEW test SET TAGS ('tag1' = 'val1', 'tag2' = 'val2', 'tag3' = 'val3');

-- Removes three tags from the view named `test`.
> ALTER VIEW test UNSET TAGS ('tag1', 'tag2', 'tag3');

-- Alter a the metric view `region_sales_metrics` defined in CREATE VIEW to drop the `total_revenue_for_open_orders` measure.
> ALTER VIEW region_sales_metrics
  AS $$
   version: 0.1
   source: samples.tpch.orders
   filter: o_orderdate > '1990-01-01'
   dimensions:
   - name: month
     expr: date_trunc('MONTH', o_orderdate)
   - name: status
     expr: case
       when o_orderstatus = 'O' then 'Open'
       when o_orderstatus = 'P' then 'Processing'
       when o_orderstatus = 'F' then 'Fulfilled'
       end
   - name: order_priority
     expr: split(o_orderpriority, '-')[1]
   measures:
   - name: count_orders
     expr: count(1)
   - name: total_revenue
     expr: SUM(o_totalprice)
   - name: total_revenue_per_customer
     expr: SUM(o_totalprice) / count(distinct o_custkey)
  $$;

> DESCRIBE EXTENDED region_sales_metrics;
 col_name                    data_type
 month	                     timestamp
 status	                     string
 prder_priority              string
 count_orders                bigint measure
 total_revenue               decimal(28,2) measure
 total_revenue_per_customer  decimal(38,12) measure

 # Detailed Table Information
 Catalog                     main
 Database                    default
 Table                       region_sales_metrics
 Owner                       alf@melmak.et
 Created Time                Sun May 18 23:45:25 UTC 2025
 Last Access                 UNKNOWN
 Created By                  Spark
 Type                        METRIC_VIEW
 Comment                     A metric view for regional sales metrics.
 View Text                   "
    version: 0.1
    source: samples.tpch.orders
    filter: o_orderdate > '1990-01-01'
    dimensions:
    - name: month
      expr: date_trunc('MONTH', o_orderdate)
    - name: status
      expr: case
        when o_orderstatus = 'O' then 'Open'
        when o_orderstatus = 'P' then 'Processing'
        when o_orderstatus = 'F' then 'Fulfilled'
        end
    - name: prder_priority
      expr: split(o_orderpriority, '-')[1]
    measures:
    - name: count_orders
      expr: count(1)
    - name: total_revenue
      expr: SUM(o_totalprice)
    - name: total_revenue_per_customer
      expr: SUM(o_totalprice) / count(distinct o_custkey)
   "
 Language                    YAML
 Table Properties            [metric_view.from.name=samples.tpch.orders, metric_view.from.type=ASSET, metric_view.where=o_orderdate > '1990-01-01']