SQL Server Management Studio (SSMS) is
the main GUI tool used by most DBAs to administer their database
environments. Throughout the releases of SQL Server, this tool (known
as Enterprise Manager previous to SQL Server 2005) has provided
increasing amounts of functionality around database development.
Although SSMS is not a database development tool, it’s important to
know what kind of development-related tasks it can handle.
IntelliSense
One of the first features you notice when using
the Query Editor in SSMS is IntelliSense. IntelliSense makes it very
easy for the user to know what objects or statements are valid and
allows for easy auto-completion of statements. For example, we have a
copy of the AdventureWorks database installed on SQL Server, and
IntelliSense lets us navigate and explore that database, as we’ll show
next.
To use IntelliSense, simply
navigate to a database—in our example, AdventureWorks—right click the
node, and select, New Query. Begin typing the query shown in Figure 1. After you type the period in the statement “SELECT * FROM Sales.” a context menu will appear, as shown in Figure 1.
Figure 1. IntelliSense content menu in the SSMS Query Editor
Notice that the context menu lists the
applicable objects that you can use to finish this part of the query.
If you scroll down to CreditCard and press the Tab key, SSMS will
auto-complete the input of that object name in the Query Editor window.
IntelliSense is useful in many situations—when you can’t remember the
object name you are looking for or the acceptable parameters of a
stored procedure, to name a few.
Query Designer
In addition to IntelliSense, another richer
feature can help users write queries—the Query Designer. If you have
used Microsoft Access, you may recognize some similarities with the
Query Designer tool that is available in both SSMS and Visual Studio.
The Query Designer helps users build queries by allowing them to
graphically adding tables and views together and simply click the
desired columns. To launch the Query Designer, click the New Query
button to get a clean Query Editor window, and select Design Query in
Editor from the Query menu on the SSMS toolbar. This will launch the
Query Designer, as shown in Figure 2.
Figure 2. Query Designer showing the Add Table dialog
Let’s add both the SalesOrderDetail
and SalesOrderHeader
tables to the Query Designer. Click each table’s name, and click the
Add button. When you have finished adding the tables or views, click
the Close button. The Query Designer knows that these two tables share
a relationship and has visually marked this in the UI. From here, you
can go ahead and add the search criteria information to the query by
checking the desired columns and typing any “group by” or “sort”
information related to the desired query. For this example, we are
interested in the sum of orders for each day, so we have configured the
Query Designer as shown in Figure 3.
Figure 3. Sales order summary query configured in Query Designer
When you click the OK button, the Query Designer yields the following T-SQL text based on the settings in Figure 3:
SELECT SUM(Sales.SalesOrderDetail.OrderQty) AS TotalQuantity,
Sales.SalesOrderHeader.OrderDate
FROM Sales.SalesOrderDetail INNER JOIN
Sales.SalesOrderHeader ON Sales.SalesOrderDetail.SalesOrderID =
Sales.SalesOrderHeader.SalesOrderID
GROUP BY Sales.SalesOrderHeader.OrderDate
ORDER BY Sales.SalesOrderHeader.OrderDate
Even through the Query Designer brought us to
back to the Query Editor window, you can always go back to the designer
for help with the query by highlighting the query and selecting Design
Query in Editor from the context menu of the highlighted query.