Top 10 exciting new features in SQL Server Management Studio 2016


[This article was originally posted in CodeProject]

Introduction

This article is an effort to list out few exciting improvements those has been made to SQL Server Management Studio 2016. I believe, most of these new features can help improve the speed of our work and more convenient to use the SSMS.

1. Color Theme

With SSMS 2016, now you can choose your preferred color theme, a feature similar to that in Visual Studio. For now, you can choose one from 2 available color themes – Blue or Light, Blue being the default theme.

Tools - Options - Environment - General - Color theme

2. Hide Save Query Window Prompt

In earlier versions of SSMS, you can’t ignore the save query windows prompt that comes when you want to close either an unsaved query window or the SSMS itself. While this restriction is actually meaningful to have in SSMS but with SSMS 2016, you can now customize to hide those prompts as per your preference.

Tools - Options - Query Execution - SQL Server - General - “Prompt to save unsaved T-SQL query windows on close”

3. Update from SSMS Menu

SSMS 2016 automatically checks for any updates available when it is online and a toast notification comes when there is an update available something as following screenshot-

However, you can also check manually for updates availability.

Tools - “Check for Updates…”

You can disable automatic checking for updates by unchecking the checkbox in the popup window that appears when you click “Check for Updates…” as described above.

4. Search Options

Confused with so many new features and steps to open the window to configure them? Don’t worry, SSMS now has the Search Options which is one of much requested feature. You can search with the feature name or a keyword.

Tools - Options

5. Highlight Current Line

How many of the times you feel lost while modifying an existing huge query or stored procedure in SSMS? The problem occurs when your SQL statements are very long and you haven’t configured text editor to wrap words automatically and then when you use the horizontal scrollbar to scroll the content, it becomes difficult to identify which the line you wanted to modify or check. Now, with SSMS 2016, an outline will follow your cursor where ever you placed it. That makes easy to identify the line you are currently working with. The best part is, it can be configured.

Tools - Options - Text Editor - General - “Highlight current line”

6. Scroll & Zoom Execution Plan Window

There are also few improvements done on the Execution Plan window. Now you can zoom the content of this window using shortcut CTRL + Scroll. You can also scroll the content of the window by dragging the content with mouse.

7. Retain Carriage Return and Line Feeds on Copy or Save

It may seem to you as a weird default behavior. In SSMS 2016, if you copy any result from the Result Grid and paste it in a notepad or any text editor, you will notice that the carriage return and line feeds are not in place. However, this can also be configured.

Tools - Options - Query Results - SQL Server - Results to Grid - "Retain CR/LF on copy or save"

Example:

Executed following query by unchecking the checckbox.

SELECT 'This is a demo text
for test purpose only' AS demotext

The output while copying to notepad was as follows-

demotext
-------------------
This is a demo text  for test purpose only

Now, executed the same query after checking the mentioned checkbox and got result as –

demotext
------------------
This is a demo text
for test purpose only

 

8. Advanced Scroll Bar

If you have used Visual Studio, then probably you are already aware of such feature. With SSMS 2016, you can use “bar mode” or “map mode” for the vertical scroll bar in the query window.

  • Bar Mode : This is the default and normal vertical scroll bar same as you can see in the previous versions.
  • Map Mode: This is new and source overview can be viewed on the scroll bar which can again be configured as –
    • Off : Source Overview will not be visible
    • Narrow: Overview with minimum width
    • Medium: Wider than previous
    • Wide: Widest scrollbar with overview

You can also configure to show Tooltip on hover or scroll using checkbox “Show Preview Tooltip”

Tools - Options - Text Editor - All languages - Scroll Bars - Behavior

You can also open the configuration window by right clicking on the vertical scroll bar and then clicking "Scroll Bar Options..."

To know rest of the features , please visit my original article on CodeProject

Thanks for reading. Awaiting your feedback 🙂

SQL Server 2016: Masking sensitive data


Introduction

Data Masking

At the time of writing this article, it’s been more than 2 months since the most secure and powerful SQL Server yet released for public. It has many features which you can use to make your data and application more secure, more maintained and easily retrieved as per your requirements. It has introduced both client side security such as Always Encrypted as well as server side security such as Data masking etc. In this post we’ll examine how Dynamic Data Masking can help secure our data.

Earlier we were masking the sensitive data by applying logic at application end or by replacing characters at SQL Server end and then returning the masked data to the application. In the former case the risk was at
1. When application fails to mask data
2. Sensitive data transferred over network
3. All the users in SQL Server with SELECT permission still can access the unmasked data

