<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Alter table to add/update multiple column comments in Get Started Discussions</title>
    <link>https://community.databricks.com/t5/get-started-discussions/alter-table-to-add-update-multiple-column-comments/m-p/96535#M8840</link>
    <description>&lt;P&gt;This approach would give me a syntax error. Essentially, error at the comma after each alter column.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;[PARSE_SYNTAX_ERROR] Syntax error at or near ','.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;ALTER TABLE hive_metastore.xxxx.yyyy.table ALTER COLUMN `col1` COMMENT 'Dhmsps4w3Lfgy emzbociW0szmpU3d', --------------------------------------------------------------^^^ ALTER COLUMN `col2` COMMENT '2z5lwE5MGWzAPgR46icOzHhFnBZHCh',&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 28 Oct 2024 15:53:00 GMT</pubDate>
    <dc:creator>bharathjs</dc:creator>
    <dc:date>2024-10-28T15:53:00Z</dc:date>
    <item>
      <title>Alter table to add/update multiple column comments</title>
      <link>https://community.databricks.com/t5/get-started-discussions/alter-table-to-add-update-multiple-column-comments/m-p/96026#M8836</link>
      <description>&lt;P&gt;I was wondering if there's a way to alter table and add/update comments for multiple columns at once using SQL or API calls. For instance -&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;ALTER&lt;/SPAN&gt; &lt;SPAN&gt;TABLE&lt;/SPAN&gt;&amp;nbsp;&amp;lt;table_name&amp;gt;&amp;nbsp;&lt;SPAN&gt;CHANGE&lt;/SPAN&gt; &lt;SPAN&gt;COLUMN&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;&amp;lt;col1&amp;gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;COMMENT&lt;/SPAN&gt; &lt;SPAN&gt;'&amp;lt;comment1&amp;gt;'&lt;/SPAN&gt;&lt;SPAN&gt;,&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;CHANGE&lt;/SPAN&gt; &lt;SPAN&gt;COLUMN&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;&amp;lt;col2&amp;gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;COMMENT&lt;/SPAN&gt; &lt;SPAN&gt;'comment2' ;&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;I would like to update column comments for multiple tables for over 200 fields and flexible using sql, pyspark, apis. Would be great if there is a way to achieve this. Thanks in advance...&amp;nbsp; &amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Thu, 24 Oct 2024 18:06:52 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/alter-table-to-add-update-multiple-column-comments/m-p/96026#M8836</guid>
      <dc:creator>bharathjs</dc:creator>
      <dc:date>2024-10-24T18:06:52Z</dc:date>
    </item>
    <item>
      <title>Re: Alter table to add/update multiple column comments</title>
      <link>https://community.databricks.com/t5/get-started-discussions/alter-table-to-add-update-multiple-column-comments/m-p/96037#M8837</link>
      <description>&lt;P&gt;Hi, assuming you have a dictionary with column name to comment mapping, you can do this using pyspark like this:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;columns_comments = {
    "col1": "comment1",
    "col2": "comment2",
    # Add all your columns and comments here
}

