1. 程式人生 > >An ultimate guide to Azure Data Studio

An ultimate guide to Azure Data Studio

An ultimate guide to Azure Data Studio

Azure Data Studio is a cross-platform database tool: If you are a MacOS or Linux user, you can see this as a mini-version of SQL Server Management Studio (SSMS) and my savior from a virtual machine to run SQL server.

Azure DS is equipped with Intellisense (fancy out-complete), version control integration (Git) as well as a really cool T-SQL editor. It can save your results as text, CSV or JSON.

Let’s go ahead and get started:

Installation

Setting up Azure SQL database:

You will need a server to link Azure DS to (in my case, I link it to Azure SQL database). If you are not familiar with how to create your own FREE Azure SQL database, you can follow this step-by-step tutorial that I created earlier.

Download and Install Azure Data Studio:

Download your Azure DS, and install it (Mac users, just double-click on it :D)

Azure Data Studio Connection prompt

Setup your connection with Azure SQL database:

At launch, Azure DS prompts you to connect to a server. Here are the key inputs that you will need:

  • Connection Type:
    Microsoft SQL Server
  • Server: you can fetch this piece of information from your Azure SQL database (if you followed the tutorial mentioned earlier, it should be something like sql-test-medium-001.database.windows.net)
  • Authentication Type: SQL Login
  • Username: your server log-in that you set-up for your server (tutorial)
  • Password: same password used to set-up your server
  • Database: Select your database name
  • Name (optional): something easy to remember
Example of where to find your server name in Azure SQL database

Test your connection:

STOP: Before you proceed, if you didn’t set up your server’s firewall to allow connections, your connection prompt will pop this form below. Add your client IP and your Azure account, and you should be good to go.

Firewall prompt

You should see your server connected. You can expand the tables in your server by clicking on the drop down in your left pane.

To test your connection, click on New Query or hit cmd-N to open a new query window. Type this SQL command to test your connection: SELECT * FROM SalesLT.Customer; and then hit Run. You should expect an output just like the one below.

Did you notice the Intellisense at play? — This is really my favorite part!

Create stored procedures

If you are familiar with job scheduling in SSMS, this is it’s Azure DS equivalent, just much cooler!

  1. Open a new query window or hit cmd-N
  2. Type sql, which should show you a drop-down menu. Select sqlCreateStoredProc
  3. It will create a template for a stored procedure. Let’s break it down into different pieces:

A. Checks if there exists any other stored procedure with the same name, Drops it and creates this one

B. Creates this stored procedure

C. Executes this stored procedure

4. Let’s update the template to fit our needs:

Example for change all occurences
  • Right-click on StoredProcedureName, click on Change All Occurrences and change it to the name you want to call your procedure. I named it testProc
  • Also, Right-click on Schema, click on Change All Occurrences and change it to your schema name (By default, if you didn’t set it up, it should be dbo, so swap it to dbo)
  • Update the function of the stored procedure:
  1. Take @customerID as input
  2. Select all the records which have the same CustomerID as the input
  3. Hit Run. This will create your stored procedure, and also runs it: EXECUTE dbo.testProc 1 will run testProc with @customerID = 1.

How to change your output to JSON

Starting from the stored procedure we just used, we will output the data in a JSON format. Now, this might come in handy in different scenarios, but in my case: it fits perfectly for a visualization tool with a javascript front end.

Reminder: In this stored procedure, we fetch all the data for any customer with the input customer id.

Now, let’s aim to store the output in a JSON file. You just add this command after your query: FOR JSON PATH et voilà!

Let’s see how the output looks like below. You can even add this command after any query and the output will be in JSON format.

PS: Even if the command has a squiggly line underneath it — it still works, trust me, I tried it :)

How to create COOL visualizations

This is huge — I am super excited that Azure Data Studio lets you create your own mini visualizations instead of just a table. You can even add it to a dashboard that constantly refreshes (can become handy in query performance, PSI calculations or literally anything).

Let’s get started already!

Create an insight

  1. Write a small query just to try this out: SELECT title as Titles, count(*) as CountFROM SalesLT.CustomerGROUP by Title

This query will fetch all the different titles for the customers, and then groups them and returns the count of each title.

2. Instead of the tabulated results, you can click on the plot icon on the right.

3. This will show you a bar plot that represents our data. Don’t forget to click Use First Column as row label in the bar plot’s settings. The actual plot is so cool, it shows you a clear legend with a tooltip functionality to check the actual counts.