Quickstart: Use SQL Server Management Studio to connect and query an Azure SQL database

You can use SQL Server Management Studio (SSMS) to manage any SQL infrastructure, from SQL Server to SQL Database for Microsoft Windows. This quickstart shows you how to use SSMS to connect to an Azure SQL database and then run Transact-SQL statements to query, insert, update, and delete data.

Prerequisites

To complete this tutorial, you need:

  • An Azure SQL database. You can use one of these techniques to create a database:

Install the latest SSMS

Before you start, make sure you've installed the latest SSMS.

SQL server connection information

Get the connection information you need to connect to the Azure SQL database. You'll need the fully qualified server name, database name, and login information for the upcoming procedures.

  1. Sign in to the Azure portal.

  2. Select SQL databases from the left menu, then select your database on the SQL databases page.

  3. On the Overview page for your database, review the fully qualified server name next to Server name. To copy the server name, hover over it and select the Copy icon.

server-name

If you forget your login information, select the server name to open the SQL server page. Here you can view the Server admin name, and select Reset password if necessary.

Connect to your database

In SMSS, connect to your Azure SQL Database server.

Important

An Azure SQL Database logical server listens on port 1433. To connect to a logical server from behind a corporate firewall, the firewall must have this port open.

  1. Open SSMS. The Connect to Server dialog box appears.

  2. Enter the following information:

    Setting     Suggested value Description 
    Server type Database engine Required value.
    Server name The fully qualified server name Something like: mynewserver20170313.database.windows.net.
    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

  3. Select Options in the Connect to Server dialog box. In the Connect to database drop-down menu, select mySampleDatabase.

    connect to db on server

  4. Select Connect. The Object Explorer window opens.

  5. To view the database's objects, expand Databases and then expand mySampleDatabase.

    view database objects

Query data

Use the following 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 blank query window opens that is connected to your database.

  2. In the query window, paste this 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 retrieve data from the Product and ProductCategory tables.

    query to retrieve data from 2 tables

Insert data

Use the following 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

Update data

Use the following UPDATE Transact-SQL code to modify the new product you just added.

  1. Replace the previous query with this one.

    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

Use the following DELETE Transact-SQL code to remove the new product that you previously added.

  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