cancel
Showing results for 
Search instead for 
Did you mean: 
Get Started Discussions
Start your journey with Databricks by joining discussions on getting started guides, tutorials, and introductory topics. Connect with beginners and experts alike to kickstart your Databricks experience.
cancel
Showing results for 
Search instead for 
Did you mean: 

Add comment to multi columns with views

anhbn2707
New Contributor

With Table, I can add comment to multi columns by using: 

ALTER TABLE your_table_name ALTER COLUMN
  col1 COMMENT 'comment1',
  col2 COMMENT 'comment2',
  col3 COMMENT 'comment3';

How can I do with views?

2 REPLIES 2

-werners-
Esteemed Contributor III

COMMENT ON COLUMN my_view.column_name IS 'My comment';
Props to Genie Code 🙂 (I was convinced it was not possible but I was wrong apparently)
This is col by col however.
With CREATE OR REPLACE VIEW you can add all comments in one go though.
You also have to be view owner.

Lu_Wang_ENB_DBX
Databricks Employee
Databricks Employee

For views, there isn’t a single multi-column ALTER VIEW ... ALTER COLUMN col1 ..., col2 ... form. Update each column one at a time instead. Databricks supports column comments on views, and newer runtimes/SQL warehouses use COMMENT ON COLUMN; older fallback uses ALTER TABLE ... ALTER COLUMN ... COMMENT.

-- Recommended on SQL Warehouse / newer runtimes
COMMENT ON COLUMN catalog.schema.my_view.col1 IS 'comment1';
COMMENT ON COLUMN catalog.schema.my_view.col2 IS 'comment2';
COMMENT ON COLUMN catalog.schema.my_view.col3 IS 'comment3';

-- Older fallback syntax
ALTER TABLE catalog.schema.my_view ALTER COLUMN col1 COMMENT 'comment1';
ALTER TABLE catalog.schema.my_view ALTER COLUMN col2 COMMENT 'comment2';
ALTER TABLE catalog.schema.my_view ALTER COLUMN col3 COMMENT 'comment3';

If you’re creating/replacing the view, you can also define comments inline in the view column list.

CREATE OR REPLACE VIEW catalog.schema.my_view (
  col1 COMMENT 'comment1',
  col2 COMMENT 'comment2',
  col3 COMMENT 'comment3'
) AS
SELECT col1, col2, col3
FROM catalog.schema.source_table;