use hos IfExists(Select1From tempdb.dbo.sysobjects where Name ='##temp_UC_CarDTL'And Xtype ='U') droptable ##temp_UC_CarDTL go SelectIdentity(int,1,1) as ID,*into ##temp_UC_CarDTL From UC_CardTL go IfExists(Select1From sysobjects where Name ='UC_CARDTL_Temp_ForImport'And Xtype ='U') droptable UC_CARDTL_Temp_ForImport go Select*into UC_CARDTL_Temp_ForImport From ##temp_UC_CarDTL Where ID In ( SelectMax(ID) From ##temp_UC_CarDTL Where1=1GroupBy CarName,CarID,Class HavingCount(*) >1 ) AlterTable[UC_CARDTL_Temp_ForImport]DropColumn[ID] go DropTable UC_CARDTL go sp_rename 'UC_CARDTL_Temp_ForImport','UC_CARDTL'