In the later case, the first two risks are eliminated but still there is a chance that the sensitive data can be revealed by the SQL Server user through which the application retrieves data.
1. When application fails to mask data
2. Sensitive data transferred over network
3. All the users in SQL Server with SELECT permission still can access the unmasked data

So, let’s quickly jump in to the next section to understand what Dynamic Data masking is and how it helps to eliminate all these chances of unmasked data getting revealed.

Data Masking

If you search for definition of Data Masking over the web, you’ll end up with varied descriptions. The simplest and straight forward definition can be –

“Data masking is the process of hiding original data with random characters or data.”
wikipedia

If we look further, there are various data masking techniques available such as

  • Shuffling: Shuffling order of the characters in the value.
    Ex: 12345 —–> 35312
  • Nulling: Substituting characters in the value with null (hash) symbol.
    Ex: 12345 —–> ###45
  • Substitution: Substituting value with another value from a substitution table.
    Ex: Suvendu Giri —–> John Ptak
  • Masking Out: Masks the complete data or a selective part of the data.
    Ex: suvendu@mydomain.com —–> suvendu@xxxx.xxx
    etc.

Considering many techniques of data masking they can be categorized into 3 types-

  • Static Data Masking: Original data in production changed to masked data.
  • On-the-fly Data masking: Copies data from one source to another source and masking done on the later.
  • Dynamic data masking: Masking done dynamically at runtime.

Dynamic Data Masking

“Dynamic data masking limits sensitive data exposure by masking it to non-privileged users. Dynamic data masking helps prevent unauthorized access to sensitive data by enabling customers to designate how much of the sensitive data to reveal with minimal impact on the application layer. It’s a data protection feature that hides the sensitive data in the result set of a query over designated database fields, while the data in the database is not changed.”
MSDN

So, dynamic data masking alters the result for non-privileged users while streaming and not with data in the production database.

Data Masking in MySQL:

As far I know, till the recent version of MySQL there is no inbuilt mechanism to provide dynamic data masking capabilities. The possible options for MySQL users seems to be

  1. Use 3rd party tools
  2. Use custom scripts/functions

Examples:

SELECT '****' AS PhoneNumber
FROM `Employees`
--Input: 1234567890        Output: ****
SELECT REPEAT('*', CHAR_LENGTH(PhoneNumber) - 6) AS PhoneNumber
FROM `Employees`
--Input: 1234567890                        Output: **********
SELECT CONCAT(SUBSTR(PhoneNumber, 1, 4), REPEAT('*', CHAR_LENGTH(PhoneNumber) - 4)) AS PhoneNumber
FROM `Employees`
--Input: 1234567890                        Output: 1234******

Data Masking in PostgreSQL:

Same as MySQL. It seems they are still lacking this feature. However, we can make use of some custom scripts or user defined functions.

Azure SQL Database too supports the dynamic data masking similar to SQL Server and I have heard from my friends that Oracle provides some kind of mechanism for data masking, although I haven’t ever used it.

Dynamic Data Masking in SQL Server 2016:

There are four masking functions available in SQL Server 2016 to provide separate ways to mask your data when they are returned by a query. These are-

[Read complete article at this link (CodeProject.com) ]

Get your hands on SQL Server 2016 RC3 on Azure


[ Note: This post is for beginners with Microsoft Azure and just about creating the environment. Will cover few exciting features in the next blog posts ]

Expiry date of the post: 1st June 2016
A post, with an expiry date ! Sounds illogical, isn’t it?
Well, it’s true and I will explain this soon.

If you are from database background and have ever used SQL Server or currently working on areas related to data or SQL Server, you must be aware of the trending buzz among the people around you. A more faster, secure and powerful SQL Server, SQL Server 2016 will be available soon. Microsoft continuously surprising people with their moves towards making many development tools like Visual Studio, .Net etc cross-platform, open-source and more feature rich. SQL Server 2016 have few surprises too and with many new features & improvements.

The biggest news this year for database professionals was the announcement to make SQL Server available to run on Linux OS (the expected time for availability is mid of 2017, though). Apart from this, peoples at Microsoft have worked hard to bring many exciting features to SQL Server 2016 in the areas like Security, BI, Data Warehousing, Analytics etc. There are many improvements and new features added to the SQL Server Management Studio too. Microsoft has announced to release SQL Server 2016 on 1st of June 2016, around 13 days more. Once it’s available you can download and checkout the features. As announced earlier, the developer edition will also be free along with Express edition as usual. Now, if you are somebody who can’t wait for another 13 days period (like me 😉 ) then let’s see what are the options available with us. As you might already know, SQL Server 2016 RC3 (Release Candidate) is available for download, you can just download and install it in your machine. But for just 13 days,  the better option is to create a free Azure account with promotional $200 credit and checkout the features without harming to your existing system. Hopefully, I have clarified on the “Expiry Date” note.

