|
| 1 | +--- |
| 2 | +title: How to RENAME a column in SQL Server |
| 3 | +--- |
| 4 | + |
| 5 | +_Official documentation: [Rename columns](https://learn.microsoft.com/en-us/sql/relational-databases/tables/rename-columns-database-engine)_ |
| 6 | + |
| 7 | +## Basic Syntax |
| 8 | + |
| 9 | +The `COLUMN` parameter is required to specify that you're renaming a column rather than another database object. |
| 10 | + |
| 11 | +```sql |
| 12 | +EXEC sp_rename 'Schema.TableName.OldColumnName', 'NewColumnName', 'COLUMN'; |
| 13 | +``` |
| 14 | + |
| 15 | +## Managing Dependencies |
| 16 | + |
| 17 | +When renaming columns, it's critical to identify and update all dependencies to prevent application failures. |
| 18 | + |
| 19 | +### Identifying Dependencies |
| 20 | + |
| 21 | +Use `sys.sql_expression_dependencies` to find objects that reference the column: |
| 22 | + |
| 23 | +```sql |
| 24 | +SELECT |
| 25 | + OBJECT_SCHEMA_NAME(sed.referencing_id) AS ReferencingSchema, |
| 26 | + OBJECT_NAME(sed.referencing_id) AS ReferencingObject, |
| 27 | + o.type_desc AS ObjectType |
| 28 | +FROM sys.sql_expression_dependencies sed |
| 29 | +JOIN sys.objects o ON sed.referencing_id = o.object_id |
| 30 | +WHERE |
| 31 | + sed.referenced_id = OBJECT_ID('dbo.Customers') |
| 32 | + AND sed.referenced_minor_id = ( |
| 33 | + SELECT column_id |
| 34 | + FROM sys.columns |
| 35 | + WHERE object_id = OBJECT_ID('dbo.Customers') |
| 36 | + AND name = 'CustomerPhone' |
| 37 | + ); |
| 38 | +``` |
| 39 | + |
| 40 | +### Updating Dependencies |
| 41 | + |
| 42 | +After renaming columns, dependent views and stored modules may still reference the metadata of the old column name. SQL Server provides system stored procedures `sp_refreshview` and `sp_refreshsqlmodule` to refresh these references: |
| 43 | + |
| 44 | +```sql |
| 45 | +-- Refresh a specific view after column renaming |
| 46 | +EXEC sp_refreshview 'dbo.CustomerContactList'; |
| 47 | + |
| 48 | +-- Refresh a stored procedure, function, trigger, or view |
| 49 | +EXEC sp_refreshsqlmodule 'dbo.GetCustomerDetails'; |
| 50 | +``` |
| 51 | + |
| 52 | +- `sp_refreshview`: Specifically for views that have become invalid after column renaming |
| 53 | +- `sp_refreshsqlmodule`: Works on multiple object types (stored procedures, functions, triggers, and views) |
| 54 | + |
| 55 | +Limitations: |
| 56 | + |
| 57 | +- They work only if the column name changes don't break the logic (same column order and data types) |
| 58 | +- Text-based dynamic SQL references in the object code still need manual updates |
| 59 | + |
| 60 | +## Schema Binding |
| 61 | + |
| 62 | +For schema-bound objects (views, functions), you must: |
| 63 | + |
| 64 | +1. Drop the schema binding first |
| 65 | +1. Rename the column |
| 66 | +1. Recreate the schema binding |
| 67 | + |
| 68 | +```sql |
| 69 | +-- 1. Drop the schema-bound view |
| 70 | +DROP VIEW dbo.CustomerDetailsView; |
| 71 | + |
| 72 | +-- 2. Rename the column |
| 73 | +EXEC sp_rename 'dbo.Customers.CustomerPhone', 'ContactNumber', 'COLUMN'; |
| 74 | + |
| 75 | +-- 3. Recreate the view with schema binding |
| 76 | +CREATE VIEW dbo.CustomerDetailsView |
| 77 | +WITH SCHEMABINDING |
| 78 | +AS |
| 79 | + SELECT CustomerID, CustomerName, ContactNumber |
| 80 | + FROM dbo.Customers; |
| 81 | +``` |
| 82 | + |
| 83 | +Note that `sp_refreshview` and `sp_refreshsqlmodule` cannot be used with schema-bound objects, as these objects require explicit recreation after column changes. |
0 commit comments