Simple Guide to provision SQL Server 2017 on Linux in Azure Virtual Machine

October 12, 2017 1 comment

Hi Everyone, In this article, we will see how to provision SQL Server 2017 on Linux in Azure Virtual Machine

Step 1: Please login to azure portal and go to Virtual Machine blade

capture20171006153829221

Step 2: Please search for SQL Server 2017

capture20171006153917048

Step 3: Please select SQL Server 2017 Developer on Ubuntu Server, I have used Ubuntu linux for this demo but you can use Red Hat or Suse Linux as well

capture20171006153950231

Step 4: Please review the legal terms, select deployment model, I prefer to use Resource Manager, so that management and cleanup is easier, Please also copy the highlighted command which will be helpful for configuring the sa password for SQL Server later

capture20171006154030026

Step 5: Please provide the name of the VM, select Disk type, provide user name and select Authentication Type, in this case I am using Password

Please select the Azure Subscription

For Resource group, I am creating a new one named “SQLLinux” and using Azure data center location as “East US”, please change this according to your region

capture20171006155312325

Step 6: In this step, we need to select the Size of our Linux VM, I am using B2S Standard, which has 2 vCPUs and 4 GB RAM, which is good for our testing, but you can choose the size based on your requirement, after selecting the size, Please click “Select” to proceed

capture20171006155634830

Step 7: In this step, we will review some of the important settings

Virtual Network – For this demo, I am creating a new Virtual Network, but if you already have other VMs running in Azure and you need this SQL Server VM to be accessible for them, Please use the existing Virtual Network

Public IP Address – I have enabled Public IP Address for this demo, but this is optional, Please don’t enable this, if you are planning to use this server only within Azure, so that it will reduce the exposure

Network Security Group (Firewall) – We will see in next step, How to configure firewall to allow external access

Auto Shutdown – This is an useful optional setting for demo or test servers, so that you can configure it to shutdown automatically based on a timing, this will reduce billing of your VM

Time Zone – Please select the time zone based on your region

capture20171006155902614

Step 8: Additional Settings Please enable Monitoring for both Boot diagnostics and Guest OS diagnostics, This will help you see the Boot log for any error or messages, after making required Settings changes, Click “Ok” to proceed

capture20171006155918920

Step 9:  To allow external access to SQL Server,  we need to change Network Firewall setting by adding Inbound and Outbound security rules for MSSQL Service

Note: You can do this change after creating the Virtual machine

capture20171006161838234

capture20171006161922839

Step 10: Review the selection options and click “Purchase” to buy and create the virtual machine

capture20171006160025569

Step 11: You can review the Virtual Machine deployment progress in the screen

capture20171006160058057

If you closed it, you can see from the Top right corner Notifications area

capture20171006160124574

You can also see the status under Virtual Machines blade as well

capture20171006160259721

Once Virtual machine is provisioned and ready, you should see the status as “Running”, see below screenshot

capture20171006160508333

Step 12:  Please click on the Virtual Machine to see the settings and also to monitor the CPU/Network/Disk usage

capture20171006160608869

Step 13: Configuring SQL Server in Azure Linux VM

Please connect to Azure Linux VM using any preferred SSH client, In this demo, I am using Bash Shell on Windows, since its built in Windows 10 now

Command to connect, Please type “Yes” to accept the authenticity of the host

ssh username@serveripaddress

capture20171006160654951

Step 14: After logging in to VM, Please run the below command to check the status of SQL Server

sudo systemctl status mssql-server

Since we haven’t configured SA password, SQL Server is not running, so you seeing the status as “Inactive”

capture20171006160938531

Step 15: Please run below command to configure SA password

sudo /opt/mssql/bin/mssql-conf setup

Please select your preferred edition of SQL Server, in this demo, we will use Developer edition, in next prompt, Please specify the SA password and press enter

capture20171006161516727

Once password is setup, SQL Server 2017 will automatically run, Please run the status command again to review the SQL Server status

sudo systemctl status mssql-server

capture20171006161118193

Step 16: Please run “SELECT @@VERSION” using SQLCMD utility to connect to the server to verify we are able to connect and see SQL Server version

capture20171006161645028

Step 17: Now we can use SQL Server Management Studio to connect using the Public IP Address and SA password provisioned

capture20171006162510369

Once connected, Please create your required databases and database objects and use as needed

capture20171006162600552

capture20171006162708830

SQL Server 2017 Developer Edition is up and running Azure Linux VM now in under 5 minutes

I believe you will find this post useful !!!

Advertisements

XE Profiler / Extended Events Profiler – New SQL Server Management Studio Feature

October 11, 2017 Leave a comment

SQL Server Management Studio v 17.3 introduces new feature called “XE Profiler” or “Extended Events Profiler”, This will be the obvious replacement for SQL Server Profiler which was part of SQL Server Tools since SQL Server 7.0

Out of the box, XE Profiler supports the following options

capture20171011111042838

