Use this space to put some text. Update this text in HTML

468x60 banner ad

Advertise with Us

Powered by Blogger.

Wednesday 11 May 2016

Deleting Duplicate Records in Sql Server


Sometimes when we don’t use primary or unique key in the SQL-Server table, then most of the chances to insert duplicate row into the SQL-Server table. It’s quite difficult task to delete the duplicate row from the SQL-Server table.

For the prevention of inserting duplicate rows into the SQL-Server table, we always use primary key or unique key in the SQL-Server table.

First Example:

Using the same ROW_NUMBER() function you can also easily delete the Duplicate Records present inside a table.
I have a [dbo].[tm_Employee] Table which is having duplicate records. I need to delete the duplicate records which are having both the First_name and Last_name same.

So, with the help of ROW_NUMBER()all the duplicate records present in the below table can be removed easily.

Below are the SQL-Server table Structure and Insert Query

CREATE TABLE [dbo].[tm_Employee](
      [First_name] [nvarchar](300) NULL,
      [Last_name] [nvarchar](300) NULL,
      [Salary] [decimal](20, 2) NULL,
      [Joining_date] [datetime] NULL,
      [Department] [nvarchar](200) NULL,
      [City] [nvarchar](250) NULL,
      [Age] [int] NULL
) ON [PRIMARY]


INSERT [dbo].[tm_Employee] ([First_name], [Last_name], [Salary], [Joining_date], [Department], [City],[Age]) VALUES ('Deepak', 'Kumar', 5000.00, 1905-06-07, 'Banking', 'Delhi',18)
INSERT [dbo].[tm_Employee] ([First_name], [Last_name], [Salary], [Joining_date], [Department], [City],[Age]) VALUES ('Deepak', 'Kumar', 5000.00, 1905-06-07, 'Banking', 'Delhi',18)
INSERT [dbo].[tm_Employee] ([First_name], [Last_name], [Salary], [Joining_date], [Department], [City],[Age]) VALUES ('Deepak', 'Singh', 8000.00, 1894-06-28, 'Insurance', 'Delhi',25)
INSERT [dbo].[tm_Employee] ([First_name], [Last_name], [Salary], [Joining_date], [Department], [City],[Age]) VALUES ('Satish', 'Singh', 7000.00, 1894-06-27, 'Banking','Mumbai',28)
INSERT [dbo].[tm_Employee] ([First_name], [Last_name], [Salary], [Joining_date], [Department], [City],[Age]) VALUES ('Satish', 'Singh', 7000.00, 1894-06-27, 'Banking','Mumbai',28)
INSERT [dbo].[tm_Employee] ([First_name], [Last_name], [Salary], [Joining_date], [Department], [City],[Age]) VALUES ('Satish', 'Rai', 12000.00, 1894-06-27, 'Insurance', 'Delhi',32)
INSERT [dbo].[tm_Employee] ([First_name], [Last_name], [Salary], [Joining_date], [Department], [City],[Age]) VALUES ('Pankaj', 'Kumar',6000.00, 1894-06-28, 'Services', 'kolkata',31)
INSERT [dbo].[tm_Employee] ([First_name], [Last_name], [Salary], [Joining_date], [Department], [City],[Age]) VALUES ('Pankaj', 'Singh', 6000.00, 1894-06-28, 'Services', 'Delhi',35)
INSERT [dbo].[tm_Employee] ([First_name], [Last_name], [Salary], [Joining_date], [Department], [City],[Age]) VALUES ('Pankaj', 'Kumar',6000.00, 1894-06-28, 'Services', 'kolkata',31)
INSERT [dbo].[tm_Employee] ([First_name], [Last_name], [Salary], [Joining_date], [Department], [City],[Age]) VALUES ('Abhishek', 'Kumar', 650000.00, 1894-06-28, 'Services', 'Delhi',24)
INSERT [dbo].[tm_Employee] ([First_name], [Last_name], [Salary], [Joining_date], [Department], [City],[Age]) VALUES ('Abhishek', 'Kumar', 650000.00, 1894-06-28, 'Services', 'Delhi',24)
INSERT [dbo].[tm_Employee] ([First_name], [Last_name], [Salary], [Joining_date], [Department], [City],[Age]) VALUES ('Abhishek', 'Kumar', 650000.00, 1894-06-28, 'Services', 'Delhi',24)

SELECT * FROM [dbo].[tm_Employee]



The query for Delete Duplicate Records:-

We can check duplicate record on the basis of  First_name and Last_name both are same then it means record has duplicate record.

Note: - If First_name and Last_name are not same means record has not duplicate record.

You can select below query simultaneously and then press execute query command.


