Peter DeBetta's blog about programming in SQL Server 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:
- Replace with:
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
and did this replace, it would then be
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.
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 W. DeBetta
Peter DeBetta works for Microsoft as an Application Platform Technology Strategist, specializing in strategies, design, implementation, and deployment of Microsoft SQL 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!
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.