rename a column name in a table ?

  • Is it possible to use a stored procedure to rename a column name in a table without losing the data held within it?

  • I've found this by searching this forum,

    exec sp_rename 'dbo.TableName.newcol','OldCol','COLUMN'

    what I'd like to do is make a Stored procedure that I can pass in TableName, oldcolumn, newColumn. So it needs to be done dynamically

  • you can create a procedure that uses sp_rename...something like this:

    create procedure sp_ChangeColumnName
    @table_name varchar(50),
    @old_name varchar(50),
    @new_name varchar(50)
    as
    
    declare @sql varchar(150)
    
    set @sql = 'sp_rename "' + @table_name + '.' + @old_name + '", "' + @new_name + '", "' + 'column"'
    exec(@sql)
    
    Then execute this by passing the parameters...
    
    
    exec sp_ChangeColumnName 'table_name', 'old_name', 'new_name'
    







    **ASCII stupid question, get a stupid ANSI !!!**

  • Go to Enterprise Manager,select database, select the table name and select the column name right click select design table. You can change what ever you want. Ok....

    Ramaa

  • many thanks Rama, that's helped a great deal

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply