T-SQL Debugging
Finally, you are able to debug
T-SQL from within the SQL Server development environment. Yes, you could
do this kind of thing using Visual Studio, but database developers
should be able to debug in the environment where they generally develop
their SQL statements—within SSMS. SQL Server 2008 provides this
capability, and it works well.
The trickiest part of
debugging may be starting the debugger. It is not all that difficult but
may be less than obvious for some. For example, let’s say you want to
debug a stored procedure. To do this, you right-click on the stored
procedure in the Object Explorer and select Script Stored Procedure As,
Execute To, New Query Editor Window, and a script for executing the
procedure is generated. If the stored procedure has parameters, you add
the SQL to assign a value to those parameters to the script. Now you are
ready to debug this script and the related stored procedure.
To initiate debugging, you
click on the green arrow on the SQL Server menu bar. When you start
debugging, several new debugging windows are added to the SSMS display,
and the query editor window shows a yellow arrow in the left margin next
to the line in the script that
is about to be run. You can now use the debug toolbar at the top of the
SSMS screen to step through your code. If you click the Step Into
button, the current statement executes, and the script progresses to the
next available statement. Figure 12
shows an example of the T-SQL Debugging Environment while debugging is
in progress. The debugging environment enables you to view values
assigned to variables, review the call stack, set breakpoints, and
perform debugging much like you would do in development environments
such as Visual Studio.
Multiserver Queries
Another slick new option
available with SQL Server 2008 is the capability to execute a script on
multiple servers at once. Multiserver queries allow the contents of a
single query editor window to be run against all the servers defined in a
given registered server group. After the group is created and servers
are registered in the group, you can right-click on the group and select
the New Query option to create a query window that can be run against
all the servers in the group. Click on the Execute button, and the query
is run against all the servers. Figure 13 shows a server group named MyTestGroup
containing three servers registered in that group, a sample query to
run against these servers, and a single result window that shows the
results of the query for all servers in the group.
Multiserver queries are
relatively easy to use. The results window includes a Server Name
column that allows you to determine which server the result came from.
These queries are backward compatible and allow you to run against prior
versions of SQL Server, including SQL Server 2005. The only caveat is
that you must first create a registered server group and the related
registered servers before you run the query, but you already know that
this task is also relatively easy.