devxlogo

Changing the Column Name Through StoredProcedure Dynamically

Changing the Column Name Through StoredProcedure Dynamically

Here’s the syntax for the stored procedure:

CREATE PROCEDURE [dbo].[RenameColumn] @tblname as varchar(50), @oldcolnameas varchar(50), @newcolname as varchar(50)ASdeclare  @tblColname varchar(100)set @tblColname =  @tblname +'.' + @oldcolnameBEGIN TRANSACTIONSET QUOTED_IDENTIFIER ONSET TRANSACTION ISOLATION LEVEL SERIALIZABLESET ARITHABORT ONSET NUMERIC_ROUNDABORT OFFSET CONCAT_NULL_YIELDS_NULL ONSET ANSI_NULLS ONSET ANSI_PADDING ONSET ANSI_WARNINGS ONCOMMITBEGIN TRANSACTIONEXECUTE sp_rename   @tblColname   , @newcolname,'COLUMN'COMMITGO

Then, execute this:

exec RenameColumn 'SFDIDs','CompanyID','CID'Here SFDIDS is the table nameCompanyID is the Old column NameCID is the new column Name
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist