<a id="flink-sql-alter-view"></a>

# ALTER VIEW Statement in Confluent Cloud for Apache Flink

Confluent Cloud for Apache Flink® enables modifying properties of an existing view.

## Syntax

```sql
ALTER VIEW [catalog_name.][db_name.]view_name RENAME TO new_view_name

ALTER VIEW [catalog_name.][db_name.]view_name AS new_statement_expression
```

## Description

ALTER VIEW enables you to change the name of a view or modify the statement
expression that defines the view.

The first syntax enables renaming a view within the same catalog and
database. The new view name must not already exist in the catalog and
database.

The second syntax enables changing the underlying statement that defines the
view. The new statement expression must be a valid SELECT statement supported
by Flink SQL. Flink doesn’t validate the new statement’s schema against the
schema of the existing view, so you can add, remove, rename, or change the
type of columns.

#### NOTE
Altering a view doesn’t affect Flink SQL statements that are already
running and reference the view. Like other catalog objects, a running
statement takes a snapshot of the view’s definition when the statement
starts, so it keeps running against that snapshot. Only statements that
you submit after the ALTER VIEW statement resolve against the updated
view. They fail if they reference a column that no longer exists or
whose type is no longer compatible with how the statement uses it. For
more information, see
[Statements take a snapshot of their dependencies](../../concepts/schema-statement-evolution.md#flink-sql-schema-and-statement-evolution-dependency-snapshot).

<a id="flink-sql-alter-view-examples"></a>

## Examples

The following examples show frequently encountered scenarios with ALTER VIEW.

### Rename a view

In the Confluent CLI or in a Cloud Console workspace, run the
following commands to rename a view.

1. Create a view.
   ```sql
   CREATE VIEW customer_orders AS
   SELECT customer_id, SUM(price) AS total_spent
   FROM `examples`.`marketplace`.`orders`
   GROUP BY customer_id;
   ```
2. Rename the view.
   ```sql
   ALTER VIEW customer_orders RENAME TO vip_customers;
   ```

   Your output should resemble:
   ```none
   Statement phase is COMPLETED.
   ```
3. Query the renamed view.
   ```sql
   SELECT * FROM vip_customers;
   ```

   The statement now references the view by its new name.

### Change the statement expression of a view

1. View the current definition of the view.
   ```sql
   SHOW CREATE VIEW vip_customers;
   ```

   Your output should resemble:
   ```none
   +------------------------------------------------------------------------------+
   |                              SHOW CREATE VIEW                                |
   +------------------------------------------------------------------------------+
   | CREATE VIEW vip_customers AS SELECT customer_id, SUM(price) AS total_spent   |
   | FROM orders                                                                  |
   | GROUP BY customer_id;                                                        |
   +------------------------------------------------------------------------------+
   ```
2. Change the statement expression of the view.
   ```sql
   ALTER VIEW vip_customers AS
   SELECT customer_id, SUM(price) AS total_spent, COUNT(*) AS order_count
   FROM `examples`.`marketplace`.`orders`
   GROUP BY customer_id
   HAVING SUM(price) > 1000;
   ```

   Your output should resemble:
   ```none
   Statement phase is COMPLETED.
   ```
3. View the updated definition of the view.
   ```sql
   SHOW CREATE VIEW vip_customers;
   ```

   Your output should resemble:
   ```none
   +-----------------------------------------------------------------------------------------------------+
   |                                         SHOW CREATE VIEW                                            |
   +-----------------------------------------------------------------------------------------------------+
   | CREATE VIEW vip_customers AS SELECT customer_id, SUM(price) AS total_spent, COUNT(*) AS order_count |
   | FROM orders                                                                                         |
   | GROUP BY customer_id                                                                                |
   | HAVING SUM(price) > 1000;                                                                           |
   +-----------------------------------------------------------------------------------------------------+
   ```

   The view now includes an additional `order_count` column representing
   the number of orders per customer, and filters for only those customers
   who have spent more than 1000.

## Related content

- [CREATE VIEW statement](create-view.md#flink-sql-create-view)
- [DROP VIEW statement](drop-view.md#flink-sql-drop-view)
- [SELECT statement](../queries/select.md#flink-sql-select)
- [Schema and Statement Evolution](../../concepts/schema-statement-evolution.md#flink-sql-schema-and-statement-evolution)

#### NOTE
This website includes content developed at the [Apache Software Foundation](https://www.apache.org/)
under the terms of the [Apache License v2](https://www.apache.org/licenses/LICENSE-2.0.html).
