SQL SERVER – Rename a Table Name Containing [ or ] Identifier in the Name – Part 2

Yesterday I posted article where we discussed how to rename a table name where identifier is part of the tablename. Read the blog post Rename a Table Name Containing [ or ] in the Name – Identifier in the Table Name. If the table name contains an identifier, it is not easy to rename a table, the default method will show an error as displayed below.

sp_rename '[]', 'ProjectA';

The above query will give us following error:

Msg 15253, Level 11, State 1, Procedure sp_rename, Line 107
Syntax error parsing SQL identifier ‘[]’.

SQL SERVER - Rename a Table Name Containing [ or ] Identifier in the Name - Part 2 renameerror1

This is because our table name contains Identifier [ as well as Identifier ]. One of the method was to rename table was to use Double Quotes around the identifier.

sp_rename '"[]"', 'ProjectA';

When we run above query, it will give us success message and rename our table to the new name.

SQL SERVER - Rename a Table Name Containing [ or ] Identifier in the Name - Part 2 renameerror2

At the end of the blog post, I asked if there is any other way to the same task.

SQL Expert Parth Malhan answered in the comment with alternative solution where he demonstrates that we can use identifier around the name of the table and rename the column as well.

EXEC sys.sp_rename '[[]]]','ProjectA'

SQL SERVER - Rename a Table Name Containing [ or ] Identifier in the Name - Part 2 renameerror3

Thanks Parth, very cool trick!

Reference: Pinal Dave (https://blog.sqlauthority.com)

SQL Server Management Studio
Previous Post
SQL SERVER – Rename a Table Name Containing [ or ] in the Name – Identifier in the Table Name
Next Post
SQL SERVER – Monitoring Server at a Glance – A Open Conversation with DBAs

Related Posts

1 Comment. Leave new

Leave a Reply