SQL Server snippets

How to rename a column of a table

Example 1

Let's assume we have a #Schools table and we want to RENAME the Name column to Sign column.

CREATE TABLE Schools(
[Name] varchar(250),
[Grade] varchar(150),
[City] varchar(150));
INSERT INTO
Schools([Name],[Grade],[City])
VALUES
('Astro School', 'Specialization school', 'Huston'),
('The white beach school', 'High School', 'Honolulu'),
('Queen High School', 'High School', 'London');
School table before renaming

 EXEC sp_RENAME 'Schools.Name' , 'Sign', 'COLUMN'
School table after renaming

You can find an interactive version of this example following this link .

Back to SQL Server cookbook page