Monday, 19 March 2018

 Delete duplicate records from Table  in SQL

        Query:- Delete duplicate records from Table  in SQL


Create Table:


 create table EmpTable (empid int,name varchar(20))

Insert Record in to table


    insert into EmpTable values(1,'Mohan')
    insert into EmpTable values (1,'Mohan')
    insert into EmpTable values (2,'Sohan')
    insert into EmpTable values(3,'Gita')
    insert into EmpTable values(2,'Sohan')
    insert into EmpTable values (3,'Gita')  

Display Record from Table


select * from EmpTable 

Copy Unique record in temporary tmpTable  from EMpTable

select distinct * into #tmpTable From EmpTable 

Delete All Record from the EmpTable

       delete from EmpTable 

Insert record from tempTable into EmpTable and Drop Temporary Table

       insert into EmpTable 
       select * from #tmpTable drop table #tmpTable 

Display Record from Table

       select * from EmpTable 


No comments:

Post a Comment