for col, comment in columns_comments.items():
    spark.sql(f"ALTER TABLE your_table_name CHANGE COLUMN {col} SET COMMENT '{comment}'")&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 24 Oct 2024 18:32:32 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/alter-table-to-add-update-multiple-column-comments/m-p/96037#M8837</guid>
      <dc:creator>filipniziol</dc:creator>
      <dc:date>2024-10-24T18:32:32Z</dc:date>
    </item>
    <item>
      <title>Re: Alter table to add/update multiple column comments</title>
      <link>https://community.databricks.com/t5/get-started-discussions/alter-table-to-add-update-multiple-column-comments/m-p/96044#M8838</link>
      <description>&lt;P&gt;Yeah I did consider this approach but one drawback I observed with this approach is it takes a while to updated comments for ~200 columns. I'm looking for more reliable solution to update column's comments concurrently.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Oct 2024 18:43:42 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/alter-table-to-add-update-multiple-column-comments/m-p/96044#M8838</guid>
      <dc:creator>bharathjs</dc:creator>
      <dc:date>2024-10-24T18:43:42Z</dc:date>
    </item>
    <item>
      <title>Re: Alter table to add/update multiple column comments</title>
      <link>https://community.databricks.com/t5/get-started-discussions/alter-table-to-add-update-multiple-column-comments/m-p/96094#M8839</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/129178"&gt;@bharathjs&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;If you want to run it faster then there is a better way.&amp;nbsp;Databricks supports the execution of multiple ALTER COLUMN operations within a single ALTER TABLE statement.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;ALTER TABLE your_table_name
  ALTER COLUMN col1 SET COMMENT 'comment1',
  ALTER COLUMN col2 SET COMMENT 'comment2',
  ALTER COLUMN col3 SET COMMENT 'comment3';&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can prepare your SQL programatically and then to run it. I have prepared a test table with 200 columns, random comments and added the comments in one shot - the execution took just 0.18 second.&lt;BR /&gt;&lt;BR /&gt;Here is the sample:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from pyspark.sql.types import StructType, StructField, IntegerType

# Define the number of columns
num_columns = 200

# Dynamically create a list of StructFields
fields = [StructField(f"col{i}", IntegerType(), True) for i in range(1, num_columns + 1)]

# Create the schema
schema = StructType(fields)

# Create an empty DataFrame with the schema
df = spark.createDataFrame([], schema)

# Define the database and table name
database_name = "default"  # You can change this as needed
table_name = "test_comments"

# Create the test table
df.write.format("delta").mode("overwrite").saveAsTable(f"{database_name}.{table_name}")

print(f"Test table '{database_name}.{table_name}' with {num_columns} columns created successfully.")
&lt;/LI-CODE&gt;&lt;LI-CODE lang="markup"&gt;import random
import string

# Function to generate a random comment
def generate_random_comment(length=30):
    return ''.join(random.choices(string.ascii_letters + string.digits + ' ', k=length))

# Generate comments for each column
columns_comments = {f"col{i}": generate_random_comment() for i in range(1, num_columns + 1)}

# Display a sample of the generated comments
for col in list(columns_comments.keys())[:5]:  # Show first 5 for brevity
    print(f"{col}: {columns_comments[col]}")&lt;/LI-CODE&gt;&lt;LI-CODE lang="python"&gt;import time
import logging

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Function to escape single quotes in comments
def escape_single_quotes(comment):
    return comment.replace("'", "\\'")

# Start the timer
start_time = time.time()

# Generate ALTER COLUMN statements for all columns
alter_statements = ",\n  ".join([
    f"ALTER COLUMN `{col}` COMMENT '{escape_single_quotes(comment)}'"
    for col, comment in columns_comments.items()
])

# Construct the full ALTER TABLE SQL command
alter_table_sql = f"""
ALTER TABLE hive_metastore.{database_name}.{table_name}
  {alter_statements};
"""

# Execute the ALTER TABLE statement
try:
    spark.sql(alter_table_sql)
    logger.info(f"Successfully updated comments for {len(columns_comments)} columns in '{database_name}.{table_name}'.")
except Exception as e:
    logger.error(f"Failed to update comments for table '{database_name}.{table_name}'. Error: {e}")

# End the timer
end_time = time.time()
batched_duration = end_time - start_time