The process is very clear and simple.

  1. Open azure.microsoft.com
  2. Following screen should appear
    1
    You can see $200 credit offer which will be valid for 30 days. Click Start now.
  3. You will be redirected to the Azure login page. Provide your existing Microsoft account details or create a new one using Sign up now.
  4. Once you are logged in, either click on Portal or just visit portal.azure.com
  5. You should be presented with something like following screenshot. Right side is your dashboard which you can customize as per your need and the left menu lists out the broad categories of Azure service offered. Click on the Virtual machines.
    2
  6. Click Add.
    3
  7. Find SQL Server in the list and click on it.
    4
  8. As you march forward, you’ll notice that further settings are getting opened as a new panel on the right side. Find SQL Server 2016 RC3 Evaluation on Windows Server 2012 R2 towards the bottom of the list and click on it.
    5.png
  9. Just click on the Create button for now.
    6.png
  10. You need to provide some more details about your virtual machine. Go ahead with filling Basic settings first. Note that I have used Azure Pass as subscription here but you will have the free trial option if you have signed up for free trial, otherwise your existing subscriptions.
    8
  11. At next step, you need to select the size which lists out the hardware details for each of the option. Choose a size from the recommended category only as these are the options which meets the hardware requirement specification that is published by the publisher (Microsoft in our case).
    NOTE: Prices mentioned in these options will be charged from your subscription account. If you are using Azure Pass or Free Trial, you can use those subscriptions to purchase this. For Free Trial, make sure not to agree for paying from your credit card. The subscription days should be truncated as per your balance in the subscription.
    9
  12. Next, you can customize various other settings such as Storage, Network etc. or just click OK to navigate to next step.
    10.png
  13. Then few more settings for your SQL Server instance like whether you want to use it inside VM, Virtual Network or available publicly over internet etc. You can customize according to your need or just click OK.
    11.png
  14. Then it will run a final validation and clicking on OK should start the deployment. You will be redirected the dashboard and you can see the progress there.
    12
  15. Once deployment is completed, you will be notified with a message “Deployment Succeeded”. Just copy the public IP from your virtual machine details and do the RDC (Remote Desktop Connection)
  16. Your VM is now ready with SQL Server 2016 RC3 installed in it. Type “SSMS” in the start screen and pin it to Task Bar or Start Screen and start using it.

That’s all for now. In my next blog posts, I will share some interesting features that are introduced with SQL Server 2016. Stay Tuned !

Thanks for reading 🙂
 

 

 

 

 

 

 

 

Tip-3: Exciting SSMS features derived from Visual Studio


In this series of posts, we’ll see few great yet less known and under utilized tips/features those are there since earlier versions of Visual Studio and later made their way to SSMS (SQL Server Management Studio). I am sure, you’ll love these tips.
Tip 3: Transact SQL Code Snippet – Insert Code Snippet

 

Have you used code snippet feature in Visual Studio? Aren’t they very useful?

What are code snippets?
Code Snippets are reusable code templates those help to write code faster.

What are Transact SQL code snippets?
“A Transact-SQL code snippet is a template containing the basic structure of a Transact-SQL statement or block. You can use snippets as a starting point when adding statements in the Database Engine Query Editor. You can insert the pre-defined snippets supplied with SQL Server, or create your own.”MSDN

We’ll discuss about how we can create a Transact SQL code snippet of our own on a later blog post. For now, we’ll see how we can use them.

You can do it either way-801800

  • Right click on the query window and click Insert Snippet…
  • Using keyboard shortcuts CTRL+K, CTRL+X802

Then choose the appropriate category and then the appropriate template to insert it in to your query window.

 

Hopefully, you liked this tips. Please stay tuned to learn few more exciting tips in this series. Will love to hear from you. Thanks for reading 🙂

 

Tip-2: Exciting SSMS features derived from Visual Studio


In this series of posts, we’ll see few great yet less known and under utilized tips/features those are there since earlier versions of Visual Studio and later made their way to SSMS (SQL Server Management Studio). I am sure, you’ll love these tips.


Tip 1: Clipboard Ring – Copy Paste rediscovered
Tip 2: Box Selection – Select and Edit text easily