TSQL template provide simple details such as Event Sequence, Name of the Event, Query Text, Session ID, Time Event occurred

capture20171011111119585

Standard Template provide more details such as Client Application Name, User Name, CPU Time, Logical Reads, Writes, Duration

capture20171011111514648

You can Pause and Resume profiling using these options

capture20171011111834646

Clear Data option is enabled only when profiling is on and running, I wish this should be enabled in stopped state as well to cleanup

One of my personal favorite feature is Bookmarks

capture20171011112054442

We can setup Bookmarks on statements which you want to review later using “Toggle Bookmark” option from Toolbar or Context menu, we can navigate between Bookmarks using “Previous Bookmark” and “Next Bookmark” options, We can clear the Bookmarks using “Clear All Bookmarks” option, it would be nice to have a warning when we use this option, so that accidental cleanup of all bookmarks can be avoided

Find” is a handy option to search for specify details such as query text

capture20171011112751489

We can also customize the Search for specific column by changing the Look In

capture20171011112956258

Also we have option to customize the list of columns we want to see as well

capture20171011113059525

We also have option to create Merged columns by combining up to 5 columns

capture20171011113242153

We can save the changes made to display settings and reuse the previously saved settings as well

capture20171011113544245

Filters option enables us to run the profiler to target specific database or application or by specific username

capture20171011113748783

We can also setup Time based filter and Fields supported in filter are as follows

capture20171011114012086

capture20171011114031275

Hope you find this article helpful !!!

Import Flat File Wizard–New Feature in SQL Server Management Studio v 17.3

October 10, 2017 Leave a comment

SQL Server Management Studio v 17.3 is released and available for download now, it packs lot of new features and updates

One of the most interesting features is “Import Flat File” Feature which can help novice to experienced users to load Flat file to SQL Server

capture20171010153301560

“Import Flat File” option is available at Database level under Tasks Context menu

capture20171010151258718

In this article, I am using FDA’ Orange Book Data product files for loading, as you can see this file is separated by ~

FDA Orange Book Data Files

capture20171010151446560

Please click “Next” to start the Import Flat File  process

capture20171010151620937

Please select the Input file, The tool is smart and it uses the File name as Target table name

capture20171010151714495

But if we prefer, we can change the Target table according to our needs or naming convention

capture20171010151751094

Click “Next” to proceed to “Preview Data” screen, In “Preview Data” screen we can see the sample records

capture20171010151752654

After previewing the data, Please click “Next” to proceed to “Modify Columns” screen, based on sampled rows, it automatically suggests the data types, but we can change the Data Type based on our input source specification, in this case, I have changed the “Applicant_Full_Name” to NVARCHAR(500) from NVARCHAR(100) and also we can specify Primary Key, which I have selected “Appl_No” and “Product_No” as Combined Primary Key

capture20171010152124723

After making required changes to Column definition, Please click to proceed, depending on the size of the data, the Import operation will run and complete

capture20171010152556076

You can click on the “Success” hyperlink in Result to view the Completed message, once reviewed, Please click “Close” to continue

capture20171010152600380

Now let us review the data imported, We can see the new table is created

capture20171010152614195

We are running a simple Select Query to see all records loaded, We can see 2499 rows are loaded

capture20171010152715115

This tools is really simple and easy to use, I believe you will find this article helpful

TRANSLATE()–Replace multiple characters in single function call

October 6, 2017 Leave a comment

TRANSLATE() is a new SQL Server 2017 string function which supports replacing multiple characters in one function call, This will help us in avoid using multiple replace statements

Syntax

TRANSLATE(InputString, StringPattern, StringReplacement)

Example Usage

In this example, we have values delimited by , for field terminator and ~ for row terminator, which we are going to replace , (comma) with | (pipe) and ~ (Tilde) with # (Hash)

DECLARE @strInput VARCHAR(255) = ‘Test1,Test2,Test3,Test4,Test5~Test11,Test12,Test13,Test14,Test15~Test21,Test22,Test23,Test24,Test25’

