Quickstart: Use SSMS to connect to and query Azure SQL Database or Azure SQL Managed Instance

Applies to: Azure SQL Database Azure SQL Managed Instance

In this quickstart, you'll learn how to use SQL Server Management Studio (SSMS) to connect to Azure SQL Database or Azure SQL Managed Instance and run some queries.

Prerequisites

Completing this quickstart requires the following items:

If you simply want to run some ad hoc queries without installing SSMS, use the Azure portal's query editor to query a database in Azure SQL Database.

Get server connection information

Get the connection information you need to connect to your database. You'll need the fully qualified server name or host name, database name, and login information to complete this quickstart.

  1. Sign in to the Azure portal.

  2. Navigate to the database or managed instance you want to query.

  3. On the Overview page, review the fully qualified server name next to Server name for your database in SQL Database or the fully qualified server name (or IP address) next to Host for your managed instance in SQL Managed Instance or your SQL Server instance on your VM. To copy the server name or host name, hover over it and select the Copy icon.

Note

For connection information for SQL Server on Azure VM, see Connect to SQL Server

Connect to your database

Note

In December 2021, releases of SSMS prior to 18.6 will no longer authenticate through Microsoft Entra ID with MFA.

To continue utilizing Microsoft Entra authentication with MFA, you need SSMS 18.6 or later.

In SSMS, connect to your server.

Important

A server listens on port 1433. To connect to a server from behind a corporate firewall, the firewall must have this port open.

  1. Open SSMS.

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

    Setting Suggested value Description
    Server type Database engine Required value.
    Server name The fully qualified server name Something like: servername.database.chinacloudapi.cn.
    Authentication SQL Server Authentication This tutorial uses SQL Authentication.
    Login Server admin account user ID The user ID from the server admin account used to create the server.
    Password Server admin account password The password from the server admin account used to create the server.

    connect to server

Note

This tutorial utilizes SQL Server Authentication.

  1. Select Options in the Connect to Server dialog box. In the Connect to database drop-down menu, select mySampleDatabase. Completing the quickstart in the Prerequisites section creates an AdventureWorksLT database named mySampleDatabase. If your working copy of the AdventureWorks database has a different name than mySampleDatabase, then select it instead.

    connect to db on server

  2. Select Connect. The Object Explorer window opens.

  3. To view the database's objects, expand Databases and then expand your database node.

    mySampleDatabase objects

Query data

Run this SELECT Transact-SQL code to query for the top 20 products by category.

  1. In Object Explorer, right-click mySampleDatabase and select New Query. A new query window connected to your database opens.

  2. In the query window, paste the following SQL query:

    SELECT pc.Name as CategoryName, p.name as ProductName
    FROM [SalesLT].[ProductCategory] pc
    JOIN [SalesLT].[Product] p
    ON pc.productcategoryid = p.productcategoryid;
    
  3. On the toolbar, select Execute to run the query and retrieve data from the Product and ProductCategory tables.

    query to retrieve data from table Product and ProductCategory

Insert data

Run this INSERT Transact-SQL code to create a new product in the SalesLT.Product table.

  1. Replace the previous query with this one.

    INSERT INTO [SalesLT].[Product]
            ( [Name]
            , [ProductNumber]
            , [Color]
            , [ProductCategoryID]
            , [StandardCost]
            , [ListPrice]
            , [SellStartDate] )
      VALUES
            ('myNewProduct'
            ,123456789
            ,'NewColor'
            ,1
            ,100
            ,100
            ,GETDATE() );
    
  2. Select Execute to insert a new row in the Product table. The Messages pane displays (1 row affected).

View the result

  1. Replace the previous query with this one.

    SELECT * FROM [SalesLT].[Product]
    WHERE Name='myNewProduct'
    
  2. Select Execute. The following result appears.

    result of Product table query

Update data

Run this UPDATE Transact-SQL code to modify your new product.

  1. Replace the previous query with this one that returns the new record created previously:

    UPDATE [SalesLT].[Product]
    SET [ListPrice] = 125
    WHERE Name = 'myNewProduct';
    
  2. Select Execute to update the specified row in the Product table. The Messages pane displays (1 row affected).

Delete data

Run this DELETE Transact-SQL code to remove your new product.

  1. Replace the previous query with this one.

    DELETE FROM [SalesLT].[Product]
    WHERE Name = 'myNewProduct';
    
  2. Select Execute to delete the specified row in the Product table. The Messages pane displays (1 row affected).

Next steps