Connect and query SQL Server using SSMS - SQL Server Management Studio (SSMS) (2024)

  • Article

Applies to: Connect and query SQL Server using SSMS - SQL Server Management Studio (SSMS) (1) SQL Server

Get started using SQL Server Management Studio (SSMS) to connect to your SQL Server instance and run some Transact-SQL (T-SQL) commands.

Note

While Microsoft Entra ID is the new name for Azure Active Directory (Azure AD), to prevent disrupting existing environments, Azure AD still remains in some hardcoded elements such as UI fields, connection providers, error codes, and cmdlets. In this article, the two names are interchangeable.

The article demonstrates how to follow the below steps:

  • Connect to a SQL Server instance
  • Create a database
  • Create a table 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

This article covers connecting and querying an instance of SQL Server. For Azure SQL, see .

To use Azure Data Studio, see connect and query SQL Server, Azure SQL Database, and Azure Synapse Analytics.

To learn more about SQL Server Management Studio, see Tips and tricks for using SQL Server Management Studio (SSMS).

Prerequisites

To complete this quickstart, you need the following prerequisites:

  • Install Download SQL Server Management Studio (SSMS).
  • Install SQL Server from the Installation Wizard (Setup) and configure a SQL Server instance.

Connect to a SQL Server instance

To connect to your SQL Server instance, follow these steps:

  1. Start SQL Server Management Studio. The first time you run SSMS, the Connect to Server window opens. If it doesn't open, you can open it manually by selecting Object Explorer > Connect > Database Engine.

    Connect and query SQL Server using SSMS - SQL Server Management Studio (SSMS) (2)

  2. The Connect to Server dialog box appears. Enter the following information:

    SettingSuggested valuesDescription
    Server typeDatabase EngineFor Server type, select Database Engine (usually the default option).
    Server nameThe fully qualified server nameFor Server name, enter the name of your SQL Server (you can also use localhost as the server name if you're connecting locally). If you're NOT using the default instance - MSSQLSERVER - you must enter in the server name and the instance name.

    If you're unsure how to determine your SQL Server instance name, see Additional tips and tricks for using SSMS.

    AuthenticationWindows Authentication

    SQL Server Authentication

    Microsoft Entra authentication

    Windows Authentication is set as default.
    You can also use SQL Server Authentication to connect. However, if you select SQL Server Authentication, a username and password are required.
    Microsoft Entra authentication is available for SQL Server 2022 (16.x) and later versions. For step-by-step configuration instructions, see Tutorial: Set up Microsoft Entra authentication for SQL Server
    For more information about authentication types, see Connect to the server (database engine).
    LoginServer account user IDThe user ID from the server account used to sign in to the server. A login is required when using SQL Server Authentication.
    PasswordServer account passwordThe password from the server account used to sign in to the server. A password is required when using SQL Server Authentication.
    Encryption 1Encryption methodSelect the encryption level for the connection. The default value is Mandatory.
    Trust server certificateTrust Server CertificateCheck this option to bypass server certificate validation. The default value is False (unchecked), which promotes better security using trusted certificates.
    Host Name in CertificateHost name of the serverThe value provided in this option is used to specify a different, but expected, CN or SAN in the server certificate.

    1 The default value is Mandatory in SSMS 20. Strict (SQL Server 2022 and Azure SQL) encryption should be used for Azure SQL Database and Azure SQL Managed Instance. Strict (SQL Server 2022 and Azure SQL) encryption can be used for SQL Server when the instance has Force Strict Encryption enabled.

    Connect and query SQL Server using SSMS - SQL Server Management Studio (SSMS) (3)

  3. After you complete all the fields, select Connect.

    You can also modify extra 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 fields.

  4. To verify that your SQL Server connection succeeded, expand and explore the objects within Object Explorer where the server name, the SQL Server version, and the username are displayed. These objects are different depending on the server type.

    Connect and query SQL Server using SSMS - SQL Server Management Studio (SSMS) (4)

Create a database

Now let's create a database named TutorialDB by following the below steps:

  1. Right-click your server instance in Object Explorer, and then select New Query:

    Connect and query SQL Server using SSMS - SQL Server Management Studio (SSMS) (5)

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

    USE masterGOIF NOT EXISTS ( SELECT name FROM sys.databases WHERE name = N'TutorialDB')CREATE DATABASE [TutorialDB]GO
  3. Execute the query by selecting Execute or selecting F5 on your keyboard.

    Connect and query SQL Server using SSMS - SQL Server Management Studio (SSMS) (6)

    After the query is complete, the new TutorialDB database appears in the list of databases in Object Explorer. If it isn't displayed, right-click the Databases node, and then select Refresh.

Create a table

In this section, you create a table in the newly created TutorialDB database. Because the query editor is still in the context of the master database, switch the connection context to the TutorialDB database by doing the following steps:

  1. In the database dropdown list, select the database that you want, as shown here:

    Connect and query SQL Server using SSMS - SQL Server Management Studio (SSMS) (7)

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

    USE [TutorialDB]-- Create a new table called 'Customers' in schema 'dbo'-- Drop the table if it already existsIF OBJECT_ID('dbo.Customers', 'U') IS NOT NULL DROP TABLE dbo.CustomersGO-- Create the table in the specified schemaCREATE 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
  3. Execute the query by selecting Execute or selecting F5 on your keyboard.

After the query is complete, the new Customers table is displayed in the list of tables in Object Explorer. If the table isn't displayed, right-click the TutorialDB > Tables node in Object Explorer, and then select Refresh.

Connect and query SQL Server using SSMS - SQL Server Management Studio (SSMS) (8)

Insert rows

Now let's insert some rows into the Customers table that you created. Paste the following T-SQL code snippet into the query window, and then select 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

Query the table and view the results

The results of a query are visible below the query text window. To query the Customers table and view the rows that were inserted, paste the following T-SQL code snippet into the query window, and then select Execute:

-- Select rows from table 'Customers'SELECT * FROM dbo.Customers;

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

Connect and query SQL Server using SSMS - SQL Server Management Studio (SSMS) (9)

You can also modify the way results are presented by selecting one of the following options:

Connect and query SQL Server using SSMS - SQL Server Management Studio (SSMS) (10)

  • The first button displays the results in Text View, as shown in the image in the next section.
  • The middle button displays the results in Grid View, which is the default option.
  • The third button lets you save the results to a file whose extension is .rpt by default.

Troubleshoot connectivity issues

To review troubleshooting techniques to use when you can't connect to an instance of your SQL Server Database Engine on a single server, visit Troubleshoot connecting to the SQL Server Database Engine.

Related content

  • SQL Server Management Studio (SSMS) Query Editor
  • Script objects in SQL Server Management Studio
  • Use templates in SQL Server Management Studio
  • SQL Server Management Studio components and configuration
  • Tips and tricks for using SQL Server Management Studio (SSMS)
Connect and query SQL Server using SSMS - SQL Server Management Studio (SSMS) (2024)
Top Articles
Delete browsing data in Chrome - Computer
Components of DSC in India - Vakilsearch
What Is Single Sign-on (SSO)? Meaning and How It Works? | Fortinet
11 beste sites voor Word-labelsjablonen (2024) [GRATIS]
What Are the Best Cal State Schools? | BestColleges
Watch Mashle 2nd Season Anime Free on Gogoanime
Words From Cactusi
12 Best Craigslist Apps for Android and iOS (2024)
Caroline Cps.powerschool.com
Dutchess Cleaners Boardman Ohio
Rachel Griffin Bikini
Salem Oregon Costco Gas Prices
Everything We Know About Gladiator 2
Craigslist Missoula Atv
Evil Dead Rise - Everything You Need To Know
Loft Stores Near Me
Why Does Lawrence Jones Have Ptsd
UMvC3 OTT: Welcome to 2013!
Scheuren maar: Ford Sierra Cosworth naar de veiling
Project Reeducation Gamcore
Greyson Alexander Thorn
University Of Michigan Paging System
Jcp Meevo Com
Koninklijk Theater Tuschinski
Mals Crazy Crab
55Th And Kedzie Elite Staffing
Blackboard Login Pjc
What Is a Yurt Tent?
Claio Rotisserie Menu
Skidware Project Mugetsu
Pacman Video Guatemala
Meggen Nut
Gt7 Roadster Shop Rampage Engine Swap
Sam's Club Gas Price Hilliard
Bursar.okstate.edu
Bridger Park Community Garden
Cvb Location Code Lookup
Naya Padkar Newspaper Today
Top-ranked Wisconsin beats Marquette in front of record volleyball crowd at Fiserv Forum. What we learned.
Reborn Rich Ep 12 Eng Sub
The 50 Best Albums of 2023
USB C 3HDMI Dock UCN3278 (12 in 1)
3302577704
Oriellys Tooele
Publictributes
Brandon Spikes Career Earnings
40X100 Barndominium Floor Plans With Shop
Myapps Tesla Ultipro Sign In
Runescape Death Guard
7 Sites to Identify the Owner of a Phone Number
Qvc Com Blogs
Pauline Frommer's Paris 2007 (Pauline Frommer Guides) - SILO.PUB
Latest Posts
Article information

Author: Manual Maggio

Last Updated:

Views: 6008

Rating: 4.9 / 5 (49 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Manual Maggio

Birthday: 1998-01-20

Address: 359 Kelvin Stream, Lake Eldonview, MT 33517-1242

Phone: +577037762465

Job: Product Hospitality Supervisor

Hobby: Gardening, Web surfing, Video gaming, Amateur radio, Flag Football, Reading, Table tennis

Introduction: My name is Manual Maggio, I am a thankful, tender, adventurous, delightful, fantastic, proud, graceful person who loves writing and wants to share my knowledge and understanding with you.