You are on page 1of 42

Table of Contents

Tutorials: SQL Server Management Studio


Connect & Query SQL Server using SSMS
Scripting Objects in SSMS
Using Templates in SSMS
SSMS Configuration
Tips and Tricks for using SSMS
Tutorials for SQL Server Management Studio (SSMS)
3/21/2018 • 1 min to read • Edit Online

THIS TOPIC APPLIES TO: SQL Server Azure SQL Database Azure SQL Data Warehouse Parallel Data
Warehouse
The SQL Server Management Studio (SSMS) tutorial introduces you to the integrated environment for managing
your SQL Server infrastructure. SQL Server Management Studio presents a graphical interface for configuring,
monitoring, and administering instances of SQL Server. It also allows you to deploy, monitor, and upgrade the
data-tier components used by your applications, such as databases. SQL Server Management Studio also provides
Transact-SQL, MDX, DMX, and XML language editors for editing and debugging scripts.

What You Will Learn


These tutorials will help you understand the presentation of information in SSMS and how to take advantage of its
features.
The best way to get acquainted with SSMS is through hands-on practice. These tutorials will familiarize you with
the various features avaialable within SSMS. These tutorials will teach you how to manage the components of
SSMS and how to find the features that you use regularly.
Here is what the tutorials will cover:
Tutorial: Connect & Query SQL Server using SSMS
In this section, you will learn how to connect to your SQL Server instance. You will also learn some basic
Transact-SQL (T-SQL) commands to create and then query a new database.
Tutorial: Scripting Objects in SSMS
In this section, you will learn how to script out various objects in SSMS, including databases and queries.
Tutorial: Using Templates in SSMS
In this section, you will learn how to work with the pre-built Templates within SSMS.
Tutorial: SSMS Configuration
In this section, you will learn the basics of configuring your SSMS environment.
Tutorial: Additional Tips and Tricks for using SSMS
In this section, you will learn additional tips and tricks for using SSMS. The tutorial includes the following:
Commenting and uncommenting text
Indenting text
Filtering Objects in Object Explorer
Accessing your SQL Server error log
Finding the name of your instance

Requirements
This tutorial is intended for experienced database administrators and database developers who are not familiar
with Visual Studio, but who are familiar with database concepts and Transact-SQL.
You must have the following installed to use this tutorial:
Install the latest version of SQL Server Management Studio (SSMS).
The first section walks you through creating a database but other sample databases can be found here:
AdventureWorks Sample Databases. Instructions for restoring databases in SSMS can be found here: Restoring a
Database.

See Also
Database Engine Tutorials
Tutorial: Connect and Query SQL Server using SQL
Server Management Studio
3/29/2018 • 5 min to read • Edit Online

This Tutorial teaches you how to use SQL Server Management Studio (SSMS) to connect to your SQL Server
instance, and run some basic Transact-SQL (T-SQL) commands. This article demonstrates how to do the following:
Connect to a SQL Server
Create a new database (TutorialDB)
Create a table (Customers) in your new database
Insert rows into your new Customers table
Query the Customers table and view the results
Use the query window table to verify your connection properties
Change which server your query window is connected to

Prerequisites
To complete this Tutorial, you need SQL Server Management Studio and access to a SQL Server.
Install SQL Server Management Studio.
If you don't have access to a SQL Server, select your platform from the following links (make sure you remember
your SQL Login and Password if you choose SQL Authentication!):
Windows - Download SQL Server 2017 Developer Edition
macOS - Download SQL Server 2017 on Docker

Connect to a SQL Server


1. Start SQL Server Management Studio (SSMS).
2. The first time you run SSMS the Connect to Server dialog box opens.
If the Connect to Server dialog doesn't open, it can be opened manually in Object Explorer >
Connect (or icon next to it) > Database Engine.

a. In the Connect to Server dialog box, fill out your connection options:
Server type: Database Engine (typically selected by default).
Server Name: This article uses the instance name SQL2016ST on the hostname NODE5
(NODE5\SQL2016ST), but you'll need to type in your own server name here. If you're not sure
how to determine your SQL Server name, you can find more information here.
Authentication: Windows Authentication (this article uses Windows Authentication, but SQL
Login is supported and will prompt you for a username and password if selected). More
information on authentication types can be found here.

You can also modify additional connection options (such as the database you're connecting to, the
connection timeout value, and the network protocol) by clicking the Options button. For the purpose
of this article, everything was left at the default values.
3. Once the fields have been filled out, click on Connect.
4. You can verify that your connection succeeded to your SQL Server by exploring the objects in Object
Explorer:

Create a database
The following steps create a database named TutorialDB.
1. Right-click your server in Object Explorer and select New Query:

2. Paste the following T-SQL code snippet into the query window:

USE master
GO
IF NOT EXISTS (
SELECT name
FROM sys.databases
WHERE name = N'TutorialDB'
)
CREATE DATABASE [TutorialDB]
GO

3. To execute the query, click on Execute (or press F5 on your keyboard).


After the query completes, the new TutorialDB appears in the list of databases within Object Explorer. If you
don’t see it, right-click the Databases node and select Refresh.

Create a Table
The following steps will now create a table in the newly created TutorialDB database. However, the query editor is
still in the context of the master database, and you want to create a table in the TutorialDB database.
1. Change the connection context of your query from the master database to TutorialDB by selecting the
database you want from the database drop-down.

2. Paste the following T-SQL code snippet into the query window, highlight it, and click Execute (or press F5
on your keyboard):
You can either replace the existing text in the query window or append it to the end. If you want to
execute everything in the query window, click Execute. If you want to execute a portion of the text,
highlight that portion, and then click Execute.

-- Create a new table called 'Customers' in schema 'dbo'


-- Drop the table if it already exists
IF OBJECT_ID('dbo.Customers', 'U') IS NOT NULL
DROP TABLE dbo.Customers
GO
-- Create the table in the specified schema
CREATE TABLE dbo.Customers
(
CustomerId INT NOT NULL PRIMARY KEY, -- primary key column
Name [NVARCHAR](50) NOT NULL,
Location [NVARCHAR](50) NOT NULL,
Email [NVARCHAR](50) NOT NULL
);
GO

After the query completes, the new Customers table appears in the list of tables in Object Explorer. If the
table is not visible, right-click the TutorialDB > Tables node in Object Explorer and select Refresh.

Insert rows
The following step will insert some rows into the Customers table that was previously created.
Paste the following T-SQL code snippet into the query window and click Execute:
-- Insert rows into table 'Customers'
INSERT INTO dbo.Customers
([CustomerId],[Name],[Location],[Email])
VALUES
( 1, N'Orlando', N'Australia', N''),
( 2, N'Keith', N'India', N'keith0@adventure-works.com'),
( 3, N'Donna', N'Germany', N'donna0@adventure-works.com'),
( 4, N'Janet', N'United States', N'janet1@adventure-works.com')
GO

View Query Results


The results of a query are visible underneath the query text window. The below steps will allow you to query the
Customers table and view the rows that were previously inserted.
1. Paste the following T-SQL code snippet into the query window and click Execute:

-- Select rows from table 'Customers'


SELECT * FROM dbo.Customers;

2. The results of the query are displayed under the area where text was entered:

3. You can modify the way results are presented by selecting one of these options:
By default, the results will be in Grid View, which is the middle button and shows the results in a table.
The first button will display your results in Text View, as shown in the image in the next section.
The third button will allow you to save your results to a file, a file ending in *.rpt by default.

Verify your query window connection properties


You can find information about the connection properties under the results of your query.
After running the aforementioned query from the previous step, review the connection properties at the
bottom of the query window.
You can determine which server and database you're connected to, and the user you're logged in with.
You can also see the query duration and the number of rows returned by the query executed earlier.

