Peter DeBetta's blog about programming in SQL Server 2008, 2005, etc. using technologies such as T-SQL, .NET, CLR, C#, VB, Visual Studio, and SQL Server Management Studio.
So I wanted to have some consistency in some T-SQL code for bracketed identifier use. Objects in the code had bracketed identifiers in some cases, and not in others (sometimes even on the next line of code) and so I saw both TableName and [TableName]. Again, RegEx saved the day. For any given object OBJECT_NAME, use the following in the Search and Replace dialog (using the RegEx option):
- Find what:
{[^\[]}{<OBJECT_NAME>}{[^\]]}
- Replace with:
\1\[\2\]\3
This will replace the OBJECT_NAME with [OBJECT_NAME] without losing surrounding white space or punctuation and without doubling up the brackets. So if I had
[SomeSchema].OBJECT_NAME
and did this replace, it would then be
[SomeSchema].[OBJECT_NAME]
And of course [SomeSchema].[OBJECT_NAME] would not be affected, so you would not end up with [SomeSchema].[[OBJECT_NAME]] after replacing.
But of course, be wise and either replace one at a time, or if you choose to replace all, always double check your code afterwards.
Comment Notification
If you would like to receive an email when updates are made to this post, please register here
Subscribe to this post's comments using
About Peter DeBetta
Peter DeBetta is an independent consultant specializing in design, development, implementation, and deployment of Microsoft SQL Server, Microsoft SharePoint Server, and .NET solutions. Peter writes courseware, articles, and books – most recently the title Introducing SQL Server 2008 from Microsoft Press. Peter speaks at conferences around the world, including TechEd, SQL PASS Community Summit, DevTeach, SQL Connections, DevWeek, and VSLive!
Peter is a Microsoft MVP for SQL Server, an MCP, President of the
North Texas SQL Server User Group, and a member of PASS.
When Peter isn’t working, you can find him singing and playing guitar (click
here to hear an original song by Peter), taking pictures, or simply enjoying life with his wife, son, and daughter.