Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to remove square brackets and period from table name? 1

Status
Not open for further replies.

Spork52

Programmer
Nov 20, 2007
134
US
I have a table with square brackets and a period in the table name like this:

[dbo].[mytable]

The brackets and dbo and period are all part of the unqualified table name (please don't ask me to explain how this mess happened).

I want to rename this table without brackets, periods, dbo, but I can't execute any SQL statements on it without getting errors.

I've tried the following:

Code:
sp_rename '[dbo].[my_table]', 'mytable'

ERROR: No item by the name of '[dbo].[my_table]' could be found in the current database 'mydatabase', given that @itemtype was input as '(null)'. 

sp_rename '[[dbo].[my_table]]', 'mytable'

ERROR: Syntax error parsing SQL identifier '[[dbo].[my_table]]'. 

sp_rename '[dbo].[[dbo].[my_table]]', 'mytable'

ERROR: Syntax error parsing SQL identifier '[[dbo].[my_table]]'.

Do the brackets have to be escaped? If so, how? I've tried enclosing them in brackets: [[] and []]. And I've tried doubling them: [[ and ]]. Nothing works.

I am using Aqua DataStudio 4.7 (similar to Enterprise Manager) to run queries.
 
Run this:

SELECT * FROM INFORMATION_SCHEMA.TABLES

and see how it is displayed in that resultset.
 
Try this:

Code:
sp_rename '[[dbo]].[my_table]]]', 'mytable'

I figured this out by running this...

[tt][blue]Select QuoteName('[dbo].[my_table]')[/blue][/tt]

Make sense?

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
That worked! Thank you so much for an incredibly fast answer. I'll remember QuoteName next time. It looks as if only the closing brackets are escaped by doubling up ]] and then the entire table name is enclosed in square brackets.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top