SELECT @strInput [Input],
REPLACE(REPLACE(@strInput, ‘,’, ‘|’), ‘~’, ‘#’) [SQL 2016 & Before – Output],
TRANSLATE(@strInput, ‘,~’, ‘|#’) [SQL 2017 Output]

Output

capture20171006114812618

As you can see, in SQL Server 2016 and prior, we need to use 2 REPLACE() function calls, in SQL Server 2017, we can use 1 TRANSLATE() function call to implement the same functionality

Hope you find this post useful !!!

STRING_AGG() – Concatenate values in Rows with Separator

October 5, 2017 Leave a comment

SQL Server 2017 introduces a very helpful String function to concatenate values from rows as one value with separator and main advantage is it doesn’t add the separator in the end of the value

Syntax for the function is as follows

STRING_AGG ( expression, separator ) [
WITHIN GROUP ( ORDER BY <order_by_expression_list> [ ASC | DESC ] ) ]

Simple Example Usage – Building CSV from one field

SELECT String_Agg(strData, ‘,’) AS CSV FROM dbo.Test

capture20171005142123779

Example Usage with ORDER BY option on different field

SELECT String_Agg(strData, ‘,’) WITHIN GROUP (ORDER BY intPK DESC) AS CSV FROM dbo.Test

capture20171005142257083

Example Usage with GROUP BY and ORDER BY

SELECT
Category,
String_Agg(strData, ‘,’) WITHIN GROUP (ORDER BY intPK DESC) AS CSV1
FROM dbo.Test
GROUP BY Category

capture20171005142405536

What is not supported ?

ORDER BY is supported only for one field or one set of fields and one direction only, , see below example, We cannot use ORDER BY intPK DESC for first STRING_AGG() and ORDER BY intPK ASC for next STRING_AGG

SELECT
Category,
String_Agg(strData, ‘,’) WITHIN GROUP (ORDER BY intPK DESC) AS CSV1,
String_Agg(strData, ‘,’) WITHIN GROUP (ORDER BY intPK ASC) AS CSV2
FROM dbo.Test
GROUP BY Category

 

The above query will return the below error message

Msg 8711, Level 16, State 1, Line 15
Multiple ordered aggregate functions in the same scope have mutually incompatible orderings.

Alternative way to get around this is use multiple queries and join them.

;WITH cteA
AS
(
SELECT
Category,
String_Agg(strData, ‘,’) WITHIN GROUP (ORDER BY intPK DESC) AS CSV1
FROM dbo.Test
GROUP BY Category
),
cteB
AS
(
SELECT
Category,
String_Agg(strData, ‘,’) WITHIN GROUP (ORDER BY intPK ASC) AS CSV2
FROM dbo.Test
GROUP BY Category
)
SELECT
A.Category, A.CSV1, B.CSV2
FROM cteA A
LEFT JOIN cteB B ON A.Category = B.Category

capture20171005143658060

Hope you find this post helpful !!!

TRIM()–New String function in SQL Server 2017

October 4, 2017 Leave a comment

SQL Server 2017 introduces long awaited string function which supports both LTRIM() and RTRIM() together

Syntax for the function is as follows

TRIM ( [ characters FROM ] string )

Example usage

SELECT TRIM(‘   SQLXpertise   ‘) [SQL2017]

Output

image

In addition to trimming spaces, TRIM() function supports removing other characters in prefix and suffix as well, see below example, we are providing #, ~, <SPACE> as characters to removed from Prefix and Suffix

Example with Character removal

SELECT TRIM(‘#~ ‘ FROM ‘#   SQLXpertise   ~’) [SQL2017]

 

Output

image

This new function will be helpful in using one TRIM() call rather than 3 function calls of LTRIM(), RTRIM() and REPLACE()

Hope  you find this post helpful !!!

CONCAT_WS() – Concatenate with Separator–New String Function in SQL Server 2017

October 3, 2017 Leave a comment

SQL Server 2017 has a new String function which supports concatenating multiple values with separator, ex. Comma (,) or Pipe (|)

This function will come handy, if you want to generate a CSV file quickly

Syntax for the function is as follows

CONCAT_WS ( separator, argument1, argument1 [, argumentN]… )

Example Usage

CREATE TABLE [dbo].[Test](
[intPK] [int] IDENTITY(1,1) NOT NULL,
[strData] [varchar](50) NOT NULL,
PRIMARY KEY CLUSTERED
(
[intPK] ASC
)
)
GO

INSERT INTO dbo.test (strData) VALUES (‘Test1’)
INSERT INTO dbo.test (strData) VALUES (‘Test2’)
INSERT INTO dbo.test (strData) VALUES (‘Test3’)
INSERT INTO dbo.test (strData) VALUES (‘Test4’)
INSERT INTO dbo.test (strData) VALUES (‘Test5’)

SELECT CONCAT_WS(‘|’, intPK, strData) AS Result FROM dbo.Test

In above example we are concatenating 2 fields using pipe as separator

Output

capture20171003120119644

NULL Handling

If any of the data fields has NULL, then we need to use ISNULL() or COALESCE() to handle this, else, the structure of the output will change

ALTER TABLE dbo.Test ADD strNullData VARCHAR(50) NULL

UPDATE dbo.Test SET strNullData=’A’ WHERE intPK=1
UPDATE dbo.Test SET strNullData=’B’ WHERE intPK=3
UPDATE dbo.Test SET strNullData=’C’ WHERE intPK=5

SELECT CONCAT_WS(‘|’, intPK, strNullData, strData) Result FROM dbo.Test

In the below output, rows 1,3 and 5 has 3 fields and rows 2 and 4 has only 2 fields

capture20171003125644345

Query with ISNULL() Handling

SELECT CONCAT_WS(‘|’, intPK, ISNULL(strNullData, ”), strData) Result FROM dbo.Test

Now the below output will have consistent list of fields

capture20171003125845846

Hope you find this helpful !!!

%d bloggers like this: