Tuesday, June 28, 2016

SQL Server 2016 is now available, It’s time to Install !!

Microsoft has launched the SQL Server 2016. We can download and install it from here link. However, when we start installation of SQL Server 2016, we can see some installation changes there. Prior to SQL Server 2016, “SQL Server Management Studio (SSMS)” was a part of the feature list of main SQL Server installation (which starts after clicking on “New SQL Server stand-alone installation or add features to an existing installation“) but now it is available on a separate install. Below is the screen shot of the feature selection list of SQL Server 2016 and SQL Server 2014:


                           SQL Server 2016 Feature list 2016 vs SQL Server 2014 Feature List


In above image, we can see that the management tools was a feature of the core SQL Server installation process in SQL Server 2014 (right side image) but in SQL Server 2016 (left side image), it is not available in the feature list of SQL Server installation process. So, even if we will do a full installation of SQL Server 2016 by selecting all features, we would not be able to get SQL Server Management Studio installed on our machine.

Let’s have a look on the first screen of SQL Server 2016 and SQL Server 2014 installation process which comes after opening the setup.exe file:


               SQL Server 2016 vs SQL Server 2014 Installation – First screen comparison

In this image we can see that the SQL Server Management Studio is now available on a separate line in SQL Server 2016. We can download and install it from here.


Once we click on the above link, we will get a web page with a download link to SQL Server Management Studio (SSMS)


SSMS 2016 installation
                                          SSMS 2016 installation


We can download and install the “SSMS-Setup-ENU” file from here. Once the download gets completed, we can open the installer file & follow the steps to install SQL Server Management Studio on our machine.

SQL Server 2016 brought tons of new features which we can start exploring once we install it.


I hope my effort of making you aware on 'SQL Server 2016 is now available, It’s time to install SQL Server 2016' helped you in your journey of SSIS.  If you feel any queries, please post them as comments below. Also, your feedback will be well appreciated as it helps us to improve better and better every time.

Sunday, June 26, 2016

Sql Server: Difference between Row_Number, Rank, Dense_Rank

Difference between Row_Number, Rank, Dense_Rank
Syntax and use:

Row_Number 
Returns the sequential number of a row within a partition of a result set, starting at 1 for the first row in each partition.

ROW_NUMBER ( )     OVER ([<partition_by_clause>] <order_by_clause>)

Rank
Returns the rank of each row within the partition of a result set. The rank of a row is one plus the number of ranks that come before the row in question.

RANK ( )    OVER ([< partition_by_clause >] < order_by_clause >)

Dense_Rank
Returns the rank of rows within the partition of a result set, without any gaps in the ranking. The rank of a row is one plus the number of distinct ranks that come before the row in question.

DENSE_RANK ( )    OVER ([<partition_by_clause> ] < order_by_clause > )

NTILE 
Distributes the rows in an ordered partition into a specified number of groups. The groups are numbered, starting at one. For each row, NTILE returns the number of the group to which the row belongs.

NTILE (integer_expression) OVER ([<partition_by_clause>] < order_by_clause >)

Where
<partition_by_clause>
Divides the result set produced by the From clause into partitions to which the Row_Number/ Rank/ Dense_Rank/ Ntile function is applied.
<order_by_clause>
Determines the order in which the Row_Number/ Rank/ Dense_Rank/ Ntile values are applied to the rows in a partition. 

We will apply these function on the below customer product table CustProd.

name
Product
cust1
decoder
cust2
cable
cust1
cable
cust2
package
cust3
decoder
cust3
cable

Please see the below snapshot for understanding of these function through example




With partition by product and order by name,

When we use partition by product, then it divides the result on the basis of product, as there are three distinct products then there will be 3 partitions.

After partition, order by name is used, that means, in the partitions Row Number, Rank or Dense Rank will be assigned as per the order of name. Here in the below result we see that rank ,row number and dense rank, all are having same value, It’s because in each partition there are distinct name given, if name would have been repeated for the same product then those records will have same rank and dense rank, but row number would have been same as shown below.

When used order by product instead of name , then we see in the below result that, the Rank and dense Rank were 1, Because we did partition of result by product , that means there will be common product in each partition , and rank and dense rank will also be same for same product. 





hope my effort of making you aware on 'SCD Type 2, step by step' helped you in your journey of SSIS.  If you feel any queries, please post them as comments below. Also, your feedback will be well appreciated as it helps us to improve better and better every time.

SSIS: SCD Type 2, step by step


SCD Type 2, step by step:

Type 2 (historical attribute): when we need to maintain the history of records, whenever some particular column value changes. 
  
By considering Type2, I assume that the employee gets promotion, Job Title changes. In such case we need to maintain the history of the employee, that with which designation or job Title he joined, and when his designation or Job Title changed.
   