In this image, the results are displayed in Text View.

Change server connection within Query Window


You can change which server your current query window is connected to by following these steps.
1. Right click within the query window > Connection > Change connection.
2. This will open the Connect to Server dialog box again, allowing you to change which server your query is
connected to.
Note that this does not change which server your Object Explorer is connected to, just the current query
window.
Tutorial: Script Objects in SQL Server Management
Studio
3/21/2018 • 3 min to read • Edit Online

This tutorial will teach you how to generate Transact-SQL (T-SQL) scripts for various objects found within SQL
Server Management Studio. In this tutorial, you will find examples of how to script the following objects:
Queries when performing actions within the GUI
Databases in two different ways ("Script As" and "Generate Script")
Tables
Stored procedures
Extended events
The summary of this tutorial is that any object in Object Explorer can be scripted by right-clicking on it and
selecting the Script Object As option.

Prerequisites
To complete this Tutorial, you need SQL Server Management Studio, access to a SQL Server, and an
AdventureWorks database.
Install SQL Server Management Studio.
Install SQL Server 2017 Developer Edition.
Download an AdventureWorks Sample Databases. Instructions for restoring databases in SSMS can be found
here: Restoring a Database.

Script Queries from GUI


Any time you perform a task using the GUI in SSMS, you can also generate the T-SQL code associated with that
task. The following examples show how to do so when taking a backup of a database, and when you shrink the
transaction log. These same steps can be applied to any action that's completed via the GUI.
Script T -SQL when backing up a database
1. Connect to your SQL Server.
2. Expand the Databases node.
3. Right-click the database > Tasks > Back up:
4. Configure the backup the way you want. For the purpose of this Tutorial, everything was left at default.
However, any changes made in the window will also be reflected in the script.
5. Click the option to Script > Script Action to Query Window:

6. Review the T-SQL populated in the query window:

Script T -SQL when shrinking the transaction log


1. Right-click the database > Tasks > Shrink > Files:
2. Select Log from the File Type drop down:

3. Click the option Script and Script Action to Clipboard:

4. Open a New Query window and paste (Right click in the window > Paste):
Script Databases
The following section teaches you how to script out the database, both using the Script As option and the
Generate Scripts option. The Script As option will recreate the database and the configuration options for it. The
Generate Scripts option will script out all of the objects in the database, but not the data. To script the data as well,
you will need to you use the Import and Export Wizard.
Script database using Script option
1. Connect to your SQL Server.
2. Expand the Databases node.
3. Right-click the database > Script Database As:

4. Review the database creation query in the window:


This option will only script out the database configuration options.
Script database using Generate Scripts option
1. Connect to your SQL Server.
2. Expand the Databases node.
3. Right-click the database > Tasks > Generate Scripts:

4. Select Next and you'll see that you can choose to script out the entire database, or specific objects within the
database:
5. Select Next. This screen is where you can configure where the script will be saved.
You can also configure advanced options by selecting Advanced:

6. Once you're ready to proceed, keep hitting Next until the scripts are generated and you get to the Finish.
Your database script will be located where it was saved in Step 5.
This will script out the schema and various objects within the database, but not the data.
Script Tables
This section covers how to script out tables from your database.
1. Connect to your SQL Server.
2. Expand your Databases node.
3. Expand your AdventureWorks database node.
4. Expand your Tables node.
5. Right-click the table you want to script out > Script Table as:
From here, there are various options such as creating the table, or inserting data into it:

Script Stored Procedures


This section covers how to script out stored procedures.
1. Connect to your SQL Server.
2. Expand your Databases node.
3. Expand your Programmability node.
4. Expand your Stored Procedure node.
5. Right-click the stored procedure you're interested in > Script Stored Procedure As:
Script Extended Events
This section covers how to script out extended events.
1. Connect to your SQL Server.
2. Expand your Management node.
3. Expand your Extended Events node.
4. Expand your Sessions node.
5. Right-click the extended session you're interested in > Script Session As:
Next steps
The next article will introduce you to the pre-built templates found within SSMS.
Advance to the next article to learn more
Next steps button
Tutorial: Using Templates within SQL Server
Management Studio
3/21/2018 • 2 min to read • Edit Online

This tutorial will introduce you to the pre-built Transact-SQL (T-SQL) templates that are available within SQL Server
Management Studio (SSMS). In this article, you will learn how to:
Use the Template Browser to generate T-SQL Scripts
Edit an existing Template
Locate the templates on disk
Create a new Template

Prerequisites
To complete this Tutorial, you need SQL Server Management Studio, and access to a SQL Server.
Install SQL Server Management Studio.
Install SQL Server 2017 Developer Edition.

Using the Template Browser


In this section, you will learn how to locate and use the Template Browser.
1. Start your SQL Server Management Studio.
2. From the View Menu > Template Browser (Ctrl + Alt + T):
You can also see recently used templates at the bottom of the Template Browser.
3. Expand the node you're interested in > Right click the Template > Open:

Double-clicking the template will have the same effect.


4. This will launch a new query window with the T-SQL already populated.
5. Modify the template to suit your needs and then Execute the query:

Edit an Existing Template


You can also edit the existing templates within Template Browser.
1. Locate the template of interest in the Template Browser.
2. Right-click the template > Edit:
3. Make the desired changes in the query window that opens.
4. Save the template by going to File > Save (Ctrl + S).
5. Close the query window.
6. Reopen the Template you saved > your new edits should be there.

Locate the Templates on Disk


Once a template is open, you can then locate it on disk.
1. Select a template in Template Browser > Edit.
2. Right-click the Query Title > Open Containing Folder. This should open your explorer to where the
templates are stored on disk:

Create a New Template


Within the Template Browser you also have the ability to create new templates. These steps will teach you to
create a new folder, and then create a new template within that folder. However, with these steps, you can also
create a custom template within the existing folders.
1. Open Template Browser.
2. Right-click SQL Server Templates > New > Folder.
3. Name this folder Custom Templates:

4. Right-click the newly created Custom Templates folder > New > Template > name your template:

5. Right-click the template you just created > Edit. This will open a New Query Window.
6. Type in the T-SQL text you want to save.
7. Save the file by going to the File menu > Save.
8. Close the existing Query Window and open your new custom template.

Next steps
The next article will provide some additional tips and tricks for using SQL Server Management Studio.
Advance to the next article to learn more
Next steps button
Tutorial: SQL Server Management Studio
Components and Configuration
3/21/2018 • 3 min to read • Edit Online

This Tutorial describes the different window components within SQL Server Management Studio (SSMS) and some
basic configuration options for your workspace. In this article, you will learn how about:
The different components that make up the SSMS environment
Changing the environmental layout and resetting it to default
Maximizing the query editor
Changing the font
Configuring startup options
Resetting the configuration back to default

Prerequisites
To complete this Tutorial, you need SQL Server Management Studio.
Install SQL Server Management Studio.

SQL Server Management Studio Components


This section covers the different window components available in the workspace, and their purpose.
Every window component can be closed by hitting the X in the corner of the title bar and then reopened
from the View dropdown in the main menu.
Object Explorer (F8): Object Explorer is a tree view of all the database objects in a server. This can include
the databases of the SQL Server Database Engine, Analysis Services, Reporting Services, and Integration
Services. Object Explorer includes information for all servers to which it is connected.

Query Window (Ctrl+N): Once you've clicked on New Query, this is the window where you will type in
your Transact-SQL (T-SQL) queries. Results of your queries are visible here as well.

Properties (F4): This is visible once the Query Window is open and displays basic properties of the query.
For example, it will show the time a query started, the number of rows returned, and connection details.
Template Browser (Ctrl+Alt+T): There are a number of pre-built T-SQL Templates that can be found in the
template browser. These templates allow you to perform various functions such as creating or backing up a
database.

