sql 删除重复记录没有大小关系时,重复值将如何处理呢? 下文就将为您介绍sql删除重复记录没有大小关系时重复值的处理方法,供您参考,希望对您有所启迪。
–> –> (roy)生成
if not object_id(tempdb..#t) is nulldrop table #tgocreate table #t([num] int,[Name] nvarchar(1))INSERT #tselect 1,na union allselect 1,na union allselect 1,na union allselect 2,nb union allselect 2,nbgo方法1:
if object_id(tempdb..#) is not nulldrop table #select distinct * into # From #t–排除重复记录结果集生成临时表#
truncate table #t–清空表
insert #t select * from # –把临时表#插入到表#t中–查看结果select * from #t
/*num name———– —-1 a2 b
(2 行受影响)#p#*/
–重新执行测试数据后用方法2方法2:
alter table #t add id int identity–新增标识列godelete a from #t a where exists(select 1 from #t where num=a.num and name=a.name and id>a.id)–只保留一条记录goalter table #t drop column id–删除标识列–查看结果select * from #t
/*num name———– —-1 a2 b
(2 行受影响)
–重新执行测试数据后用方法3方法3:
declare roy_cursor cursor local For#p#select count(1)-1,num,name from #t group by num,name having count(1)>1declare @con int,@num int,@name nvarchar(1)open roy_cursorfetch next from roy_cursor into @con,@num,@namewhile @@fetch_status=0beginset rowcount @con;delete #t where num=@num and name=@nameset rowcount 0;fetch next from roy_cursor into @con,@num,@nameendclose roy_cursordeallocate roy_cursor–查看结果select * from #t/*num name———– —-1 a2 b
(2 行受影响)*///利用存储过程
declare @max integer,@id integerdeclare cur_rows cursor local for select 主字段,count(*) from 表名 group by 主字段 having count(*) > 1open cur_rowsfetch cur_rows into @id,@maxwhile @@fetch_status=0beginselect @max = @max -1set rowcount @maxdelete from 表名 where 主字段 = @idfetch cur_rows into @id,@maXendclose cur_rowsset rowcount 0//使用函数
select distinct * into #tmp from tablenamedrop table tablenameselect * into tablename from #tmpdrop table #tmp
sql server数据库表中如何根据某个字段删除重复数据?
我用游标实现了你的功能。 你首先建立一张空表,和你的操作表一样的结构,但是要求是空表,没有任何内容,比如是tempReg2你把下面的代码拷贝到SQL查询分析器,稍作修改就行。 ※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※DECLARE Cursor_Title CURSOR FOR SELECT distinct title FROM RegMemberOPEN Cursor_Titledeclare @str varchar(50)FETCH NEXT FROM Cursor_Title Into @strWHILE @@FETCH_STATUS = 0BEGIN insert into tempReg2 select top 1 * from RegMember where title=@str FETCH NEXT FROM Cursor_Title Into @strENDCLOSE Cursor_TitleDEALLOCATE Cursor_Title※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※我用的表名是RegMember,重复的列名是title,所以这两个名称需要你替换一下。 别的可以不变。
sql delete语句删除重复

selectdistinct*intotemptablefromtableAtruncatetabletableAinsertintotableAselect*fromtemptable或者altertabletableAaddID_NEWintidentitygodeleteafromtableAawhereexists(select1fromtableAwhereName=_NEW>_NEW)goaltertabletableAdropcolumnID_NEW
用一条SQL语句查询出一张表中的重复信息,并且删除重复的数据,是数据不再重复?
delete from 表 as a where (select COUNT(*) from 表 where 比较字段=a.比较字段)>1 错了
发表评论