Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
551 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Written by Andrew Gould
In this tutorial
When you've finished writing a query you need to execute it in order to see the results.
When you execute a query you should make sure that you will be doing so on the correct database. You can do this by selecting the name of the database you want to use from the drop down list at the top of the screen:
Use this drop down list to select the database on which you want to execute the query.
To avoid having to select the database from a drop down list you can write a USE statement at the top of your query:
Write the keyword USE followed by the name of the database you want to use.
The GO keyword executes a batch of SQL statements. Certain SQL statements must be the first statement in a batch and so the GO command is used to start a new batch. In the above example the GO keyword is optional.
Before you execute the query you should make sure that you've selected the relevant part of your code; Management Studio will execute whichever bit of text you have selected. When you have only written a single query on a page you can click anywhere on the page to select it:
Click anywhere on the page to select this single query.
When you've written multiple queries on a page and you only want to run one of them you must make sure you've selected the entire query that you want to execute:
This page contains three separate SELECT statements. We've selected the middle one of the three to ensure that only this one will run when we choose to execute it.
When you've made sure you're using the correct database and you have the appropriate text selected on the page, click the Execute button at the top of the screen to run your query:
You could also press F5 on the keyboard to run a query.
By default you should see the results of your query in a grid at the bottom of the screen:
The Results grid appears at the bottom of the screen.
You can close the Results panel by pressing CTRL + R on the keyboard.
You can change the output style of a query to be presented as text or even exported to a file. Use the toolbar at the top of the screen to choose the output style of a query:
The three buttons shown here in the red box allow you to change the output style of a query.
Once you've selected one of the three buttons you'll need to execute the query again to see the results. If you choose Results to Text you'll see the output in the Messages panel at the bottom of the screen:
This option isn't much different from the original Results to Grid option.
If you choose Results to File you can choose to save the results when you execute the query:
The default file type is a Crystal Reports report, but you can change this to All Files (*.*) using the drop down list at the bottom of the dialog box and add a .txt extension to the filename.
You can open the output file using Notepad or any other text editor on your computer:
Open the file using a text editor to see the results.
Queries like the one we've written here aren't saved as part of the database so if you want to be able to reuse it you need to save it. To do this, from the menu choose: File > Save, or press CTRL + S on the keyboard:
Choose a location and enter a file name, then click Save.
When you want to reuse a query you need to open the file. To do this, from the menu choose: File > Open > File... or press CTRL + O on the keyboard.
Browse to the location of the saved file and double-click on it or select it and click Open.
The next part of this tutoiral section describes a few neat extra things you can do when writing simple queries, including how to select all of the columns in a table without having to list their names.
You can learn more about this topic on the following Wise Owl courses:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.