How to rename a table in SQL Server?

后端 未结 8 1273
忘了有多久
忘了有多久 2020-12-12 10:10

The SQL query that I have used is :

ALTER TABLE oldtable RENAME TO newtable;

But, it gives me an error.

8条回答
  •  盖世英雄少女心
    2020-12-12 11:08

    If you try exec sp_rename and receieve a LockMatchID error then it might help to add a use [database] statement first:

    I tried

     exec sp_rename '[database_name].[dbo].[table_name]', 'new_table_name';
     -- Invalid EXECUTE statement using object "Object", method "LockMatchID".
    

    What I had to do to fix it was to rewrite it to:

    use database_name
    exec sp_rename '[dbo].[table_name]', 'new_table_name';
    

提交回复
热议问题