Blog Post

Perfectly Placed Parentheses

,

Like many other programming languages T-SQL uses parentheses () for a number of tasks. To help determine precedence, function calls etc. Now it’s nothing like LISP but with enough in one statement it can still get confusing at times.

The last page of any Lisp program

The last page of any Lisp program

Here is a fairly simple T-SQL Example

Parens1

Intellisense is telling us that something is wrong, but what? If you can tell just by looking more power to you. I personally need help, and this isn’t anything like the worst I’ve ever seen. (Subqueries containing function calls and other subqueries for example.)

Parens

You’ll notice that when I go over the parentheses the one I’ve selected and it’s pair turn yellow, unless there isn’t a pair of course. You can also use Ctrl-] to flip between the open and close parenthesis in a pair. This can be particularly useful to make sure that you remembered a close parenthesis at the end of a subquery. In this case that last close parenthesis doesn’t have a match. Now finding out that you are missing an open parenthesis doesn’t mean you know where it’s supposed to go. But you can track the different pairs, making sure that each time you open a parenthesis you close it in the correct place. In this case it belonged right at the beginning.

FYI yellow isn’t the default (it’s a light gray). I find the default hard to see (I’m getting old) so I changed it to yellow in the options under fonts and colors.

Parens2

Last but not least this particular functionality is called Automatic Matching of Syntax Pairs and will also affect BEGIN/END, BEGIN TRY/END TRY and BEGIN CATCH/END CATCH pairs. At least in the query editor window. There are other pairs in other editors.

Filed under: Microsoft SQL Server, SQLServerPedia Syndication, SSMS, T-SQL Tagged: microsoft sql server, SSMS, T-SQL

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating