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.
Let’s move on and take a look at some variations of the SELECT query. Then we’ll
see how easy it is to insert, modify, and delete items from the database using
other keywords.
Selecting Certain Fields
If you didn’t want to select all the fields from the database table, you’d include
the names of the specific fields that you wanted in place of the * in your query.
For example, if you’re interested only in the department names—not their
IDs—you 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, it’s 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, it’s 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 you’re writing
now.
It’s 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