Problem with Renaming a Column in SQL Server

/*Initial Table*/  
CREATE TABLE AllocationDetails
  (
     Conversion_Fee_Per_Share FLOAT
  )

/*Faulty Rename*/  
EXEC sp_rename
  'dbo.AllocationDetails.[Conversion_Fee_Per_Share]',
  '[Conversion_Fee]',
  'COLUMN'

/*Fixed Rename*/  
EXEC sp_rename
  'dbo.AllocationDetails.[[Conversion_Fee]]]',
  'Conversion_Fee',
  'COLUMN'

DROP TABLE AllocationDetails 

The column name to use in the second sp_rename call is that returned by SELECT QUOTENAME('[Conversion_Fee_Per_Share]').

Alternatively and more straight forwardly one can use

EXEC sp_rename
  'dbo.AllocationDetails."[Conversion_Fee]"',
  'Conversion_Fee',
  'COLUMN'

QUOTED_IDENTIFIER is always set to on for that stored procedure so this doesn’t rely on you having this on in your session settings.

Leave a Comment