1

I have a table with a lot of columns and I would like to add the prefix ABC_ for every columns.

I tried the below query :

    SELECT syscolumns.name as old_column_name, 'ABC_' + syscolumns.name as new_column_name
    FROM sysobjects 
    JOIN syscolumns ON sysobjects.id = syscolumns.id
    WHERE sysobjects.name = 'ABCtable'
    ORDER BY sysobjects.name,syscolumns.colid

When I execute above query I got all my columns with the prefix ABC_ but in my table nothing has changed.

Pac0
  • 21,465
  • 8
  • 65
  • 74
Paul
  • 43
  • 3
  • 1
    You have just done a regular select statement and not told it to update anything. – arahman Jan 08 '19 at 09:03
  • 2
    Possible duplicate of [Rename column SQL Server 2008](https://stackoverflow.com/questions/16296622/rename-column-sql-server-2008) – P.Salmon Jan 08 '19 at 09:05
  • 3
    Why/how is adding `ABC_` onto the start of all of the column names going to *help* you here? You'll still have the same large amount of columns and you've now worsened e.g. intellisense. – Damien_The_Unbeliever Jan 08 '19 at 09:07
  • No I have to do this to realize a jointure with an other table. It is not a duplicate because in my cas I want to add a prefix for each columns not to rename one column. – Paul Jan 08 '19 at 09:11
  • 1
    Have you considered using table aliases to disambiguate column names from different tables and column aliases to uniquely label them? – P.Salmon Jan 08 '19 at 09:16
  • No how do you do this ? – Paul Jan 08 '19 at 09:17
  • Don't do this. You will gain nothing! – jarlh Jan 08 '19 at 09:48
  • Request you to up vote my answer if you found right and helpful. – Suraj Kumar Jan 08 '19 at 10:15

3 Answers3

1

You can try something like this.

    DECLARE @Colums AS TABLE(IndexNo INT IDENTITY(1,1), OldCol VARCHAR(MAX), NewCol VARCHAR(MAX))
    INSERT INTO @Colums
    SELECT syscolumns.name as old_column_name, 'ABC_' + syscolumns.name as new_column_name
    FROM sysobjects 
    JOIN syscolumns ON sysobjects.id = syscolumns.id
    WHERE sysobjects.name = 'ABCtable'
    ORDER BY sysobjects.name,syscolumns.colid


    DECLARE @I INT=1;
    WHILE EXISTS(SELECT * FROM @Colums WHERE IndexNo=@I)
    BEGIN
        DECLARE @OldCol VARCHAR(MAX)='', @NewCol VARCHAR(MAX)=''
        SELECT @OldCol='ABCtable.'+OldCol, @NewCol=NewCol FROM @Colums WHERE IndexNo = @I

        EXEC sp_rename @OldCol, @NewCol, 'COLUMN'

        SET @I = @I+1;
    END
Dumi
  • 1,414
  • 4
  • 21
  • 41
0

You can use sp_rename to rename a column/table.

EXEC sp_rename 'TableName.OldColumnName', 'NewColumnName', 'COLUMN'
Buchiman
  • 320
  • 5
  • 18
0

You can create a cursor as shown below. Please find the below query.

create table studInfo (Id int, EmpCode Varchar(20), Name Varchar(20))
insert into studInfo values (1,'1001', 'A'),(2, '1002','B'), (3, '1003','C'), (4,'1004', 'D'), (5,'1005', 'E')

select * from studInfo

Declare @OldSQLcolName Varchar(50)
Declare @NewColName Varchar(50)

DECLARE RenameCursor CURSOR FOR
SELECT 'studInfo.' + COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'studInfo'

OPEN RenameCursor;
FETCH NEXT FROM RenameCursor INTO @OldSQLcolName;

WHILE @@FETCH_STATUS = 0
   BEGIN      
      set @NewColName = 'ABC_' + Replace(@OldSQLcolName,'studInfo.','')
      --select @OldSQLcolName, @NewColName

      EXEC sp_rename @OldSQLcolName, @NewColName, 'COLUMN'
      FETCH NEXT FROM RenameCursor INTO @OldSQLcolName;   
   END

CLOSE RenameCursor
DEALLOCATE RenameCursor

select * from studInfo
drop table studInfo

The output before and after updated column name is as shown below

enter image description here

You can find the live demo Live Demo Here

Suraj Kumar
  • 5,547
  • 8
  • 20
  • 42