SQL Server Management Studio (SSMS) Tutorial-Microsoft Docs
SQL Server Management Studio (SSMS) Tutorial-Microsoft Docs
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.
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 to and query a SQL Server instance
by using SQL Server Management Studio
5/9/2018 • 5 minutes 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. The article demonstrates how to do the following:
Connect to a SQL Server instance
Create a database ("TutorialDB")
Create a table ("Customers") in your new database
Insert rows into your new table
Query the new table and view the results
Use the query window table to verify your connection properties
Change the server that your query window is connected to
Prerequisites
To complete this tutorial, you need SQL Server Management Studio and access to a SQL Server instance.
Install SQL Server Management Studio.
If you don't have access to a SQL Server instance, select your platform from the following links. If you choose SQL
Authentication, use your SQL Server login credentials.
Windows: Download SQL Server 2017 Developer Edition.
macOS: Download SQL Server 2017 on Docker.
For Authentication, select Windows Authentication. This article uses Windows Authentication,
but SQL Server login is also supported. If you select SQL Login, you will be prompted for a
username and password. For more information about authentication types, see Connect to server
(database engine).
You can also modify additional connection options by selecting Options. Examples of connection
options are the database you're connecting to, the connection timeout value, and the network
protocol. This article uses the default values for all the options.
3. After you've completed all the fields, select Connect.
Examples of successful connections
To verify that your SQL Server connection succeeded, expand and explore the objects within Object Explorer.
These objects will be different depending on the type of server you're connected to.
Connecting to an on-prem SQL server - in this case NODE5\SQL2016ST:
Create a database
Create a database named TutorialDB by doing the following:
1. Right-click your server instance in Object Explorer, and then select New Query:
2. Into the query window, paste the following T-SQL code snippet:
USE master
GO
IF NOT EXISTS (
SELECT name
FROM sys.databases
WHERE name = N'TutorialDB'
)
CREATE DATABASE [TutorialDB]
GO
2. Paste the following T-SQL code snippet into the query window, select it, and then select Execute (or select
F5 on your keyboard).
You can either replace the existing text in the query window or append it to the end. To execute everything in
the query window, select Execute. To execute a portion of the text, highlight that portion, and then select
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 is complete, the new Customers table is displayed in the list of tables in Object Explorer. If the table
is not displayed, right-click the TutorialDB > Tables node in Object Explorer, and then select Refresh.
The results of the query are displayed under the area where text was entered:
2. Modify the way results are presented by selecting one of the following options:
The middle button displays the results in Grid View, which is the default option.
The first button displays the results in Text View, as shown in the image in the next section.
The third button lets you save the results to a file whose extension is .rpt by default.
NOTE
This action changes only the server that the query window is connected to, not the server that Object Explorer is
connected to.
Tutorial: Script objects in SQL Server Management
Studio
5/9/2018 • 5 minutes to read • Edit Online
This tutorial teaches you to generate Transact-SQL (T-SQL ) scripts for various objects found within SQL Server
Management Studio (SSMS ). In this tutorial, you find examples of how to script the following objects:
Queries, when you perform actions within the GUI
Databases in two different ways (Script As and Generate Script)
Tables
Stored procedures
Extended events
To script any object in Object Explorer, right-click it and select the Script Object As option. This tutorial shows
you the process.
Prerequisites
To complete this tutorial, you need SQL Server Management Studio, access to a server that's running SQL Server,
and an AdventureWorks database.
Install SQL Server Management Studio.
Install SQL Server 2017 Developer Edition.
Download AdventureWorks2016 sample databases.
Instructions for restoring databases in SSMS are here: Restore a database.
7. Select Execute to execute the query to back up the database via T-SQL.
Script T -SQL when you shrink the transaction log
1. Right-click the database AdventureWorks2016 > Tasks > Shrink > Files:
2. Select Log from the File type drop-down list box:
4. Open a New Query window and paste. (Right-click in the window. Then select Paste.)
5. Select Execute to execute the query and shrink the transaction log.
Script databases
The following section teaches you to script out the database by using the Script As and Generate Scripts options.
The Script As option re-creates the database and its configuration options. You can script both the schema and the
data by using the Generate Scripts option. In this section, you create two new databases. You use the Script As
option to create AdventureWorks2016a. You use the Generate Scripts option to create AdventureWorks2016b.
Script a database by using the Script option
1. Connect to a server that's running SQL Server.
2. Expand the Databases node.
3. Right-click the database AdventureWorks2016 > Script Database As > Create To > New Query Editor
Window:
7. Select Execute to execute the query and create your new AdventureWorks2016a database.
Script a database by using the Generate Scripts option
1. Connect to a server that's running SQL Server.
2. Expand the Databases node.
3. Right-click AdventureWorks2016 > Tasks > Generate Scripts:
4. The Introduction page opens. Select Next to open the Chose Objects page. You can select the entire
database or specific objects in the database. Select Script entire database and all database objects.
5. Select Next to open the Set Scripting Options page. Here you can configure where to save the script and
some additional advanced options.
a. Select Save to new query window.
b. Select Advanced and make sure these options are set:
Script Statistics set to Script Statistics.
Types of data to script set to Schema only.
Script Indexes set to True.
NOTE
You can script the data for the database when you select Schema and data for the Types of data to script option.
However, this isn't ideal with large databases. It can take more memory than SSMS can allocate. This limitation is okay
for small databases. If you want to move data for a larger database, use the Import and Export Wizard.
10. Select Execute to execute the query and create your new AdventureWorks2016b database.
Script tables
This section covers how to script out tables from your database. Use this option to either create the table or drop
and create the table. You can also use this option to script the T-SQL associated with modifying the table. An
example is to insert into it or update to it. In this section, you drop a table and then re-create it.
1. Connect to a server that's running SQL Server.
2. Expand your Databases node.
3. Expand your AdventureWorks2016 database node.
4. Expand your Tables node.
5. Right-click dbo.ErrorLog > Script Table as > DROP And CREATE To > New Query Editor Window:
6. Select Execute to execute the query. This action drops the Errorlog table and re-creates it.
NOTE
The Errorlog table is empty by default in the AdventureWorks2016 database. So you're not losing any data by
dropping the table. However, following these steps on a table with data causes data loss.
Next steps
The next article introduces you to the prebuilt T-SQL templates found within SSMS.
Go to the next article to learn more:
Next steps
Tutorial: Using templates in SQL Server Management
Studio
5/22/2018 • 2 minutes to read • Edit Online
This tutorial introduces you to the prebuilt Transact-SQL (T-SQL ) templates that are available in SQL Server
Management Studio (SSMS ). In this article, you learn how to:
Use the template browser to generate T-SQL scripts
Edit an existing template
Locate 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.
4. Right-click the newly created Custom Templates folder, and then select New > Template. Enter a name for
your template:
5. Right-click the template you created, and then select Edit. The New Query Window opens.
6. Enter the T-SQL text that you want to save.
7. In the File menu, select Save.
8. Close the existing query window, and then open your new custom template.
Next steps
The next article provides additional tips and tricks for using SQL Server Management Studio.
Additional tips and tricks for using SSMS
Tutorial: SQL Server Management Studio
components and configuration
5/22/2018 • 3 minutes to read • Edit Online
This tutorial describes the various window components in SQL Server Management Studio (SSMS ), and some
basic configuration options for your workspace. In this article, you learn how to:
Identify the components that make up the SSMS environment
Change the environment layout, and reset it to the default
Maximize the query editor
Change the font
Configure startup options
Reset the configuration to the default
Prerequisites
To complete this tutorial, you need SQL Server Management Studio.
Install SQL Server Management Studio.
Query Window (Ctrl+N ): After you select New Query, enter your Transact-SQL (T-SQL ) queries in this
window. The results of your queries also appear here.
Properties (F4): You can see the Properties view when the Query Window is open. The view displays basic
properties of the query. For example, it shows the time that a query started, the number of rows returned,
and connection details.
Template Browser (Ctrl+Alt+T): Template Browser has various pre-built T-SQL templates. You can use
these templates to perform various functions, such as creating or backing up a database.
Object Explorer Details (F7): This view is more granular than the view in Object Explorer. You can use
Object Explorer Details to manipulate multiple objects at the same time. For example, in this window, you
can select multiple databases, and then either delete them or script them out simultaneously.
Change the environment layout
This section describes how to change the environment layout, such as how to move various windows.
To move a window, press and hold the title, and then drag the window.
To pin or unpin a window, select the pushpin icon in the title bar:
Each window component has a drop-down menu that you can use to manipulate the window in various
ways:
When two or more query windows are open, the windows can be tabbed vertically or horizontally so that
both query windows are visible. To view tabbed windows, right-click the title of the query, and then select the
tabbed option that you want:
Here's a Horizontal Tab Group:
To restore the default environment layout, in the Window menu, select Reset Window Layout:
This tutorial gives you some additional tricks for using SQL Server Management Studio (SSMS ). This article
shows 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 database. To learn how to restore a database in SSMS, see Restoring a
database.
USE master
GO
5. Highlight the Alter Database portion of the text, and then select the Comment button on the toolbar:
6. Select Execute to run the uncommented portion of the text.
7. Highlight everything except for the Alter Database command, and then select the Comment button:
8. Highlight the Alter Database portion of the text, and then select the Uncomment button to uncomment
it:
9. Select Execute to run the uncommented portion of the text.
USE master
GO
3. Highlight the Alter Database portion of the text, and then select the Increase Indent button on the
toolbar to move this text forward:
4. Highlight the Alter Database portion of the text again, and then select the Decrease Indent button to
move this text back.
Filter objects in Object Explorer
You can filter objects to make finding a specific object easier in databases that have many objects. This section
describes how to filter tables, but you can use the following steps in any other node in Object Explorer:
1. Connect to your SQL server.
2. Expand Databases > AdventureWorks > Tables. All the tables in the database appear.
3. Right-click Tables, and then select Filter > Filter Settings:
4. In the Filter Settings window, you can modify some of the following filter settings:
Filter by name:
Filter by schema:
5. To clear the filter, right-click Tables, and then select Remove Filter.
Access your SQL Server error log
The error log is a file that contains details about things that occur in your SQL Server instance. You can browse
and query the error log in SSMS. The error log is a .log file that's located on your disk.
Open the error log in SSMS
1. Connect to your SQL server.
2. Expand Management > SQL Server Logs.
3. Right-click the Current error log, and then select View SQL Server Log:
4. Modify the text in the single quotes to text you want to search for.
5. Execute the query, and then review the results:
Find the error log location if you're connected to SQL Server
1. Connect to your SQL server.
2. Open a New Query window.
3. Paste the following T-SQL code in your query window, and then select Execute:
4. The results show the location of the error log in the file system:
Find the error log location if you can't connect to SQL Server
1. Open SQL Server Configuration Manager.
2. Expand Services.
3. Right-click your SQL Server instance, and then select Properties:
The format of the name is HOSTNAME\INSTANCENAME. If you see only the host name, then you've
installed the default instance and your instance name is MSSQLSERVER. When you connect to a default
instance, the host name is all you need to enter to connect to your SQL server.
When you're connected to SQL Server
When you're connected to SQL Server, you can find the server name in three locations:
1. The name of the server is listed in Object Explorer:
select @@Servername
4. View the results of the query to identify the name of the SQL Server instance you're connected to: