Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to rename column name with T-SQL

I need help with SQL Server Table Column. I´m trying to rename Column Name, but this doesn't work.

USE COST_mesta_test GO EXEC sp_rename '[dbo].[1965$].obec' , 'people' , 'COLUMN' GO 

I tried different combination of brackets [] and path to table "obec" (for example: 1965.obec; 1965$.obec; 1965.[obec]; etc.) But still i get an error:

Either the parameter @objname is ambiguous or the claimed @objtype (COLUMN) is wrong.

like image 375
VilemRousi Avatar asked Feb 20 '12 00:02

VilemRousi


People also ask

How do I change the column name in T?

You can also change the column name by right-clicking on the Column name and choosing 'rename' option in the Object Explorer.


1 Answers

I don't really understand how your table is set up - ie. the table name, the column name etc - so this is an example of how the proc works for column renames:

If I had a table like this:

CREATE TABLE [dbo].[Company]( [ID] [int], [CompanyName] [varchar](20) ) 

and wanted to change the [CompanyName] column, this is the command:

EXEC sys.sp_rename      @objname = N'dbo.Company.CompanyName',      @newname = 'Name',      @objtype = 'COLUMN' 

I suspect that your first argument is not correct.

From the documentation (sp_rename (Transact-SQL))

If the object to be renamed is a column in a table, object_name must be in the form table.column or schema.table.column. If the object to be renamed is an index, object_name must be in the form table.index or schema.table.index

like image 52
Beno Avatar answered Oct 24 '22 00:10

Beno