Home >Database >Mysql Tutorial >How to Correctly Rename a Column in SQL Server 2008?

How to Correctly Rename a Column in SQL Server 2008?

DDD
DDDOriginal
2025-01-07 15:51:41864browse

How to Correctly Rename a Column in SQL Server 2008?

Column renaming in SQL Server 2008

Question:

In SQL Server 2008, I tried to rename a column in a table using the following SQL statement:

<code class="language-sql">ALTER TABLE table_name RENAME COLUMN old_name to new_name;</code>

However, this statement doesn't seem to work. What's the problem?

Answer:

The SQL statement you provided is not the correct syntax for renaming columns in SQL Server 2008. To rename columns in SQL Server 2008, you need to use the sp_rename stored procedure.

The syntax for

sp_rename is as follows:

<code class="language-sql">EXEC sp_RENAME 'TableName.OldColumnName', 'NewColumnName', 'COLUMN';</code>

The correct SQL statement for your case would be:

<code class="language-sql">EXEC sp_RENAME 'table_name.old_name', 'new_name', 'COLUMN';</code>

Remember to enclose your values ​​in single quotes.

More resources:

The above is the detailed content of How to Correctly Rename a Column in SQL Server 2008?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn