Problem with Renaming a Column in SQL Server

前端 未结 4 1847
我寻月下人不归
我寻月下人不归 2020-12-19 13:49

So I was trying to rename a column in my table from Conversion_Fee_PerShare to just Conversion Fee.

I looked up online and found the syntax

相关标签:
4条回答
  • 2020-12-19 14:25
    /*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.

    0 讨论(0)
  • 2020-12-19 14:38

    Never mind I found out:

    ALTER TABLE dbo.AllocationDetails
    DROP COLUMN [[Conversion_Fee]]]
    

    OR

    sp_RENAME 'dbo.AllocationDetails.[[Conversion_Fee]]]' , 'Conversion_Fee', 'COLUMN'
    

    these will work fine. :)

    Using Double Quotes:

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

    will also work.

    0 讨论(0)
  • 2020-12-19 14:43

    Run following queries together.

    USE [DATABASE_NAME];
    
    GO
    EXEC sp_rename '[SCHEMA_NAME].[TABLE_NAME].OLD_COLUMN_NAME', 'NEW_COLUMN_NAME', 'COLUMN';
    GO
    

    It will resolve your problem

    0 讨论(0)
  • 2020-12-19 14:47

    To fix this:

    sp_RENAME 'dbo.AllocationDetails.[[Conversion_Fee]]]' , 'Conversion_Fee', 'COLUMN'
    
    0 讨论(0)
提交回复
热议问题