|
Lesson 21: The Query Analyzer
Lesson 21 The Query AnalyzerIn this lesson you’ll learn how to:
In the previous lessons, we’ve used the Enterprise Manager to create and maintain database objects and to perform basic data operations. In this lesson, we’ll look at another one of Microsoft SQL Server’s graphical toolsthe Query Analyzer.
Understanding the Query AnalyzerAlthough it’s possible to create and execute queries and other Transact-SQL statements in the Enterprise Manager, its greatest strength is as a tool for database administration. The Query Analyzer, on the other hand, is primarily a programming tool.The Query Analyzer provides you with powerful tools for writing and debugging complex sets of Transact-SQL statements in various forms. (We’ll look at one of these, SQL scripts, in this lesson.) It also provides the means to analyze the performance of queries via execution plans and the Index Tuning Wizard, as we’ll see in Lesson 23.
Starting the Query AnalyzerYou can start the Query Analyzer from within the Enterprise Manager or from the Windows Start menu. When you start the Query Analyzer from the Enterprise Manager, the Query Analyzer will transfer the connection information from the Enterprise Manager: if you are connected to a server, the Query Analyzer will connect to that server, and if you have a database selected, the Query Analyzer will select that database.
TIP: You can use the isqlw command from the command prompt as a third way to start the Query Analyzer. If you start the Query Analyzer from the Start menu, or if you haven’t made a server or database connection in the Enterprise Manager, you must make the connection manually from within the Query Analyzer. Start the Query Analyzer from the Enterprise Manager
SQL Server displays a list of database objects in the Details Pane.
SQL Server opens the Query Analyzer, automatically connecting to the server and the Aromatherapy database.
NOTE: If you don’t see the Object Browser in the Query Analyzer, press F8 to display it.
Start the Query Analyzer from the Start menu
The icons in the Microsoft SQL Server folder appear in a list.
Query Analyzer icon The Query Analyzer displays the Connect to SQL Server dialog box.
The Query Analyzer connects to the server, and then opens. The Query Analyzer will connect to whichever database has been selected as the default for your login.
TIP: If your server isn’t set to start automatically, you can use the Start SQL Server If It Is Stopped check box to automatically start the server when you connect.
Selecting a DatabaseThe Query Analyzer will use the currently selected database to resolve references in queries and other Transact-SQL statements. Its toolbar, shown in Figure 21-1, contains a combo box showing the database that is currently selected. You can switch to a different database by selecting its name in the toolbar or from the Query menu.
Figure 21-1. The Query Analyzer toolbar displays the currently selected database. Selecting a database using the toolbar
The Query Analyzer displays the Select Database dialog box.
TIP: You can also use the shortcut key Ctrl-U to open the Select Database dialog box.
The Query Analyzer selects the Aromatherapy database.
Using the Query WindowThe Query Analyzer initially displays two windows, the Object Browser and the Query window. When you first open the Query Analyzer, a single Query window is opened, but you can open a new window at any time by clicking the New Query button on the Query Analyzer toolbar.
New Query button The Query window displays the name of the database server, the current database, the current login, and the query name in the title bar. The Query window is similar to the Enterprise Manager’s Query Designer but is much more powerful. As an editor, the Query window is more flexible and powerful than the Query Designer’s SQL Pane. While the Query Designer can process only a limited set of SQL statements, the Query window can process any valid Transact-SQL statement, and you can enter multiple statements for processing in a single batch. Although the Query window doesn’t support the Grid or Diagram views available in the Query Designer, it supports additional views of a query that are used to analyze performance, as we’ll see in Lesson 23. The Query window also gives you the option of displaying each view of the query in a separate tab, rather than as panes. This is often the most convenient way of viewing the query.
Entering Transact-SQL StatementsThe simplest way to use the Query window is to type in an SQL statement, just as you would in the SQL Pane of the Query Designer. Unlike the Query Designer, however, the Query Analyzer’s Query window provides some extra assistance by color coding the Transact-SQL statements you enter. Table 21-1 shows the colors the Query window uses.
Table 21-1. Color coding used in the Query Analyzer’s Query window. Execute a SELECT query
SELECT OilID, OilName, LEFT(LatinName, 10)
Execute Query button
The Query Analyzer adds a pane to the Query window containing two tabs: the Grids tab, which contains the query results, and the Messages tab.
The Query window displays the messages generated by the query.
The Query Analyzer displays the Options dialog box.
The Query Analyzer changes the display of the Query window.
Using SQL ScriptsA script is a set of Transact-SQL statements stored in a file. Scripts are most often used to keep a permanent record of the commands used to create and populate database objects. Because the scripts are stored in text files, rather than in the database, they can be used to re-create the database on different servers. (SQL Server actually uses scripts to create the Pubs and Northwind sample databases.)Although scripts are most often used for creating database objects, they’re not limited to this use. Any valid Transact-SQL statement can be included in a script. The SQL statements in a script are grouped into batches. A script can contain one or more batches, and each batch can contain one or more SQL statements. In a script containing more than one batch, the batches are separated by the GO command. If a script doesn’t contain the GO command, all the statements will be executed as a single batch. Create a script
SELECT OilID, OilName, LEFT(LatinName, 10) TIP: Only the last three lines have been added to the query used in the previous exercise.
The Query Analyzer displays the results in two panes of the Grids tab of the Query window.
Save button
The Query Analyzer displays the Save Query dialog box.
IMPORTANT: If you don’t select the Editor tab before clicking the Save button, the Query Analyzer will save the result of the query, rather than the query itself.
The Query Analyzer saves the new script file. Open a script
New Query button
The Query Analyzer opens a new, empty Query window.
Open button
The Query Analyzer displays the Open Query File dialog box.
The Query Analyzer displays the script in the Query window.
Execute Query button
The Query Analyzer displays the results in the Grids tab.
Using the Object BrowserThe Object Browser is contained in the pane on the left side of the Query Analyzer window. Just as the Console Tree of the Enterprise Manager provides a hierarchical view of the objects in a SQL Server installation, the Objects tab of the Object Browser displays a hierarchical view of the databases in the server to which you’re connected, as well as other common objects used in creating Transact-SQL programs.
TIP: The Templates tab of the Object Browser contains a hierarchical view of the available programming templates. We’ll look at templates in the next lesson. The Objects tab of the Object Browser is arranged somewhat differently from the Console Tree in the Enterprise Manager. Only database objects are listed, so items such as Logins and Data Transformation Services packages aren’t visible. Also, the tables in a database are divided into two folders for convenience: User Tables and System Tables. Additionally, rather than simply listing the tables in a database the way the Enterprise Manager Console Tree does, the Object Browser displays the columns, indexes, constraints, and triggers that you’ve defined for the table, as well as the dependencies, the views, and the tables that depend on the table. Figure 21-2 shows the Object Browser display for the Oils table.
Figure 21-2. The Object Browser displays information about the tables in your database that’s useful from a programming perspective. The Common Objects folder of the Object Browser contains, along with the built-in data types, a set of common Transact-SQL functions grouped into categories. For each function, the Parameters folder contains a description of each parameter, including its name and data types. Figure 213 shows the LEFT function as it is displayed in the Object Browser window.
Figure 21-3. The Object Browser displays functions by categories in the Common Objects folder. Using the Object Browser, you can open a table or view, similar to the way you display rows by opening the Query Designer in the Enterprise Manager. Having done so, you can simply view the rows, or you can insert new rows and edit existing ones. You can also use the Object Browser when you’re creating Transact-SQL programs. You can add objects to the Query window using drag-and-drop, and you can create several different kinds of scripts automatically from the context menu.
Opening ObjectsWhen you right-click a table or view in the Object Browser and select Open, the Query Analyzer displays the rows in the table (or the results of the view’s SELECT statement) in the Open Table window.Open a table
The Query Analyzer displays a list of tables in the database.
The Query Analyzer displays the rows in the table in an Open Table window.
Open a view
The Query Analyzer displays a list of all views in the database.
The Query Analyzer displays the rows returned by the query in an Open Table window.
Adding Objects to the Editor PaneOne of the simplest and most convenient capabilities of the Object Browser is its support for drag-and-drop. Whenever you need to specify an object listed in the Object Browser, simply drag it into position in the Editor Pane of the Query window, and its name will be pasted into the editor window.
TIP: If you drag-and-drop one of the functions listed in the Common Objects folders, the Object Browser will paste the function’s name, but not its parameters. To paste the function’s complete syntax, use the Scripting command described in the next section. Add a database object
Clear Window button
The Query Analyzer empties the Editor Pane. SELECT *
The Query Analyzer pastes the table name into the statement.
Execute Query button
The Query Analyzer displays the results in the Grids Pane.
The Query Analyzer pastes all the column names into the statement.
Execute Query button
The Query Analyzer displays the results in the Grids pane.
Scripting ObjectsScripting is a more sophisticated version of the drag-and-drop functionality of the Object Browser. Scripting creates a complete Transact-SQL statement and is available from the context menus of most objects. Not all script types are applicable to all object types, and some types, such as function parameters or columns, have no scripting available at all.The available scripts and the objects to which they apply are shown in Table 21-2. We’ll be looking at most of these scripts in the next lesson about creating and maintaining database objects.
Table 21-2. Scripting Options. Scripts can be written to a new Query window, a script file, or the clipboard (where they can be pasted into an existing Query window). Some scripts, such as the function execution scripts, use replaceable parameters. The Query Analyzer provides a dialog box to make it easy to replace these parameters with the appropriate values. Script a SELECT statement
The Query Analyzer opens a new Query window with the SELECT statement.
TIP: The Object Browser creates the SELECT statement on a single line. You can reformat it to improve readability as shown in the illustration.
Execute Query button
The Query Analyzer displays the results in the Grids Pane.
Script a function
The Query Analyzer opens a new Query window with a SELECT statement, including the function.
The Query Analyzer opens the Replace Template Parameters dialog box.
The Query Analyzer replaces the parameters in the query.
Execute Query button
The Query Analyzer displays the results in the Grids Pane.
Last Updated: Friday, July 6, 2001 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||