The sp_rename follies

SQL Server
28 Comments

Before we get started…

I know. I know. BOL. It’s documented.

They even show you how to rename a table.

Thanks, sp_rename!
Thanks, sp_rename!

But sometimes…

You just forget.

And as with most simple mistakes, fixing them is… Weird.

Here’s what happened to me recently, when I was working on a table swapping demo.

Here’s where I was going to rename it, and then make another table with the INT column as a BIGINT.

Which worked, except…

Oh, that's just great.
Oh, that’s just great.

dbo.dbo.

Like most people who make mistakes, I decided to revisit the documentation afterwards. And, yeah, you don’t specify schema in the new object name.

So, now that you all know you’re smarter than me, how would you fix it?

I’ll spare you the trial and error:

There were quite a few different arrangements of brackets and schema prefixes leading up to this.

I hope this post saves someone a little time.

Brent says: dear reader, please use this as part of an April Fool’s prank. Just not in production.

Previous Post
Three Easy Tweaks to Tune Up Your SQL Server
Next Post
Pull Request 101 for DBAs Using GitHub

28 Comments. Leave new

  • Ricardo Marques
    July 8, 2015 8:39 am

    Try this on SQL 2008 and 2008 R2… Create a view, then rename it. Then create a view with the old name of the first view but with a diferent query. Then run sp_refreshview on the the two views. Then check the deffinition of the the views.

    http://sqlblog.com/blogs/davide_mauri/archive/2010/10/12/bug-with-sp-rename-sp-refreshview.aspx

    Reply
  • Geoff Patterson
    July 8, 2015 9:12 am

    Ha, I remember this “dbo.dbo.” causing an error in our testing environment years ago and spending a fair amount of time trying to untangle what was going on and how to fix it. Thanks for a post that brings back amusing memories!

    Reply
  • Alex Friedman
    July 8, 2015 9:39 am

    LOL noob
    😉

    Reply
  • Tony Nitzke
    July 8, 2015 11:13 am

    It makes sense that you don’t specify the schema in the @newname. Otherwise it would imply that you could change the schema with this method.

    Reply
  • While I will admit that I am the “DBA by Accident” type, I am a little confused why this sproc exists…clearly you used to the GUI to demonstrate the “gotcha”. So, why wouldn’t you just right-click on the object and select Rename??? I read M$ documentation, and I don’t see any special functionality that derives from this sproc method over the GUI…Am I missing something?

    Reply
    • Because scripts.

      For everything you can manipulate in the GUI, there should be an equivalent in TSQL. So you can automate / script it.

      Reply
    • Are you going to do that on your Development, Test and Production servers?

      What happens if you accidentally type it incorrectly or hit ‘enter’ before you wanted to in one of the environments

      What happens if this takes down your business critical website?

      Reply
    • Tony Nitzke
      July 9, 2015 6:15 pm

      I would reverse the question. Why would you do something in the GUI that you can do in a script? 🙂

      Reply
  • I did the same thing a while back only I managed to put []’s around my table name. So the actual table name was “[tablename]”. Took me a while to figure out what I’d done since all of my errors came back looking like:

    [tablename] does not exist

    Which looked ok to me.

    Reply
    • reuben anderson
      July 8, 2015 12:17 pm

      Oh, those are wonderful. When the software is suddenly overly literal rather than context sensitive.

      Reply
  • Tim Cartwright
    July 8, 2015 11:43 am

    HAHAHAHA Laughing with you! Not at you! 😀

    Reply
  • I’m chuckling, but only because I did this about a month ago and figured out what happened by looking at (you guessed it) BOL. 🙂

    Reply
  • reuben anderson
    July 8, 2015 12:15 pm

    I just had this for a user yesterday actually.

    This kind of stuff drives me mental just through not being predictable. I get so fed up with having to check the syntax every time I need a statement. I tend to remember the concept regarding what the function achieves.. but then have to look up .. is a table valued function or just a view… which parameters are mandatory … does the WITH clause need brackets or not… sigh. It will never change.

    Reply
  • Matthew Johnson
    July 8, 2015 12:26 pm

    Not sure what happened to my first post: what is the value of using this SP over the GUI? I earned the “DBA by Accident” role, so if there is something I am missing, educate me.

    Reply
    • The advantage of using the SP over the GUI is having the change scripted out so that the change you are making in dev today you can just run in test tomorrow and prod next week. You might also only be able to make changes to prod by checking them into source control and getting somebody else to make them.

      Reply
      • Matthew Johnson
        July 8, 2015 4:45 pm

        Ahhh, the fun parts that I miss out on because, not only do we TECHNICALLY not have team managing our servers, we don’t have a difference between Dev/Test/Prod…not going to defend our environment; just suffice it to say, I have a lot of control here…

        Reply
    • Erik Darling
      July 8, 2015 4:47 pm

      On top of what Stuart mentions, the GUI doesn’t always work either. It didn’t in the real life scenario the demo is based on. If anything else is accessing the object, you’ll just get a lock timeout error. I’ve seen Intellisense be part of a blocking issue at times. It probably would have worked for the demo case, though, since it was on my laptop.

      Reply
    • Alex Friedman
      July 9, 2015 12:16 am

      Also, sometimes you want to do several renames in one transaction while locking the object(s).

      Reply
  • ray herring
    July 8, 2015 12:29 pm

    There are a couple of other “oops that hurt” little stumbling blocks.
    For example Renaming the table does not rename the other dependent objects such as Triggers, Constraints, and Keys. These things exist independently in Sys.Objects and so must have unique names.

    Reply
  • I used sp_rename for the first time not long ago. I used the example from the documentation so got t right but will have to remember this so I don’t get it wrong next time

    Reply
  • or you could just code around it:

    SELECT
    Pizza [Pizza]
    FROM
    dbo.[dbo.Pan] Pan

    Reply
  • well in my corp often peaople rename _bak _OLD _NEWX ect beacause is not use source control all time..so hopfully is not on production but somtime when need do shema compare is verry weird i take more time clean garbage.

    Reply
  • Also – Don’t forget that sp_rename does not rename the other objects associated with a table, like the Primary Key, Constraints, Indexes, Triggers, etc. When you rename a table, you likely also need to rename all of those other objects as well.

    Reply
  • It is really annoying, funnily enough the ssms object explorer thing lets you rename it back without having to think about [‘s in the right places.

    Reply
  • Groooooaaannn…. Yep, done that.
    I like the feature that instantaneously drops all indexes on an index view after doing an ALTER VIEW, without warning. Even when nothing was changed. This has made me laugh a few times, then cry.

    Reply
    • “Feature”

      Reply
      • agreed. considering all the paperwork and hoop-jumping it can take to get an index built on a view in the first place, the fact that it can all evaporate with a simple ALTER VIEW is a darned refreshing bit of convenience, i say.

        Reply
  • Another issue is if you do a rename and have code that follows which uses the renamed table. Validation checks fail:

    EXEC sp_rename ‘dbo.table1’ ‘table2’

    SELECT * FROM dbo.table2…

    Gives “Invalid object name ‘table2′”. Hang on… didn’t I just rename it? It seems the validator (at least on old SQL Versions), fails to acknowledge the rename.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.