131

How can I in change the table name using a query statement?

I used the following syntax but I couldn't find the rename keyword in SQL server 2005.

Alter table Stu_Table rename to Stu_Table_10
1
  • ALTER TABLE TABLE_NAME RENAME TO NEW_TABLE_NAME Works only in Oracle DB. Commented Feb 23, 2013 at 15:53

11 Answers 11

243

Use sp_rename:

EXEC sp_rename 'myschema.Stu_Table', 'Stu_Table_10'

You can find documentation on this procedure on MSDN.

If you need to include a schema name, this can only be included in the first parameter (that is, this cannot be used to move a table from one schema to another).

Sign up to request clarification or add additional context in comments.

2 Comments

Please remember that for sp_rename it is only the first parameter that can take a Schema name, the second argument MUST be without the Schema name. As in EXEC sp_rename 'dbo.Stu_Table', 'Stu_Table_10'
Henrik's comment is pretty important, this should probably be part of the answer? Saved me from some headache.
71

In MySQL :-

RENAME TABLE `Stu Table` TO `Stu Table_10`

4 Comments

The question is about SQL Server, your answer is for MySQL.
I upvoted because this question is referenced in google for mysql too.
Worked for me with Mysql
+1 for mysql answer as Google brought me here also. Remember even tho OP ticks the answer that worked for them, others may find alternative answers useful such as myself.
18

In PostgreSQL:

Alter table student rename to student_details;

Comments

17

Please use this on SQL Server 2005:

sp_rename old_table_name , new_table_name

it will give you:

Caution: Changing any part of an object name could break scripts and stored procedures.

but your table name will be changed.

Comments

6

In MySQL :

RENAME TABLE template_function TO business_function;

Comments

3

ALTER TABLE table_name RENAME TO new_table_name; works in MySQL as well.

Screen shot of this Query run in MySQL server

Alternatively: RENAME TABLE table_name TO new_table_name; Screen shot of this Query run in MySQL server

Comments

2

Syntex for latest MySQL versions has been changed.

So try RENAME command without SINGLE QUOTES in table names.

RENAME TABLE old_name_of_table TO new_name_of_table;

Comments

0
RENAME TABLE old_table_name TO new_table_name;

Comments

0

SQL Server table name can be changed in two ways

  1. Execute the below query, by using the system stored procedure sp_rename

    EXEC sp_rename 'dbo.old_table_name','new_table_name';

  2. Open SSMS and expand the database folder. Right-click on a table and click Rename. Enter a new name by over writing on existing name and then Go to the file menu and click Save.

enter image description here

Comments

-1

rename table name :

RENAME TABLE old_tableName TO new_tableName;

for example:

RENAME TABLE company_name TO company_master;

1 Comment

Could you point to the SQL server 2005 documentation where this is described?
-1

execute this command

sp_rename 'Employee','EData'

Comments