For making such scenario, I have created two tables; one is 'tmpPerson', which is created as follows by using Adventure Works database’s tables:

---Start-TmpPerson table ---
CREATE TABLE tmpPerson (EmpId int, Title varchar(10) NULL,FirstName varchar(20),MiddleName varchar(20),LastName varchar(20),JobTitle varchar(100),BirthDate Date,MaritalStatus char(1),Genderchar(1),NationalIDNumber varchar(20),CurrentFlag int , StartDate date,EndDate date DEFAULT ('2050-12-31'))
INSERT INTO tmpPerson (EmpId, Title,FirstName, MiddleName,LastName, JobTitle,BirthDate,MaritalStatus,Gender,NationalIDNumber,CurrentFlag, StartDate)
SELECT e.BusinessEntityID, per.Title,
       per.FirstName,
       per.MiddleName,
       per.LastName,
       e.JobTitle,
       e.BirthDate,
       e.MaritalStatus,
       e.Gender,
       e.NationalIDNumber,
       e.CurrentFlag,
       e.ModifiedDate
FROM HumanResources.Employee e
INNER JOIN Person.Person per one.BusinessEntityID=per.BusinessEntityID 
----End –TmpPerson table----

I have also created one table tmpPersonStage similar to tmpperson, which stores the records which are changed by changing any of the column value. For Type 2, I have made some changes in Job Title, as below:

---Start--tmpPersonStage table----
CREATE TABLE tmpPersonstage (EmpId int, Title varchar(10) NULL,FirstNamevarchar(20), MiddleName varchar(20),LastName varchar(20),JobTitle varchar(100),BirthDate Date,MaritalStatus char(1),Genderchar(1),NationalIDNumber varchar(20),CurrentFlag int , StartDate date,EndDate date DEFAULT ('2050-12-31'))
INSERT INTO tmpPersonstage (EmpId, Title,FirstName, MiddleName,LastName, JobTitle,BirthDate,MaritalStatus,Gender,NationalIDNumber,CurrentFlag, StartDate,EndDate)
SELECT EmpId,
       Title,
       FirstName,
       MiddleName,
       LastName,
       JobTitle,
       BirthDate,
       MaritalStatus,
       Gender,
       NationalIDNumber,
       CurrentFlag,
       StartDate,
       EndDate
FROM dbo.tmpperson
WHERE empid IN (3,
                6,
                8,
                58,
                49) ----changes for type 2

UPDATE tmpPersonstage
 SET JobTitle ='Design Engineer' WHERE EmpId=3
UPDATE tmpPersonstage
 SET JobTitle ='Production Technician - WC10' WHERE EmpId=6
UPDATE tmpPersonstage
 SET JobTitle ='Production Technician - WC50' WHERE EmpId=8
UPDATE tmpPersonstage
  SET JobTitle ='Research and Development Engineer' whereEmpId=49
UPDATE tmpPersonstage
SET JobTitle ='Engineering Manager',
      LastName='Keill' whereEmpId=58 ---End---tmpPersonstage table -------

In the above script, I have updated the JobTitle of some employees, for which initially the job titles in tmpPerson table were as below
 

After preparing the initial steps for the scenario, let’s move to SSIS package to apply SCD transformation. The below steps are the implementation of SCD Type 2

Step 1:
Drag and drop one Data Flow Task in the Control flow tabs.


And name this DFT as ‘scd type 2’
Step2:
Go to Data Flow tab, Drag and drop OleDB Source and Slowly Changing Dimension transformation from Data flow transformations.
Step 3:
Configure OleDB source, here the source will be the table which has changed or new records, and for which history will be maintained, or inserted in the main table. In our scenario the source table is ‘tmpPersonStage’, which keeps some updated and new records for updated records history will be maintained and new records will be inserted into the main table ‘tmpPerson’




Step 4: 
Configure the SCD transformation; double click on SCD transformation, one wizard will open as below:

Click on next and create either new connection or already created connection to AdventureWorks2008, and while mapping the input columns to dimension column, at least one column need to be mentioned as ‘Business Key’.
  
Business key is the column on the basis of which the updation or insertion into ‘tmpPerson’ will be made. Here we have mentioned EMPID as BusinessKey, so if EmpId of input table ‘tmpPersonStage’ is found in ‘tmpPerson’table then the respective row  in ‘tmpPerson’ will be updated with the changes  from ‘tmpPersonStage’ input table 


Click on next. In the below window, change type need to be mentioned on certain column. Change Type can be ‘Fixed attribute’, ‘Changing attribute’ and ‘Historical Attribute’

As in this scenario is based on Historical attribute we will select some columns for which history will be maintained if found modified and new record will be inserted.


