Viewing Results in Text Format
By default, the query editor of SQL Server Management Studio displays the
results in a grid like the one shown in Figure 8.3. As you work with SQL
Server, you may start to find this view a little impractical; in particular, it
makes viewing longer strings of text painful because each time you run the
query, you need to resize the columns in the grid. Personally, I prefer the
plain text view, which is shown in Figure 8.4. You can enable this mode by
selecting Query > Results To > Results To Text.
Lets move on and take a look at some variations of the SELECT query. Then well
see how easy it is to insert, modify, and delete items from the database using
other keywords.
Selecting Certain Fields
If you didnt want to select all the fields from the database table, youd include
the names of the specific fields that you wanted in place of the * in your query.
For example, if youre interested only in the department namesnot their
IDsyou could execute the following:
SELECT Department
FROM Departments
This statement would retrieve data from the Department field only. Rather than
specifying the *, which would return all the fields within the database table, we
specify only the fields that we need.
Selecting All Columns Using *
To improve performance in real-world development scenarios, its better to
ask only for the columns that are of interest, rather than using *. Moreover,
even when you need all the columns in a table, its better to specify them by
name, to safeguard against the possibility that future changes, which cause
more columns to be added to the table, affecting the queries youre writing
now.
Its important to note that the order of the fields in a table determines the order
in which the data will be retrieved. Take this query, for example:
SELECT DepartmentID, Department
FROM Departments
You could reverse the order in which the columns are returned with this query:
299
Selecting Certain Fields

Get Build Your Own ASP.NET 2.0 Web Site Using C# & VB, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.