SSMS delivers an equally impressive number of
features for database developers. Many of the features were available
with SQL Server 2005, but SQL Server 2008 has added some new ones as
well. T-SQL Debugging, IntelliSense in the Query Editor, and multiserver
queries are a few of those new tools for developers found in SQL Server
2008.
The Query Editor
The Query Editor sits at the
top of the list for development tools in SSMS. The Query Editor, as its
name indicates, is the editing tool for writing queries in SSMS. It
contains much of the functionality that was contained in SQL Server
2000’s Query Analyzer. The capability to write T-SQL queries, execute
them, return results, generate execution plans, and use many of the
other features you may be familiar with in Query Analyzer are also
available with the Query Editor.
One main difference with the
Query Editor is that is has been integrated into the SSMS environment.
In SQL Server 2000, the Query Analyzer was a separate application with
its own independent interface. In SQL Server 2008, SSMS houses the
query-editing capabilities along with all the administrative
capabilities.
Note
The biggest upside to
the integration of the query-editing tool into the SSMS environment is
that you can find almost anything you need to administer or develop on
your SQL Server database in one spot. There is no need to jump back and
forth between applications. One possible downside, however, is that SSMS
may be much more than some database developers need.
Clicking the New Query
button, opening a file, and selecting the Script to File option from a
list of database objects in the Object Explorer are just a few of the
ways to launch the Query Editor. Figure 1 shows the query editor window with a sample SELECT statement from the AdventureWorks2008 database. The query editor window is displayed on the right side of the screen and the Object Explorer on the left side.
The basic editing
environment within the Query Editor is similar to Query Analyzer. The
top portion of the query editor window contains the query. The bottom
portion contains the results of an executed query. The results can be
displayed as text, displayed in a grid format, or output as XML.
However, in the Query Editor, windows are by default managed differently
than with Query Analyzer. Multiple query editor windows are displayed
in a tabbed format; in comparison, Query Analyzer displayed a separate
window for each query.
Tip
The
tabbed document display has some advantages, but you can set an option
in SSMS that causes the Query Editor to behave much like the Query
Analyzer. To do this, you select Tools, Options to launch the Options
dialog. The default page has a section named Environmental Layout. If
you choose the MDI Environment option, you set SSMS in MDI mode instead
of the tabbed layout.
IntelliSense
IntelliSense has finally made it
to the SQL Server Query Editor. This much-anticipated tool was slated
for SQL Server 2005, but it was pulled before making it to the
marketplace. Fortunately, it made it to SQL Server 2008, and it was
worth the wait. This is especially true for those developers who have
been working with Visual Studio or other Microsoft development tools
that have this feature.
IntelliSense is a handy
tool that helps you complete queries as you are typing them in the query
editor window. Start typing and you will see. For example, type SELECT * FROM A
in the query editor window, and a drop-down appears in the query editor
window after you start typing the first letter after the FROM
clause. The drop-down, in this case, contains the databases and tables
from which you can select data. If you type in a stored procedure name
to execute, a drop-down shows you the parameters that the stored
procedure accepts. Type SYS. in the query editor window, and you see a drop-down of all the objects available in the SYS
schema. This includes catalog views and the related columns that these
views contain. If you type in a query that is incorrect, IntelliSense
places a red squiggly line under the part of the query that is
syntactically incorrect.
The value of this tool will
become more apparent as you use it. It can be confusing at times, but
it will ultimately speed up your development time. It can also reduce
the number of times you need to go to Books Online or some other help
source and will make your development life easier.
Note
IntelliSense works only with SQL
Server 2008 databases. If you start typing a query against a database
from a prior version, the handy IntelliSense drop-downs do not appear.
Query Editor Types
The Query Editor in SQL Server
2008 enables you to develop different types of queries. You are not
limited to database queries based on SQL. You can use the Query Editor
to develop all types of SQL Server Scripts, including those for SQL
Server Analysis Services (SSAS) and SQL Server Mobile Edition. The SSAS
queries come in three different flavors: multidimensional expressions
(MDX), data mining expressions (DMX), and XML for analysis (XMLA). Only
one selection exists for creating SQL Server Mobile Edition scripts.
You
see these new query options when you create a new query. When you
select New from the SSMS menu, you can choose what type of query to
create. You use the Database Engine Query choice to create a T-SQL query
against the Database Engine. The other new query options correspond to
SSAS and SQL Server Mobile Edition. The SSMS toolbar has icons that
correspond to each type of query that can be created.
Each query type has a code
pane that works much the same way across all the different types of
queries. The code pane, which is the topmost window, color-codes the
syntax that is entered, and it has sophisticated search capabilities and
other advanced editing features that make it easy to use.
Disconnected Editing
SQL Server 2008 is able to
use the code editor without a database connection. When creating a new
query, you can choose to connect to a database or select Cancel to leave
the code pane disconnected. To connect to the database later, you can
right-click in the code pane window and select the Connect option. You
can also disconnect the Query Editor at any time or choose the Change
Connection option to disconnect and connect to another database all at
once.
Along with disconnected
editing are some changes to the Windows behavior that are worth noting.
The biggest changes relate to the behavior of query windows currently
open at the time a file is opened for editing. With SQL Server 2000
Query Analyzer, the currently selected window would be populated with
the contents of the file you were opening. Prior to this replacement, a
prompt would be displayed asking whether you wanted to save your
results. If the query window was empty, the contents would be replaced
without the prompt for saving.
With SQL Server 2008, a new query
window is opened every time a new file is opened. The new window
approach is faster but can lead to many more open windows in the
document window. You need to be careful about the number of
windows/connections you have open. Also, you need to be aware that the
tabbed display shows only a limited number of windows. Additional
connections can exist even if their tabs are not in the active portion
of the document window.
Editing sqlcmd Scripts in SSMS
sqlcmd is a
command-line utility introduced in SQL Server 2008. You can use it for
ad hoc interactive execution of T-SQL statements and scripts. It is
basically a replacement for the ISQL and OSQL commands used in versions prior to SQL Server 2005. (OSQL still works with SQL Server 2008, but ISQL has been discontinued.)
You can write, edit, and execute sqlcmd scripts within the Query Editor environment. The Query Editor in SSMS treats sqlcmd
scripts in much the same way as other scripts. The script is
color-coded and can be parsed or executed. This is possible only if you
place the Query Editor in SQLCMD mode, which you do by selecting Query,
SQLCMD Mode or selecting the SQLCMD mode icon from the SSMS toolbar.
Figure 2 shows a sample sqlcmd script in SSMS that can be used to back up a database. This example illustrates the power and diversity of a sqlcmd script that utilizes both T-SQL and sqlcmd statements. It uses environment variables set within the script. The script variables DBNAME and BACKUPPATH are defined at the top of the script with the SETVAR command. The BACKUP statement at the bottom of the script references these variables, using the convention $(variablename), which substitutes the value in the command.
sqlcmd scripts that
are edited in SSMS can also be executed within SSMS. The results are
displayed in the results window of the query editor window, just like
any other script. After you test a script, you can execute it by using
the sqlcmd command-line utility. The sqlcmd command-line utility is a powerful tool that can help automate script execution.
Regular Expressions and Wildcards in SSMS
SSMS has a robust search
facility that includes the use of regular expressions. Regular
expressions provide a flexible notation for finding and replacing text,
based on patterns within the text. Regular expressions are found in
other programming languages and applications, including the Microsoft
.NET Framework. The regular expressions in SSMS work in much the same
way as these other languages, but there are some differences in the
notation.
The option to use
regular expressions is available whenever you are doing a find or
replace within an SSMS script. You can use the find and replace option
in the code pane or results window. You can use the Find and Replace
option from the Edit menu or press either the Ctrl+F or Ctrl+H shortcut
keys to launch the Find and Replace dialog box. Figure 3 shows an example of the Find and Replace dialog that utilizes a regular expression. This example is searching for the text Customer, preceded by the @ character and not followed by the Id
characters. This kind of search could be useful for searching a large
stored procedure where you want to find the customer references but
don’t want to see the variables that contain customer in the first part of the variable name.
You use regular expressions
only when the Use check box in the Find and Replace dialog is selected.
When this option is selected, you can choose either Regular Expressions
or Wildcards. Wildcard searches work much the same way in SSMS as they
do in file searches. For example, if you want to find any references to
the word zip, you could enter *zip* in the Find What text box. The wildcard options are limited but very effective for simple searches.
Regular expressions have a much
more extensive number of available search options. When you choose the
option to use regular expressions, the arrow button is enabled to the
right of the text box where you enter your search text. If you click
this button, you are given an abbreviated list of regular expression
characters that you can use in your searches. A brief description of
what each character represents in the search is listed next to the
character. For a complete list of characters, you can choose the
Complete Character List option at the bottom of the list. This option
brings you to the Books Online topic “How to: Search with Regular Expressions,” which gives a comprehensive review of all the characters.