ALTER VIEW

說明

ALTER VIEW 陳述式可以變更與檢視相關的元資料。它可以變更檢視的定義、將檢視名稱變更為不同的名稱、透過設定 TBLPROPERTIES 來設定和取消設定檢視的元資料。

重新命名檢視

重新命名現有的檢視。如果新的檢視名稱已存在於來源資料庫中,則會擲回 TableAlreadyExistsException。此操作不支援跨資料庫移動檢視。

如果檢視已快取,則此指令會清除檢視的快取資料及其所有參考它的相依項。下次存取檢視時,檢視的快取會延遲填滿。此指令會讓檢視的相依項保持未快取狀態。

語法

ALTER VIEW view_identifier RENAME TO view_identifier

參數

設定檢視屬性

設定現有檢視的一個或多個屬性。這些屬性是關鍵值配對。如果屬性的金鑰存在,則其值會被新的值取代。如果屬性的金鑰不存在,則關鍵值配對會新增到屬性中。

語法

ALTER VIEW view_identifier SET TBLPROPERTIES ( property_key = property_val [ , ... ] )

參數

取消設定檢視屬性

刪除現有檢視的一個或多個屬性。如果指定的金鑰不存在,則會擲回例外。使用 IF EXISTS 來避免例外。

語法

ALTER VIEW view_identifier UNSET TBLPROPERTIES [ IF EXISTS ]  ( property_key [ , ... ] )

參數

ALTER View AS SELECT

ALTER VIEW view_identifier AS SELECT 陳述式會變更檢視的定義。SELECT 陳述式必須有效,而且 view_identifier 必須存在。

語法

ALTER VIEW view_identifier AS select_statement

請注意,ALTER VIEW 陳述式不支援 SET SERDESET SERDEPROPERTIES 屬性。

參數

範例

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

-- Verify that the new view is created.
DESCRIBE TABLE EXTENDED tempdb1.v2;
+----------------------------+----------+-------+
|                    col_name|data_type |comment|
+----------------------------+----------+-------+
|                          c1|       int|   null|
|                          c2|    string|   null|
|                            |          |       |
|# Detailed Table Information|          |       |
|                    Database|   tempdb1|       |
|                       Table|        v2|       |
+----------------------------+----------+-------+

-- Before ALTER VIEW SET TBLPROPERTIES
DESC TABLE EXTENDED tempdb1.v2;
+----------------------------+----------+-------+
|                    col_name| data_type|comment|
+----------------------------+----------+-------+
|                          c1|       int|   null|
|                          c2|    string|   null|
|                            |          |       |
|# Detailed Table Information|          |       |
|                    Database|   tempdb1|       |
|                       Table|        v2|       |
|            Table Properties|    [....]|       |
+----------------------------+----------+-------+

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

-- Use `DESCRIBE TABLE EXTENDED tempdb1.v2` to verify
DESC TABLE EXTENDED tempdb1.v2;
+----------------------------+-----------------------------------------------------+-------+
|                    col_name|                                            data_type|comment|
+----------------------------+-----------------------------------------------------+-------+
|                          c1|                                                  int|   null|
|                          c2|                                               string|   null|
|                            |                                                     |       |
|# Detailed Table Information|                                                     |       |
|                    Database|                                              tempdb1|       |
|                       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 tempdb1.v2 UNSET TBLPROPERTIES ('created.by.user', 'created.date');

--Use `DESC TABLE EXTENDED tempdb1.v2` to verify the changes
DESC TABLE EXTENDED tempdb1.v2;
+----------------------------+----------+-------+
|                    col_name| data_type|comment|
+----------------------------+----------+-------+
|                          c1|       int|   null|
|                          c2|    string|   null|
|                            |          |       |
|# Detailed Table Information|          |       |
|                    Database|   tempdb1|       |
|                       Table|        v2|       |
|            Table Properties|    [....]|       |
+----------------------------+----------+-------+

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

-- Use `DESC TABLE EXTENDED` to verify
DESC TABLE EXTENDED tempdb1.v2;
+----------------------------+---------------------------+-------+
|                    col_name|                  data_type|comment|
+----------------------------+---------------------------+-------+
|                          c1|                        int|   null|
|                          c2|                     string|   null|
|                            |                           |       |
|# Detailed Table Information|                           |       |
|                    Database|                    tempdb1|       |
|                       Table|                         v2|       |
|                        Type|                       VIEW|       |
|                   View Text|   select * from tempdb1.v1|       |
|          View Original Text|   select * from tempdb1.v1|       |
+----------------------------+---------------------------+-------+