Although renaming a column is extremely uncommon, SQL Server provided us with the ability to do so in the event that it was required. I came to the realization that I needed to rename a column while working on one of the blog pieces. I figured I’d offer this information so that newcomers can learn from it.
CREATE TABLE tbStudentDetails(
StudentID INT,
StudentName VARCHAR(30),
Ranking INT);
INSERT INTO tbStudentDetails VALUES
(1001,’Zafar Iqbal’,2),
(3,’Tahir Iqbal’,1),
(5,’Danial Hussain’,3)
— Before Column Rename
SELECT * FROM tbStudentDetails
— Renaming the existing column
EXEC sp_rename ‘tbStudentDetails.Ranking’, ‘GradeID’, ‘COLUMN’
— After Column Rename
SELECT * FROM tbStudentDetails
Hope you find this article helpful.
Please subscribe for more interesting updates.