MS SQL Server

ByHariharan Rajendran

Latest Update on SQL Server Management Studio

This simple post highlights the top level updates on components in SQL Server Management Studio.

The latest release of SQL Server Management Studio v17 RC3 has updates on below section compare than the earlier version.

  1. Microsoft SQL Server Management Studio
  2. Microsoft Analysis Services Client Tools

ByHariharan Rajendran

SQL Server Drop Database User

This blog post explains the issue that we usually face while dropping the users from the database and how can we resolve the issue.

We can create a user for database easily by any of the following user types,

  1. SQL User with login
  2. SQL User without login
  3. User mapped to a certificate
  4. User mapped to a asymmetric key
  5. Windows User

If we want to drop the user then we can write drop statement but it will throw a below error in case the user owned any of the schemas.

Msg 15138, Level 16, State 1, Line 20

The database principal owns a schema in the database, and cannot be dropped.

To resolve the issue, Follow the below steps.

Step 1: Check the schema that owned by the user.

SELECT *

FROM sys.schemas s

WHERE s.principal_id = USER_ID(‘Username’)

Step 2: Change the schema back to dbo.

ALTER AUTHORIZATION ON SCHEMA::<schemaname> TO dbo;

Step 3: Drop the user

DROP USER <username>

ByHariharan Rajendran

SQL Query – Network Bandwidth Optimization

This article explains that how can we improve the performance when we access the SQL Query via network.

Scenario: I have taken SQL Server installed on Virtual Machine with 7GB of RAM and accessing the SQL Server database from local SQL Server management studio.

Step 1: Up and Run VM with SQL Server installed.

Step 2: Enable the DNS name of the Virtual Machine.

Step 3: Enable the TCP port with 1433 in portal.

Step 4: Create TCP port inside virtual Machine. Type WF.msc in command prompt. Right click on Inbound Rules and choose New.

Step 5: Make sure SQL Server services are up and running.

Step 6: Enable the SQL Server as mixed mode authentication.

Step 7: Create a new user login and check.

Step 8: Access the database from local SSMS.

Performance Improvement with NitroAccelerator

Step 1:

Install the “NitroAccelerator” in local machine, http://nitrosphere.com/trial/?ref=83095

Step 2: Run the query in local SSMS without Nitro Accelerator.

Step 3: Start the accelerator and test the query performance.

ByHariharan Rajendran

Generate SELECT Statement

This article explains that how to generate SELECT statement with all the table columns.

In the Production environment, most of the tables will have more than 100 columns. If those tables are used in a report with all the columns then it will be difficult to type all the columns.

To ease the process, we can generate the SELECT statement and can use that for any purpose.

Use the below code and pass the table which you want to use in the SELECT statement.

In this example, I used FactInternetSales table.

—–Code——-

Declare @Column varchar(500), @tableName varchar(25)

SET @tableName =’FactInternetSales’

SET @Column = ”

select @Column = @Column + Column_Name + ‘, ‘ from [AdventureWorksDW2014].INFORMATION_SCHEMA.COLUMNS

where table_name=@tableName

select ‘SELECT ‘ + SUBSTRING(@Column, 0, LEN(@Column)) + ‘ From ‘+@tableName as SELECTScript

—–Code———-

SQL1

Copy and paste the result and execute the script.

SQL2

ByHariharan Rajendran

Avoid Using Dataset Filters in SSRS

If you are Microsoft SQL Server Report Developer, then you have a chance to notice the dataset properties as it is the place where we supply the data subsets to the report.

Let us understand the filters in SSRS first and analyse why dataset filters should be avoided.

We have three different main places to filter the data in SSRS

  1. T-SQL Script
  2. Dataset Filters
  3. Run-time Filter using Parameters

Filter in T-SQL Script

When we understand the business requirements, need to categories what are the filters static and dynamic.

Always use the static filters in T-SQL script. Dynamic filters can be done through parameters but still need to add the filter in T-SQL Script with parameters.

For example,

If you want to filter country values as the US on your country table.

SELECT State, City, Country FROM dbo.COUNTRY

WHERE Country=’US’

Note: In this case, we are restricting data in database server itself.

Dataset Filters

You can filter in SSRS report dataset.

Go to Dataset -> Filters

RD

Note: In this case, we are taking all the data to report server and filtering the data in the report.

Run-time filter using Parameter

It is a special kind of filter because this will be the requirement from business in most of the cases.

First 2 filters won’t be visible to end users but this filter will be visible and user needs to filter the value while running the report.

