| Title | Tutorial: Additional tips and tricks for using SQL Server Management Studio | ||||||||
|---|---|---|---|---|---|---|---|---|---|
| description | A tutorial that covers some additional tips and tricks for using SSMS. | ||||||||
| keywords | SQL Server, SSMS, SQL Server Management Studio | ||||||||
| author | MashaMSFT | ||||||||
| ms.author | mathoma | ||||||||
| ms.date | 03/13/2018 | ||||||||
| ms.topic | tutorial | ||||||||
| ms.prod | sql | ||||||||
| ms.technology | ssms | ||||||||
| ms.prod_service | sql-tools | ||||||||
| ms.reviewer | sstein | ||||||||
| manager | craigg | ||||||||
| helpviewer_keywords |
|
This tutorial gives you some additional tricks for using SQL Server Management Studio (SSMS). This article shows you how to:
[!div class="checklist"]
- 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
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.
You can comment and uncomment portions of your text by using the Comment button on the toolbar. Text that is commented out is not executed.
- Open SQL Server Management Studio.
- Connect to your SQL server.
- Open a New Query window.
- Paste the following T-SQL code in 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-
Highlight the Alter Database portion of the text, and then select the Comment button on the toolbar:
-
Select Execute to run the uncommented portion of the text.
-
Highlight everything except for the Alter Database command, and then select the Comment button:
[!NOTE] The keyboard shortcut to comment text is CTRL + K, CTRL + C.
-
Highlight the Alter Database portion of the text, and then select the Uncomment button to uncomment it:
[!NOTE] The keyboard shortcut to uncomment text is CTRL + K, CTRL + U.
-
Select Execute to run the uncommented portion of the text.
You can use the indentation buttons on the toolbar to increase or decrease the indent of your text.
- Open a New Query window.
- Paste the following T-SQL code in 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-
Highlight the Alter Database portion of the text, and then select the Increase Indent button on the toolbar to move this text forward:
-
Highlight the Alter Database portion of the text again, and then select the Decrease Indent button to move this text back.
In databases that have many objects, you can use filtering to search for specific tables, views, etc. This section describes how to filter tables, but you can use the following steps in any other node in Object Explorer:
-
Connect to your SQL server.
-
Expand Databases > AdventureWorks > Tables. All the tables in the database appear.
-
Right-click Tables, and then select Filter > Filter Settings:
-
In the Filter Settings window, you can modify some of the following filter settings:
-
To clear the filter, right-click Tables, and then select Remove Filter.
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.
-
Connect to your SQL server.
-
Expand Management > SQL Server Logs.
-
Right-click the Current error log, and then select View SQL Server Log:
- Connect to your SQL server.
- Open a New Query window.
- Paste the following T-SQL code in your query window:
sp_readerrorlog 0,1,'Server process ID' -
Modify the text in the single quotes to text you want to search for.
-
Execute the query, and then review the results:
- Connect to your SQL server.
- Open a New Query window.
- Paste the following T-SQL code in your query window, and then select Execute:
SELECT SERVERPROPERTY('ErrorLogFileName') AS 'Error log file location' The path for your SQL Server errog log can vary depending on your configuration settings. The path for the error log location can be found in the startup parameters within the SQL Server Configuration Manager. Follow the steps below to locate the relevant startup parameter identifying the location of your SQL Server error log. Your path may vary from the path indicated below.
-
Open SQL Server Configuration Manager.
-
Expand Services.
-
Right-click your SQL Server instance, and then select Properties:
-
Select the Startup Parameters tab.
-
In the Existing Parameters area, the path after "-e" is the location of the error log:
There are several errorlog.* files in this location. The file name that ends with *.log is the current error log file. File names that end with numbers are previous log files. A new log is created every time the SQL server restarts.
-
Open the errorlog.log file in Notepad.
You have a few options for finding the name of your SQL server before and after you connect to SQL Server.
-
Follow the steps to locate the SQL Server error log on disk. Your path may vary from the path in the image below.
-
Open the errorlog.log file in Notepad.
-
Search for the text Server name is.
Whatever is listed in the single quotes is the name of the SQL Server instance that you'll be connecting to:
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, you can find the server name in three locations:
-
The name of the server is listed in Object Explorer:
-
The name of the server is listed in the Query window:
-
The name of the server is listed in Properties.
If you're connected to an alias or to an Availability Group listener, that information appears in Object Explorer and Properties. In this case, the SQL Server name might not be readily apparent, and must be queried:
- Connect to your SQL server.
- Open a New Query window.
- Paste the following T-SQL code in the window:
select @@Servername 
















