SQL Server – Rename column via T-SQL

Cheat Code
EXEC sp_rename ‘[dbo].[TABLE_NAME].[OLD_COLUMN_NAME]’,’NEW_COLUMN_NAME’, ‘COLUMN’;

I just received a set of tables in SQL Server which was loaded from CSV file without column header on 1st row. They provide separate EXCEL file which define the column name.
You can imagine that all fields are like “[Column01]”, “[Column02]”, etc.

Good luck to our Data Analyst writing
“SELECT [Column01], sum([Column10]) from [dbo].[csv01] group by [Column01]” 😈 😈 😈

Well… I’m a good guy and love to make people live easy, so I use the above cheat code by generating the script over EXCEL file, you now “Substitute” function, and once I have the scripts, copy-paste-F5.
Your’re welcome users!

Photo by Andri from Pexels

Published by Feivel

We love to travel!

Leave a comment

Design a site like this with WordPress.com
Get started