Object Explorer Details(F7): This is a more granular view of what's visible in the Object Explorer, and
allows you to manipulate multiple objects at once. For example, the Object Explorer Details window allows
you to select multiple databases simultaneously and either delete them or script them out.
Change the Environmental Layout
This section discusses manipulating the environmental layout, such as moving the various windows around.
Each window component can be moved around by holding down the title and dragging the window around.
Each window component can be pinned and unpinned by selecting the pushpin icon in the title bar:

Each window component has a drop-down arrow that allows for the window to be manipulated in various
ways:

Once you have two or more query windows open, they can be tabbed vertically or horizontally so that both
query windows are visible at once. To achieve this, right-click the title of the query and select the desired
tabbed option.
This is the Horizontal Tab Group:

This is the Vertical Tab Group:


To merge the tabs back again, right-click the query title again and Move to Next Tab Group or
Move to Previous Tab Group:

To restore the default environmental layout, click on the Window Menu > Reset Window Layout:

Maximize Query Editor


The query editor can be maximized to full screen mode.
1. Click anywhere within the Query Editor Window.
2. Press SHIFT + ALT + ENTER to toggle between full-screen mode and regular mode.
This keyboard shortcut works with any document window.
Change Basic Settings
This section discusses how to modify some basic settings within SSMS. These options are found within the Tools
menu option:

The highlighted toolbar can be modified by going to the menu: Tools > Customize:

Change the font


The font can be changed from the menu: Tools > Options > Fonts and Colors:
Change the Startup Options
The startup options determine what your workspace looks like when you first launch SSMS. These can be
configured from the menu: Tools > Options > Startup:

Reset Settings to Default


All of these settings can be exported and imported from the menu: Tools > Import and Export Settings
This is also where you can reset all of your settings to default.

Next steps
The next article will teach you some additional tips and tricks for using SSMS, such as finding your SQL Server error
log and your SQL instance name.
Advance to the next article to learn more
Next steps button
Tutorial: Additional Tips and Tricks for using SSMS
3/29/2018 • 5 min to read • Edit Online

This tutorial will provide you with some additional tricks for using SQL Server Management Studio. This article will
teach you how to:
Comment / Uncomment your Transact-SQL (T-SQL) text
Indent your text
Filter Objects in Object Explorer
Access your SQL Server Error log
Find the name of your SQL Server Instance

Prerequisites
To complete this Tutorial, you need SQL Server Management Studio, access to a SQL Server, and an
AdventureWorks database.
Install SQL Server Management Studio.
Install SQL Server 2017 Developer Edition.
Download an AdventureWorks Sample Databases. Instructions for restoring databases in SSMS can be found
here: Restoring a Database.

Comment / Uncomment your T-SQL Code


Portions of your text can be commented and uncommented by using the comment button in the toolbar. Text that
is commented out will not be executed.
1. Open SQL Server Management Studio.
2. Connect to your SQL Server.
3. Open a New Query window.
4. Paste the following T-SQL code snippet into your text window:

USE master
GO

-- Drop the database if it already exists


IF EXISTS (
SELECT name
FROM sys.databases
WHERE name = N'TutorialDB'
)

DROP DATABASE TutorialDB


GO

CREATE DATABASE TutorialDB


GO

ALTER DATABASE [TutorialDB] SET QUERY_STORE=ON


GO

5. Highlight the Alter Database portion of the text and click Comment in the toolbar:
6. Click Execute to run the uncommented portion of the text.
7. Highlight everything other than the Alter Database command and click Comment in the toolbar:

8. Highlight the Alter Database portion and click Uncomment to uncomment it:
9. Click Execute to run the uncommented portion of the text.

Indent your Text


The indentation buttons allow you to increase and decrease the indent of your text.
1. Open a New Query window.
2. Paste the following T-SQL code snippet into your text window:

USE master
GO