print(f"Batched ALTER TABLE duration: {batched_duration:.2f} seconds.")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The output:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="filipniziol_0-1729840763995.png" style="width: 400px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/12283i2D69B1329C77E1D0/image-size/medium?v=v2&amp;amp;px=400" role="button" title="filipniziol_0-1729840763995.png" alt="filipniziol_0-1729840763995.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;The comments:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="filipniziol_0-1729840826365.png" style="width: 400px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/12285i3DD8E94587C4877A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="filipniziol_0-1729840826365.png" alt="filipniziol_0-1729840826365.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Oct 2024 07:20:35 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/alter-table-to-add-update-multiple-column-comments/m-p/96094#M8839</guid>
      <dc:creator>filipniziol</dc:creator>
      <dc:date>2024-10-25T07:20:35Z</dc:date>
    </item>
    <item>
      <title>Re: Alter table to add/update multiple column comments</title>
      <link>https://community.databricks.com/t5/get-started-discussions/alter-table-to-add-update-multiple-column-comments/m-p/96535#M8840</link>
      <description>&lt;P&gt;This approach would give me a syntax error. Essentially, error at the comma after each alter column.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;[PARSE_SYNTAX_ERROR] Syntax error at or near ','.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;ALTER TABLE hive_metastore.xxxx.yyyy.table ALTER COLUMN `col1` COMMENT 'Dhmsps4w3Lfgy emzbociW0szmpU3d', --------------------------------------------------------------^^^ ALTER COLUMN `col2` COMMENT '2z5lwE5MGWzAPgR46icOzHhFnBZHCh',&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Oct 2024 15:53:00 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/alter-table-to-add-update-multiple-column-comments/m-p/96535#M8840</guid>
      <dc:creator>bharathjs</dc:creator>
      <dc:date>2024-10-28T15:53:00Z</dc:date>
    </item>
    <item>
      <title>Re: Alter table to add/update multiple column comments</title>
      <link>https://community.databricks.com/t5/get-started-discussions/alter-table-to-add-update-multiple-column-comments/m-p/100829#M8841</link>
      <description>&lt;P&gt;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/117376"&gt;@filipniziol&lt;/a&gt;&amp;nbsp;Is batch update maybe a preview feature or something that was only added in a recent version of the databricks runtime? I'm seeing the same error as&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/129178"&gt;@bharathjs&lt;/a&gt;&amp;nbsp; in DBR 14.3, 16.0 ML and serverless V1&lt;/P&gt;</description>
      <pubDate>Tue, 03 Dec 2024 20:59:42 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/alter-table-to-add-update-multiple-column-comments/m-p/100829#M8841</guid>
      <dc:creator>lukew_usafacts</dc:creator>
      <dc:date>2024-12-03T20:59:42Z</dc:date>
    </item>
    <item>
      <title>Re: Alter table to add/update multiple column comments</title>
      <link>https://community.databricks.com/t5/get-started-discussions/alter-table-to-add-update-multiple-column-comments/m-p/100873#M8843</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/134842"&gt;@lukew_usafacts&lt;/a&gt;&amp;nbsp;,&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/129178"&gt;@bharathjs&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;My apologies, the below syntax does not work:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;ALTER TABLE your_table_name
  ALTER COLUMN col1 SET COMMENT 'comment1',
  ALTER COLUMN col2 SET COMMENT 'comment2',
  ALTER COLUMN col3 SET COMMENT 'comment3';&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I generated the code, checked the print statement and thought it worked, but in fact it failed..&amp;nbsp;&lt;BR /&gt;The generated comments were not updated and they were created in the previous run, when I was running the ALTER statements one by one.&lt;/P&gt;&lt;P&gt;Sorry for confusion&lt;/P&gt;</description>
      <pubDate>Wed, 04 Dec 2024 08:43:12 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/alter-table-to-add-update-multiple-column-comments/m-p/100873#M8843</guid>
      <dc:creator>filipniziol</dc:creator>
      <dc:date>2024-12-04T08:43:12Z</dc:date>
    </item>
    <item>
      <title>Re: Alter table to add/update multiple column comments</title>
      <link>https://community.databricks.com/t5/get-started-discussions/alter-table-to-add-update-multiple-column-comments/m-p/141557#M11166</link>
      <description>&lt;P&gt;The correct SQL syntax for this is:&lt;/P&gt;&lt;LI-CODE lang="php"&gt;ALTER TABLE your_table_name ALTER COLUMN
  col1 COMMENT 'comment1',
  col2 COMMENT 'comment2',
  col3 COMMENT 'comment3';&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Dec 2025 04:33:24 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/alter-table-to-add-update-multiple-column-comments/m-p/141557#M11166</guid>
      <dc:creator>dxwell</dc:creator>
      <dc:date>2025-12-10T04:33:24Z</dc:date>
    </item>
  </channel>
</rss>

