After the Enterprise Manager, the Query Analyzer is probably the next most important tool that you will use with SQL Server 2012.
It gives you the ability to enter a SQL statement, execute it, and view the results.
You can then analyze these results to see if the query can be optimized. Queries that are issued "on-the-fly," (as you think of them) are known as ad hoc queries.
The following simulation illustrates a typical way in which you might use the Query Analyzer. Click the Start Simulation button to open the simulation in a new browser window; follow the instructions to work through it.
Note: If you have access to SQL Server 2012, you might want simply to click Instructions Only and use your own software to execute the procedures.
To invoke the Query Analyzer, click
Start>>Programs>>Microsoft SQL Server 2012>>Query Analyzer.
Using Query Analyzer
To use the Query Analyzer:
- Click Start>>Programs>>Microsoft SQL Server 2012 >>Query Analyzer.
- Click the down arrow to the right of the Database text box to display the drop-down menu.
- Select the master database. (Normally when you roll your mouse cursor over the various selections, they will be highlighted. In this case, simply click master.)
- Click in the upper-left corner of the Query Pane and type
select name from sysusers
. Then click the Execute Query button (green arrow button) to execute the query.
- View the results of your query in the Results grid.
Interacting Through the Query Window
This part of the Management Studio takes the place of what was, at one time, a separate tool that was called Query Analyzer.
The Query window is your tool for interactive sessions with a given SQL Server. It is where you can execute statements using Transact-SQL (T-SQL).
I pronounce it "Tee-Squeal" but it is supposed to be "Tee-Sequel."
T-SQL is the native language of SQL Server and is a dialect of Structured Query Language (SQL), and is largely compliant with modern ANSI/ISO SQL standards. You will that most RDBMS products support basic ANSI/ISO SQL compatibility.
Because the Query window is where you will spend a fair amount of time, let us take a more in-depth look at this tool and get familiar with how to use it.
Getting Started
Follow these steps:
- Open a new Query window by clicking the New Query button toward the top-left of the Management Studio or choosing
File -> New -> New Query With Current Connection from the File menu.
When the Query window opens, you will get context-sensitive menus and toolbar buttons designed for T-SQL.
Before you look at the specifics, let us get your very first query out of the way.
- Select AdventureWorks in the database dropdown box on the SQL Editor toolbar.
- Type the following code into the main window of the Query window: SELECT * FROM Person.Address;
In the next lesson, we will take a look at the wizards that come with Microsoft SQL Server 2012.