This can be achieved by adding the parameter in T-SQL script,

SELECT State, City, Country FROM dbo.COUNTRY

WHERE Country=@Country

RD2

Why should we avoid dataset filters?

The main reason is to improve the performance of the report.

In case 1 (Filter in  T-SQL Script), we are filtering the data in database layer itself. It means we are taking only required data to the reporting layer and showing the result. The performance of the report will be good as it is processing only fewer records.

In case 2 (Dataset filter), we are taking all the records from table database and filtering in the report server. It means report server has to make process and filter the data and show the result. We are giving burden to report server so it will affect the performance.

 

ByHariharan Rajendran

T-SQL Basics – Day 1

If you are reading this article then you are looking an very easy way to understand the T-SQL Scripts.

Let us learn T-SQL with a scenario, consider you have a sheet with some employee information. Your boss asking questions from that sheet and you need to answer him. In simple, your task is “Read from Sheet”.

Convert the general words into technical terms,

Sheet – Table (Employee) in a Database

Questions – T-SQL Statement

Answer – Output of T-SQL

Read – Select

Read from Sheet – Select from Table

If you are familiar with above terms then you can easily learn T-SQL Statements.

Let’s start,

Boss: What are the information available of an employee?

You: Read all the field name from Sheet

Tab1

T-SQL: SELECT * FROM Employee

Tab17

Note: “*” denotes all the fields from a table.

Boss: Do we have employee Name, Age & Country?

You: Yes

Tab2

T-SQL: SELECT Name, Age, Country FROM Employee

Tab18

Boss: What is the age of Richard? Or what is the country name of Richard?

You: 42 or UK

Tab3

T-SQL: SELECT * FROM Employee Where Name=’Richard’

Tab13

Note: Need to use WHERE keyword when we say specific data.

Boss: Who are all crossed age 40?

You: Read from sheet

Tab4

T-SQL: SELECT * FROM Employee WHERE Age > 40

Tab14

Note: Need to use WHERE keyword when we say specific data.

Boss: Whose names start with letter K?

You: Read from sheet

Tab5

T-SQL: SELECT * FROM Employee WHERE Name LIKE ‘K_%’

Tab15

Note: Need to use WHERE keyword when we say specific data. Use LIKE keyword when you want to play with the letters or pattern in a value. There are few other patterns available for LIKE which will discuss in the next article.

Boss: Convert the values of a Marital Status column into detailed values.

You: Add a new column

Tab6

T-SQL: SELECT *, CASE WHEN [Marital Status] =’M’ THEN ‘Married’ ELSE ‘UnMarried’ End MStatus FROM Employee  

Tab16

Note: Use “CASE WHEN XXXX THEN XXXXX ELSE XXXXX END” Syntax when you want derive something. We can discuss the detailed version above syntax in next article.

 

ByHariharan Rajendran

Limitations of Custom Reports in SQL Server Management Studio

I have discussed Custom Reports in my earlier post, this post highlights the limitation of custom reports in SQL Server Management studio.

Microsoft provided few custom reports for R services. When I use those custom reports, SQL Server Management Studio has stopped working for all the reports.

SSMS_Stopped

Then I started to check the reports in visual studio. When I check the reports, it used some of the expressions which SQL Server Management not support.

Limitations,

  1. The dataset should be embedded in the report.
  2. Expressions – Regular Expression like sum, count will work but report specific expressions will not be supported, it could be an expression to open drill down reports and etc.
  3. Parameters – If the report has a parameter that should have a default value.

Download the modified version of R Services Custom Reports.

Download

ByHariharan Rajendran

SQL Server vNext vs SQL Server – Installation Features

Recently, Microsoft released SQL Server vNext which is a platform that gives us choices of development languages, data-types for on-premises and in the cloud.

It also opens up a channel to bring the power of SQL Server in Linux.

There are many features available in SQL Server vNext, check out Whats’s New in SQL Server vNext.

This post gives you the small comparison on SQL Server and SQL Server vNext in terms of installation.

Screenshot from SQL Server 2016,

SQLServerFeatures

Screenshot from SQL Server vNext,

vNext

As you can see, there is a new update on SQL Server Integration Services.

Integration Services Scale Out gives performance booster for package execution by distributing executions to multiple machines.

New Service account to support SSIS Scale Out Master and Worker

vNext_ServiceAccounts

Scale Out Master Configuration page,

vNext_ScaleOutMaster

Scale Out Worker Configuration page,

vNext_ScaleOutWorker

Start play with latest version of SQL Server vNext.