Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql server 2008: sp_RENAME table disappeared

Tags:

sql

sql-server

I renamed the table by sp_RENAME (SQL SERVER 2008)

sp_RENAME 'dbname.scname.oldtblname' 'dbname.scname.newtblnam'

The resulting message (it was in black color - so I get it as just a warning or successful message) was something like "Caution: Changing any part of an object name could break scripts and stored procedures."

So after this command my table with 45 million records just disappeared and I don't have any backup. This was a new table.

:) Do you guys have any idea about bringing my table back? :)

p.s. Yeah, my smile is not ":(", because when the seriousness of the problem goes up the threshold, ":(" becomes ":)".

like image 553
Azho KG Avatar asked Sep 22 '10 10:09

Azho KG


People also ask

What is Sp_rename in SQL Server?

sp_rename automatically renames the associated index whenever a PRIMARY KEY or UNIQUE constraint is renamed. If a renamed index is tied to a PRIMARY KEY constraint, the PRIMARY KEY constraint is also automatically renamed by sp_rename . sp_rename can be used to rename primary and secondary XML indexes.

Which command is used to rename a table?

ALTER TABLE table_name RENAME TO new_table_name; Columns can be also be given new name with the use of ALTER TABLE.


2 Answers

  • What does this say?
    SSMS does not refresh Object explorer automatically so it could be there

    USE dbname SELECT OBJECT_ID('scname.newtblnam')

  • sp_rename says

You can change the name of an object or data type in the current database only. The names of most system data types and system objects cannot be changed.

You specified dbname so it's possible you have an object [dbname.scname.newtblnam] in dbo schema (or similar)

  • And did you do a backup first? Best practice before any (formal) schema changes, y'know

FWIW, I've never lost a table or other object using sp_rename

like image 129
gbn Avatar answered Sep 24 '22 21:09

gbn


Faced that awful bug too, here is the solution:

--original
sp_RENAME 'dbname.scname.oldtblname', 'dbname.scname.newtblnam' 

--workaround 
sp_RENAME 'dbname.scname.[dbname.scname.newtblnam]','oldtblname' 

name in [] is actually your TABLE name after sp_rename, to make SQL server to understand it put it in square brackets.

like image 38
Stan Avatar answered Sep 22 '22 21:09

Stan