Problem renaming column in Sql Server table

Asked

Viewed 697 times

0

I needed to rename a column from a table in Sql Server and used the stored SP_RENAME system processor.

I used it as follows:

USE [meu_db]
GO

EXEC sp_rename '[dbo].[minhaTabela].[minhaColunaAntiga]', '[minhaColunaNova]', 'COLUMN';  
GO

That is, I tried to change the name from 'myColunaAntiga' to 'myColunaNova', but the way I put it, sp_rename considered the characters '[' and ']' literal and the column became called '[myColunaNova]' instead of 'myColunaNew'.

Now I’m trying to fix it like this:

EXEC sp_rename '[dbo].[minhaTabela].[minhaColunaNova]', 'minhaColunaNova', 'COLUMN';  
GO

But it didn’t work.

I tried too:

EXEC sp_rename '[dbo].[minhaTabela].[[minhaColunaNova]]', 'minhaColunaNova', 'COLUMN';  
GO

But it didn’t work either. I even tried with 3 '[]' and it didn’t work either.

Someone would have an idea how to fix this?

2 answers

2


  • 1

    It worked, I used it like this: EXEC sp_rename 'dbo.minhaTabela."[minhaColunaNova]"', 'minhaColunaNova', 'COLUMN';
GO. Thank you Seal.

1

Just remove the keys from your first call:

USE meu_db
GO

EXEC sp_rename 'minhaTabela.minhaColunaAntiga', 'minhaColunaNova', 'COLUMN';  
GO

SP_RENAME

Changes the name of a user created object in the current database. This object can be a table, index, column, alias type data, or Microsoft . NET User-defined framework common language type Runtime (CLR).

SP_RENAME - B. Renaming a column

USE AdventureWorks2012;  
GO  
EXEC sp_rename 'Sales.SalesTerritory.TerritoryID', 'TerrID', 'COLUMN';  
GO  
  • I understand Sorack, but I already executed the code and the column is now called '[myColunaNova]', with the characters '[]'. So I need to rename from '[myColunaNova]' to'myColunaNova'. Get it?

  • @Marcostulio is not used [] in column names

  • 1

    Yeah, I figured that out now. It served as an apprenticeship. Thanks.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.