90Have you ever imagined to select a particular portion of the text or rectangular region of the text from multiple rows like the figure -1 ?
Box selection?
Why I need that ? Oh ! Believe me, many times while executing queries for various tests you’ll need it.

Is this really possible?
Sad, with traditional horizontal selection system you can’t do that.

If you are known to Visual Studio 2010 (release: 12 April 2010) or newer environment, probably you already know this. Since Eclipse 3.5 (release: 24 June 2009), this facility is there. If you have SQL Server 2014 (not sure about SQL Server 2012), you have the power to do columnar selection also known as box selection.

box-selectionYou can do it either way-

  • Holding down the ALT key and dragging mouse to select text as we do for normal selection.
  • Using keyboard shortcuts ALT + SHIFT + ARROW KEYS

You can do following actions through Box Selection

  • Copy : Copy rectangular region/box of text
  • Text Insertion: Select the box and start typing to replace the selection with typed in new text on every selected line.
  • Paste:
    • Single Copy – Box Paste: On box selection pasting some text will be applied to every line
    • Box Copy – Box Paste: Paste contents of one box selection to another box selection
    • Box Copy – Single Paste: Little weird though. Check what is happening !
  • Zero-Length boxes: Vertical selection of zero characters wide to create a multi-line insertion point for new or copied text. To do this, just hold down the ALT key and keep pressing down/up key till the length you want.  This will create a multi-line insertion point.

 

Hopefully, you liked this tips. Please stay tuned to learn few more exciting tips in this series. Will love to hear from you. Thanks for reading 🙂

 

SQL Server: Custom Sorting


AscDescThis is one of the many custom sorting we do with SQL queries. In few cases we may need to sort all records of a result set in a particular order except one specific row containing some specific value, which we may want to keep on the top of all other rows or we may like to move it to the bottom. Now, let’s relate this to few real time scenarios.

Scenario-1:

Sometimes we need an element to be placed at bottom in the list. For example, a Dropdownlist containing educational qualifications may contain  an item with text as “Other”. While showing all these educational qualifications from database we may need this particular element (i.e, “Other”) to move to the bottom in the list. If we try to sort this list in the SELECT query by normal ORDER BY keyword (like ORDER BY Qualification) then we may not get the desired sequence.

To get the desired result we need to use CASE WHEN keyword along with ORDER BY. Let’s illustrate this with following example-

Simple ORDER BY :


SELECT * FROM
(
 SELECT 'I.Sc.' AS Qualification
 UNION
 SELECT 'B.Sc.' AS Qualification
 UNION
 SELECT 'Other' AS Qualification
 UNION
 SELECT 'P.G.' AS Qualification
 UNION
 SELECT 'MCA' AS Qualification
 UNION
 SELECT 'DCA' AS Qualification
 UNION
 SELECT 'P.H.D' AS Qualification
) AS TBL
ORDER BY Qualification

OUTPUT:

5

ORDER BY with CASE WHEN :


SELECT * FROM
(
 SELECT 'I.Sc.' AS Qualification
 UNION
 SELECT 'B.Sc.' AS Qualification
 UNION
 SELECT 'Other' AS Qualification
 UNION
 SELECT 'P.G.' AS Qualification
 UNION
 SELECT 'MCA' AS Qualification
 UNION
 SELECT 'DCA' AS Qualification
 UNION
 SELECT 'P.H.D' AS Qualification
) AS TBL
ORDER BY CASE WHEN Qualification='Other' 
THEN 'ZZZZZ' ELSE Qualification END

OUTPUT:

6

EXPLAINATION:

Here we have set the value of item “Other” as “ZZZZZ” while the sorting is being done. Generally “ZZZZZ” will be the last item in any real world list and hence will move to the last.

Scenario 2:

In some other cases we may need the desired item to be moved to top of the result set. An example for this situation can be, a Dropdownlist containing items for Book Category. The list may have an item with text “General” , which is meant for those books whose category is not clearly known. Let’s assume that most of the books are supposed to belong this category. Then we have to place this item in the first index of the Dropdownlist so that it will be easier for user to pick the item. Now, we need a little modification in the ORDER BY clause as follows.


ORDER BY CASE WHEN Qualification='General' THEN NULL ELSE Qualification END

I’ll love to hear from you.

Thanks

You may also like :

Dynamic ORDER BY in SQL Server

CodeProject

ASP.Net : DropDownList containing category – sub category hierarchy


Introduction:

This article will provide us a way to embed both categories and sub-categories hierarchy in a single DropDownList exactly same as the image on the left.

I searched the web for this but did not find exactly what I needed. So, I planned to write my own logic for this. If somebody has a better idea then I really appreciate and request to share.

Description:

Lets take an example where we need to show states and districts of India in a single DropDownList with their hierarchy. Here state will behave as category and districts can be considered as sub-categories.

In DB part we need two tables i.e, States and Districts may be like following structure.

  • State       –> (StateId int pk, StateName varchar(50))
  • Districts –> (DistrictId int pk, StateId int fk, DistrictName varchar(50))

Or, this script may help you creating the tables


CREATE TABLE [dbo].[States](
 [StateId] [int] IDENTITY(1,1) NOT NULL,
 [StateName] [varchar](50) NULL,
 CONSTRAINT [PK_States] PRIMARY KEY CLUSTERED
(
 [StateId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

CREATE TABLE [dbo].[Districts](
 [DistrictId] [int] IDENTITY(1,1) NOT NULL,
 [StateId] [int] NOT NULL,
 [DistrictName] [varchar](50) NULL,
 CONSTRAINT [PK_Districts] PRIMARY KEY CLUSTERED
(
 [DistrictId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[Districts] WITH CHECK ADD CONSTRAINT [FK_Districts_States1] FOREIGN KEY([StateId])
REFERENCES [dbo].[States] ([StateId])
GO

ALTER TABLE [dbo].[Districts] CHECK CONSTRAINT [FK_Districts_States1]
GO

Now insert some sample data or you can execute this script.


/****** Object: Table [dbo].[States] ******/
SET IDENTITY_INSERT [dbo].[States] ON
INSERT [dbo].[States] ([StateId], [StateName]) VALUES (1, N'Odisha')
INSERT [dbo].[States] ([StateId], [StateName]) VALUES (2, N'West Bengal')
INSERT [dbo].[States] ([StateId], [StateName]) VALUES (3, N'Bihar')
INSERT [dbo].[States] ([StateId], [StateName]) VALUES (4, N'Jharkhand')
SET IDENTITY_INSERT [dbo].[States] OFF
/****** Object: Table [dbo].[Districts] ******/
SET IDENTITY_INSERT [dbo].[Districts] ON
INSERT [dbo].[Districts] ([DistrictId], [StateId], [DistrictName]) VALUES (1, 1, N'Mayurbhanj')
INSERT [dbo].[Districts] ([DistrictId], [StateId], [DistrictName]) VALUES (2, 1, N'Keonjhar')
INSERT [dbo].[Districts] ([DistrictId], [StateId], [DistrictName]) VALUES (3, 1, N'Khorda')
INSERT [dbo].[Districts] ([DistrictId], [StateId], [DistrictName]) VALUES (4, 1, N'Balasore')
INSERT [dbo].[Districts] ([DistrictId], [StateId], [DistrictName]) VALUES (5, 2, N'Hoogly')
INSERT [dbo].[Districts] ([DistrictId], [StateId], [DistrictName]) VALUES (6, 2, N'Howrah')
INSERT [dbo].[Districts] ([DistrictId], [StateId], [DistrictName]) VALUES (7, 2, N'Jalpaiguri')
INSERT [dbo].[Districts] ([DistrictId], [StateId], [DistrictName]) VALUES (8, 3, N'Aurangabad')
INSERT [dbo].[Districts] ([DistrictId], [StateId], [DistrictName]) VALUES (9, 3, N'Patna')
INSERT [dbo].[Districts] ([DistrictId], [StateId], [DistrictName]) VALUES (10, 4, N'Bokaro')
SET IDENTITY_INSERT [dbo].[Districts] OFF

Place a DropDownList in your page.


<asp:DropDownList ID="drpDistricts" runat="server"></asp:DropDownList>

Lastly do the coding required to bind the DropDownList. Things to remember are we have a single DropDownList and it should be meant for choosing a sub-category(District), which means categories should be disabled for clicking. Although there may be several ways like looping the sub-categories and inserting categories in-between etc. but, I personally prefer to query both of them in a single query.


SELECT '- '+DistrictName AS TextField,DistrictId AS ValueField,StateId FROM DBO.Districts UNION SELECT UPPER(StateName),0,StateId FROM dbo.States ORDER BY StateId,DistrictId

Put DataTextField =”TextField” and DataValueField=”ValueField”

Now, lets do the coding to disable the categories (States).



foreach (ListItem item in drpDistricts.Items)
 {
 if (item.Value.ToString() == "0")
 {
 item.Attributes.Add("Style", "color:maroon");
 item.Attributes.Add("Disabled", "true");
 }
 }

That’s all.
Thanks for reading.
Feedback or suggestion are most welcome.