Go to Next and select "Use single column to show current expire records" option 

As in ‘tmpPerson’, we have both the option available one through ‘CurrentFlag’ column and another through ‘Startdate’ and ‘EndDate’.
  
We can opt for first option and use column ‘CurrentFlag’ as the indicator of the current record. By this the updated record will be inserted with ‘CurrentFlag’ value as ‘1’ and the records which became old for the respective ‘EmpID’ the ‘CurrentFlag’ will be set to ‘0’.
  
And if we opt for second option then we need to update the old record’s EndDate with ‘GetDate ()’ and need to insert the updated record with ‘StartDate’ as ‘GetDate ()’. For new records, we need to use StartDate as ‘GetDate ()’ .
  
Through this post, I am selecting first option, and I will be using ‘CurrentFlag’ Column to show the current record.

Go to next and don't select  ‘Enable inferred member support’, as we don’t require it for this scenario.
Click next, which displays New Records, other Outputs(Historical Attribute Output), which means there will be two arrows from SCD, one will take the new records, another will take the records for which history needs to be maintained.
Click next and Finish the wizard. As we finish the wizard we see some transformation like ‘OleDb command’ and ‘Insert Destination’ are automatically created.
  
OLEDB command will update the ‘CurrentFlag’ column of old records and will set it to ‘0’ for the ‘EmpID’ for which records are updated. These all will be union all with all new records along with the new updated records having ‘CurrentFlag’ 1 and will be inserted into the main table ‘tmpPerson’


Step 5:
Step 5.a:

Double click on Derived Column, and we see that one column with ‘Replace Currentflag’ has been derived from current flag and the value is set to ‘0’.


Step 5.b:
Double click on Oledb command, and it will be configured automatically as below. The connection will be automatically configured to table ‘tmpPerson’ of Adventure works 2008, in which the records will be updated.
Also when clicked on Component Properties tab then the update command is also configure, which we can see on string value editor.

The update command updates Current Flag to 0 on the basis of EmpId , and Current Flag(value 1)



In column mapping tab the parameter are mapped with the columns mentioned in query. These Parameters contain the Replace Current Flag column from derived transformation and EmpID from SCD

Step 5.c:

When clicked on next Derived Column transformation, the same Current Flag column is derived column of Replace Current flag, and set it to 1, for new and updated records.
Step 5.d:

Insert Destination will also be configured automatically, to insert the new records coming from SCD to tmpPerson table. 


Step 7: 
Execute the DFT 
After execution, we can see that, we updated 5 records in the tmppersonstage table, which are inserted into tmpperson table, along with the old records current flag updated as 0. As there was no new records, so no row is inserted into tmpperson.



I hope my effort of making you aware on 'SCD Type 2, step by step' helped you in your journey of SSIS.  If you feel any queries, please post them as comments below. Also, your feedback will be well appreciated as it helps us to improve better and better every time.


Saturday, June 25, 2016

SQL Server Fundamentals

SQL Server is a collection of Services, Applications and libraries

Services

·         Database Engine
·         Integration services
·         Reporting services
·         Analysis services
·         SQL Server Compact

Applications

To talk to SQL Server these r the ways,
·         SSMS
·         SQLCmd
·         PowerShell
·         SQLServer Profiler
·         Tuning Advisor

Libraries

·         SMO(SQLServer Management Objects )
·         LINQ to SQL
·         ADO.NET
SMO contains all the database objects , table objects…etc everything is in object format.SSMSis a GUI editor to work on SQLServer.SQLCmd is command line editor to work on SQLServer.Toget the details of the sqlserver need to use SQLcmd /?as shown in below fig,

Fig-SQL Cmd

Working with SQLCmd

All sqlcmd commands, except GO, must be prefixed by a colon (:).For more info on SQLCmd visit SQLServer Books online from SSMS.

Fig-Working Wth Sql Cmd(Connect to the sqlserver database and write T-SQL queries as shown above)

Working with SQL Powershell


Fig-SQL PowerShell

Fig-SQL PowerShell(• In the above screens SQL PS is used to access the sqlserver databases , tables and to get the info of that tables)

LINQ to SQL

LINQ is a new feature in c# to get data or to do manipulation in sqlserver database.LINQ syntax is similar to T-sql query syntax bit differs.We can u LINQPad editor work on with LINQ to SQL.

The above LINQ query gives the similar data from two different tables, just like joins in t-SQL.Its in LINQPad editor, the same syntax v can use in VS editor also using   C#


I hope my effort of making you aware on new way of SQL Server Fundamentals helped you in your journey of SQL. If you feel any queries, please post them as comments below. Also, your feedback will be well appreciated as it helps us to improve better and better every time.