BLOGS BY TOPIC
BLOGS BY AUTHOR
BLOGS BY YEAR
This blog teaches the absolute basics of writing queries in Microsoft SQL Server Management Studio. You'll learn how to use the SELECT and FROM keywords to create simple queries showing all of the records from a single table.
- Getting Started with SQL Queries
- Referring to Tables in the FROM Clause
- Adding Columns to the SELECT List
- Executing SQL Queries (this blog)
- Useful Hints for Writing Basic Queries
Posted by Andrew Gould on 23 July 2012
You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.
Executing SQL Queries
When you've finished writing a query you need to execute it in order to see the results.
Using the Correct Database
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.
Selecting the Query
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.
Executing a Query
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.
Changing How Results are Presented
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.
Saving and Opening Queries
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 final part of this blog series 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.