how to delete a record which is duplicate code example
Example 1: delete duplicate rows
WITH CTE AS(
SELECT [col1], [col2], [col3], [col4], [col5], [col6], [col7],
RN = ROW_NUMBER()OVER(PARTITION BY col1 ORDER BY col1)
FROM dbo.Table1
)
DELETE FROM CTE WHERE RN > 1
Example 2: avoid duplicate record
alter procedure [dbo].[sp_InsertBankName]
(
@bankname as varchar(50)
,@banknamea as nvarchar(50)
)
as
begin
declare @bankid as varchar(10)
declare @bCount as int
select @bCount = count(bankname) from bankname where bankname = @bankname
if @bCount = 0
begin
select @bankid=isnull(max(convert(int,bankid)),0)+1 from bankname
insert into bankname
(bankid,bankname,banknamea)
values (@bankid,@bankname,@banknamea)
end
end