-- Drop the database if it already exists


IF EXISTS (
SELECT name
FROM sys.databases
WHERE name = N'TutorialDB'
)

DROP DATABASE TutorialDB


GO

CREATE DATABASE TutorialDB


GO

ALTER DATABASE [TutorialDB] SET QUERY_STORE=ON


GO

3. Highlight the Alter Database portion of the text and press Increase Indent in the toolbar to move this text
forward:

4. Highlight the Alter Database portion of the text again and this time click Decrease Indent to move this
text back.
Filter Objects in Object Explorer
When a database has many objects, finding a specific object can prove difficult. To make this easier, you have the
ability to filter objects. This section explains how to filter tables, but the same steps can be applied to any other
node within Object Explorer
1. Connect to your SQL Server.
2. Expand your Databases node.
3. Expand your AdventureWorks database node.
4. Expand your Tables node.
You'll notice that you can see all the tables that are present in the database.
5. Right Click the Tables node > Filter > Filter Settings:

6. In the Filter Settings window, you can modify filter settings. A few examples:
Filter by name:
Filter by schema:

7. To clear the filter, right-click Tables > Remove Filter


Access your SQL Server Error log
The error log is a file that contains details about things occurring within your SQL Server. It can be browsed and
queried within SSMS. It can also be found as a .log file on disk.
Open Error log within SSMS
1. Connect to your SQL Server.
2. Expand the Management node.
3. Expand the SQL Server Logs node.
4. Right-click the Current error log > View SQL Server Log:

Query error log within SSMS


1. Connect to your SQL Server.
2. Open a New Query window.
3. Paste the following T-SQL code snippet into your query Window:

sp_readerrorlog 0,1,'Server process ID'

4. Modify the text in the single quotes to text of interest.


5. Execute the query and review the results:
Find error log location if you're connected to SQL
1. Connect to your SQL Server.
2. Open a New Query window.
3. Paste the following T-SQL code snippet into your query window and click Execute:

SELECT SERVERPROPERTY('ErrorLogFileName') AS 'Error log file location'

4. The results show you the location of the error log within the file system:

Find error log location if you cannot connect to SQL


1. Open your SQL Server Configuration Manager.
2. Expand the Services node.
3. Right click on your SQL Server instance > Properties:

4. Select the Startup Parameters tab.


5. In the Existing Parameters area, the path after the "-e" is the location of the error log:
You'll notice that there are several errorlog.* in this location. The one ending with *.log is the current one.
The ones ending with numbers are previous logs, as a new log is created every time the SQL Server
restarts.
6. Open this file in Notepad.

Determine SQL Server Name...


There are different ways to determine the name of your SQL Server before and after you connect to your SQL
Server.
...When you don't know it
1. Follow the steps to locate the SQL Server Error log on disk.
2. Open the errorlog.log in Notepad.
3. Navigate through it until you find the text "Server name is":
Whatever is listed in the single quotes is the name of the SQL Server and what you'll be connecting to:

The format of the name is 'HOSTNAME\INSTANCENAME'. If all you see is the hostname, then you've
installed the default instance, and your instance name is 'MSSQLSERVER'. When connecting to a default
instance, the hostname is all you need to type in to connect to your SQL Server.
...Once you're connected to SQL
There are three places to find which SQL Server you're connected to.
1. The name of the server will be listed in Object Explorer:

2. The name of the server will be listed in the query window:

3. The name of the server will also be listed in the Properties window.
To access this open the View Menu > Properties Window:
...If you're connected to an Alias or Availability Group Listener
When you're connected to an alias or an Availability Group listener, then that's what will show up Object Explorer
and Properties. In this case, the SQL Server name may not be readily apparent, and must be queried.
1. Connect to SQL Server.
2. Open a New Query window.
3. Paste the following T-SQL Code snippet into the window:

select @@Servername

4. View the results of the query to identify the name of the SQL Server you're connected to:

You might also like