WITH TempEmployee (First_name, Last_name, duplicateRecordCount)
AS
(
 SELECT
   First_name,
   Last_name,
   ROW_NUMBER()OVER(PARTITION BY First_name, Last_name ORDER BY First_name)
   AS duplicateRecordCount
 FROM [dbo].[tm_Employee]
)

DELETE
FROM TempEmployee
WHERE duplicateRecordCount > 1


TempEmployee is used for Temporary purpose; Instead of TempEmployee you can give any name as per convenance upto you.

After the execution of the above query, [dbo].[tm_Employee] table will have the following records.


SELECT * FROM [dbo].[tm_Employee]




Note: Row_Number() function is present in SQL Server 2005 and in later version(s)



Second Example:

If your table has identity column or auto increament column. Then you can have to delete the duplicate record by the following sub-query.

Here I have taken [dbo].[tm_Employee1] table with the [ID] as identity column.

Below are the Table structure and Insert query:

CREATE TABLE [dbo].[tm_Employee1]
(
      [ID] INT IDENTITY(1,1) NOT NULL,
      [First_name] [nvarchar](300) NULL,
      [Last_name] [nvarchar](300) NULL,
      [Salary] [decimal](20, 2) NULL,
      [Joining_date] [datetime] NULL,
      [Department] [nvarchar](200) NULL,
      [City] [nvarchar](250) NULL,
      [Age] [int] NULL
) ON [PRIMARY]


INSERT [dbo].[tm_Employee1] ([First_name], [Last_name], [Salary], [Joining_date], [Department], [City],[Age]) VALUES ('Deepak', 'Kumar', 5000.00, 1905-06-07, 'Banking', 'Delhi',18)
INSERT [dbo].[tm_Employee1] ([First_name], [Last_name], [Salary], [Joining_date], [Department], [City],[Age]) VALUES ('Deepak', 'Kumar', 5000.00, 1905-06-07, 'Banking', 'Delhi',18)
INSERT [dbo].[tm_Employee1] ([First_name], [Last_name], [Salary], [Joining_date], [Department], [City],[Age]) VALUES ('Deepak', 'Singh', 8000.00, 1894-06-28, 'Insurance', 'Delhi',25)
INSERT [dbo].[tm_Employee1] ([First_name], [Last_name], [Salary], [Joining_date], [Department], [City],[Age]) VALUES ('Satish', 'Singh', 7000.00, 1894-06-27, 'Banking','Mumbai',28)
INSERT [dbo].[tm_Employee1] ([First_name], [Last_name], [Salary], [Joining_date], [Department], [City],[Age]) VALUES ('Satish', 'Singh', 7000.00, 1894-06-27, 'Banking','Mumbai',28)
INSERT [dbo].[tm_Employee1] ([First_name], [Last_name], [Salary], [Joining_date], [Department], [City],[Age]) VALUES ('Satish', 'Rai', 12000.00, 1894-06-27, 'Insurance', 'Delhi',32)
INSERT [dbo].[tm_Employee1] ([First_name], [Last_name], [Salary], [Joining_date], [Department], [City],[Age]) VALUES ('Pankaj', 'Kumar',6000.00, 1894-06-28, 'Services', 'kolkata',31)
INSERT [dbo].[tm_Employee1] ([First_name], [Last_name], [Salary], [Joining_date], [Department], [City],[Age]) VALUES ('Pankaj', 'Singh', 6000.00, 1894-06-28, 'Services', 'Delhi',35)
INSERT [dbo].[tm_Employee1] ([First_name], [Last_name], [Salary], [Joining_date], [Department], [City],[Age]) VALUES ('Pankaj', 'Kumar',6000.00, 1894-06-28, 'Services', 'kolkata',31)
INSERT [dbo].[tm_Employee1] ([First_name], [Last_name], [Salary], [Joining_date], [Department], [City],[Age]) VALUES ('Abhishek', 'Kumar', 650000.00, 1894-06-28, 'Services', 'Delhi',24)
INSERT [dbo].[tm_Employee1] ([First_name], [Last_name], [Salary], [Joining_date], [Department], [City],[Age]) VALUES ('Abhishek', 'Kumar', 650000.00, 1894-06-28, 'Services', 'Delhi',24)
INSERT [dbo].[tm_Employee1] ([First_name], [Last_name], [Salary], [Joining_date], [Department], [City],[Age]) VALUES ('Abhishek', 'Kumar', 650000.00, 1894-06-28, 'Services', 'Delhi',24)

SELECT * FROM [dbo].[tm_Employee1]

--Selecting distinct records
SELECT * FROM [dbo].[tm_Employee1] E1
WHERE E1.ID = (SELECT MAX(ID) FROM [dbo].[tm_Employee1] E2
WHERE E2.First_name = E1.First_name AND E1.Last_name = E2.Last_name)

