Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Renamed a table to sp_help and cannot rename it back

Somehow one of our tables was accidentally renamed to 'sp_help'.

We have tried to rename it back to the previous table name using sp_rename but we are getting the following error:

Msg 15225, Level 11, State 1, Procedure sp_rename, Line 332
No item by the name of 'MyDatabase.dbo.sp_help' could be found in the current database 'MyDatabase', given that @itemtype was input as '(null)'.

We have also tried to rename it directly in management studio with the same error. Is there any other way of renaming this table back to the previous name?

like image 390
spfuller Avatar asked Jan 15 '14 13:01

spfuller


People also ask

How do I rename a SQL replicated table?

Uncheck the table you want to rename, then rename the table, then re-add it to the article list (make sure to uncheck "Show only checked articles in the list" option to see all addable objects). Then start a new snapshot in the snapshot agent: Local Publications -> Properties -> View Snapshot Agent Status -> Start.

Can we rename table name in SQL?

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.


1 Answers

ALTER the metadata of the dbo.sp_help object and perform a SWITCH.

Here's a test case that should get you started:

CREATE TABLE dbo.t_Foo
(
    Bar         BIT
);
GO

EXECUTE dbo.sp_rename @objname = 'dbo.t_Foo', @newname = 'sp_help';
GO

CREATE TABLE dbo.t_Foo
(
    Bar         BIT
);
GO

ALTER TABLE [dbo].[sp_help]
SWITCH TO dbo.t_Foo;
GO

SELECT  *
FROM    dbo.t_Foo;
GO

DROP TABLE [dbo].[sp_help]
GO
like image 120
Avarkx Avatar answered Sep 19 '22 03:09

Avarkx