Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rename a table in SQL Server?

The SQL query that I have used is :

ALTER TABLE oldtable RENAME TO newtable; 

But, it gives me an error.

Server: Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'TO'.

like image 590
Switch Avatar asked Mar 17 '11 06:03

Switch


People also ask

Can we rename the table name?

Any database user can easily change the name by using the RENAME TABLE and ALTER TABLE statement in Structured Query Language. The RENAME TABLE and ALTER TABLE syntax help in changing the name of the table.


2 Answers

To rename a table in SQL Server, use the sp_rename command:

exec sp_rename 'schema.old_table_name', 'new_table_name' 
like image 63
Jeff Hornby Avatar answered Oct 15 '22 22:10

Jeff Hornby


To rename a column:

sp_rename 'table_name.old_column_name', 'new_column_name' , 'COLUMN'; 

To rename a table:

sp_rename 'old_table_name','new_table_name'; 
like image 44
Ravi Kumar Avatar answered Oct 15 '22 22:10

Ravi Kumar