The query for Delete Duplicate Records:-

--Deleting duplicates
DELETE [dbo].[tm_Employee1]
WHERE ID < (SELECT MAX(ID) FROM [dbo].[tm_Employee1] E2
WHERE E2.First_name = tm_Employee1.First_name AND E2.Last_name = tm_Employee1.Last_name)

SELECT * FROM [dbo].[tm_Employee1]
 

Friday 6 May 2016

Ajax Updapanel Control with Triggers


When you are using simple asp.net application and you have to click on button, the hole page is postback to the server.

If we want to avoid this full postback of page and round trip to server we need to use ajax updatepanel control for partial page postback.

Ajax updatepanel is used to avoid full postback of the page, avoid refresh of the whole page content with postback. By using Ajax updatepanel we can refresh only required part of page instead of refreshing whole page.
UpdateMode Property
When an UpdatePanel control is not inside another UpdatePanel control, the panel is updated according to the settings of the UpdateMode and ChildAsTriggers properties, together with the collection of triggers.
By default updatepanel contains UpdateMode="Always" if we want to set it conditionally we need to change this property UpdateMode="Conditional"

If the UpdateMode property is set to Always, the UpdatePanel control's content is updated on every postback that originates from anywhere on the page.

ChildrenAsTriggers Property

If the UpdateMode property is set to Conditional

1.The postback is caused by a control that is defined as a trigger by using the Triggers property of the UpdatePanel control. the control explicitly triggers an update of the panel content.

2. The ChildAsTriggers property is set to true and a child control of the UpdatePanel control causes a postback.

3.A child control of a nested UpdatePanel control does not cause an update to the outer UpdatePanel control unless it is explicitly defined as a trigger
4.When an UpdatePanel control is inside another UpdatePanel control, the child panel is automatically updated when the parent panel is updated.
ContentTemplate is used to hold the content of the page means suppose we designed page with some controls we will place controls inside of the ContentTemplate

 


Example

.aspx Page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UpdatePanel.aspx.cs" Inherits="Admin_UpdatePanel" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdPnlTest" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <table width="30%" align="center" style="border: 1px solid black;" cellpadding="5"
                cellspacing="0" border="0">
                <tr>
                    <td colspan="4" class="clsError">
                        <br />
                    </td>
                </tr>
                <tr>
                    <td colspan="4" style="color: Green; font-family: Times New Roman; font-size: 14px;
                        font-weight: bold;">
                        <asp:Label ID="lblUpdatePanelResult" runat="server"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td colspan="4" class="clsError">
                        <br />
                    </td>
                </tr>
                <tr>
                    <td colspan="4" style="color: Blue;">
                    </td>
                </tr>
                <tr>
                    <td colspan="4" class="clsError">
                        <asp:Button ID="btnUpdatePanel" CssClass="CLSBUTTON" runat="server" Text="Update Panel"
                            OnClick="btnUpdatePanel_Click" />
                        &nbsp;
                    </td>
                </tr>
            </table>
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="btnUpdatePanel" EventName="Click" />
        </Triggers>
    </asp:UpdatePanel>
    <br />
    <br />
    <table width="30%" align="center" style="border: 1px solid black;" cellpadding="5"
        cellspacing="0" border="0">
        <tr>
            <td colspan="4" class="clsError">
                <br />
            </td>
        </tr>
        <tr>
            <td colspan="4" style="color: Green;">
            </td>
        </tr>
        <tr>
            <td colspan="4" class="clsError">
                <br />
            </td>
        </tr>
        <tr>
            <td colspan="4" style="color: Blue; font-family: Times New Roman; font-size: 14px;
                font-weight: bold;">
                <asp:Label ID="lblWithoutUpdatePanelResult" runat="server"></asp:Label>
            </td>
        </tr>
        <tr>
            <td colspan="4" class="clsError">
                <br />
            </td>
        </tr>
        <tr>
            <td colspan="4" class="clsError">
                &nbsp;
                <asp:Button ID="btnWithoutUpdatePanel" runat="server" CausesValidation="false" CssClass="CLSBUTTON"
                    Text="Without Update Panel" OnClick="btnWithoutUpdatePanel_Click" />
            </td>
        </tr>
    </table>
    </form>
</body>
</html>


.aspx.cs Page

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_UpdatePanel : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnUpdatePanel_Click(object sender, EventArgs e)
    {
        lblUpdatePanelResult.Text = "Using Update Panel Refreshed at " +DateTime.Now.ToString();
    }
    protected void btnWithoutUpdatePanel_Click(object sender, EventArgs e)
    {
        lblWithoutUpdatePanelResult.Text = "Without Using Update Panel Refreshed at " + DateTime.Now.ToString();
    }
}

Result: