sqlserver删除重复行
⑴ sql中如何删除重复数据
select
字段1,字段2,字段3
from
table
group
by
字段1,字段2,字段3
having
count(*)>1
用上边这句能找出所有重复的数据
字段1,2,3你替换成你表里的字段名,如果有更多字段的话,你就继续添加,最后group
by的时候不要忘记了
删除的时候要建立一个临时表
create
table
new_table
as
select
字段1,字段2,字段3
from
old_table
group
by
字段1,字段2,字段3;
然后删除原表数据
truncate
table
old_table;
然后把临时表数据反插回去
insert
into
new_table
select
*
from
old_table;
⑵ 如何删除 SQL Server 表中的重复行
Microsoft SQL Server 表不应该包含重复行和非唯一主键。为简洁起见,在本文中我们有时称主键为“键”或“PK”,但这始终表示“主键”。重复的 PK 违反了实体完整性,在关系系统中是不允许的。SQL Server 有各种强制执行实体完整性的机制,包括索引、唯一约束、主键约束和触发器。
尽管如此,在某些情况下还可能会出现重复的主键;如果出现此类情况,就必须清除重复主键。出现重复主键的情形之一是,在 SQL Server 外部的非关系数据中存在重复的 PK,在导入这些数据时没有强制执行 PK 唯一性。出现重复主键的另一种情形来自数据库设计错误,如没有对每张表强制执行实体完整性。
通常在尝试创建唯一索引时会发现重复的 PK,因为如果找到重复的键,唯一索引的创建即会中止,并且将显示以下消息:
Msg 1505, Level 16, State 1 Create unique index aborted on plicate key.
如果使用的是 SQL Server 2000 或 SQL Server 2005,则会收到以下错误消息:
Msg 1505, Level 16, State 1 CREATE UNIQUE INDEX terminated because a plicate key was found for object name '%.*ls' and index name '%.*ls'.The plicate key value is %ls.
本文讨论如何查找和删除表中重复的主键。但是,您应该仔细检查出现重复键的进程以避免重复出现。
⑶ SQL Server 2000 删除重复记录问题
你可以重新建立一个新表,然后将原表中的信息不重复的选出来,复制到新表中,再将原表删除,就ok了。
⑷ 如何删除 SQL Server 表中的重复行
一个最简单的方法,distinct去重复知道吧~用语句把所有去掉重复的记录查出来放进表A中,然后把表A的名字改成原来的,原来的删掉
⑸ sql server 2008怎么删去重复的数据
1、要有定位基准,也就是说,你的表必需要有一个不重复的键值,如果没有,请你给这个表加一个字段,将这个字段设为自增变量字段,建议为int类型,比如字段名可为“编码”。
2、查重复的数据:
select *from 表名 where 编码 in
(select 编码 from 表名 group by 编码 having count(1) >= 2) 3、删除所有有重复的记录:
delete from 表名 where
编码 in(select 编码 from 表名 group by 编码 having count(1) >= 2)4、删去重复的,只留下重复记录中编码最大的一条:
delete from 表名 where
编码 in(select 编码 from 表名 group by 编码 having count(1) >= 2)
and 编码 not in (select max(编码)from 表名 group by 编码 having count(1) >=2)
⑹ SQL Server中怎样可以从SELECT语句的结果集中删除重复行
在要删除的有重复数据中存在几种情况:
1.存在两条完全相同的纪录
这是最简单的一种情况,用关键字distinct就可以去掉。
example: select distinct * from table(表名) where (条件)
2.存在部分字段相同的纪录(有主键id即唯一键)
如果是这种情况的话用distinct是过滤不了的,这就要用到主键id的唯一性特点及group by分组
example:
select * from table where id in (select max(id) from table group by [去除重复的字段名列表,....])
3.没有唯一键ID
example:
select identity(int1,1) as id,* into newtable(临时表) from table
select * from newtable where id in (select max(id) from newtable group by [去除重复的字段名列表,....])
(6)sqlserver删除重复行扩展阅读:
SQL Server 是Microsoft 公司推出的关系型数据库管理系统。具有使用方便可伸缩性好与相关软件集成程度高等优点,可跨越从运行Microsoft Windows 98 的膝上型电脑到运行Microsoft Windows 2012 的大型多处理器的服务器等多种平台使用。
Microsoft SQL Server 是一个全面的数据库平台,使用集成的商业智能 (BI)工具提供了企业级的数据管理。Microsoft SQL Server数据库引擎为关系型数据和结构化数据提供了更安全可靠的存储功能,使您可以构建和管理用于业务的高可用和高性能的数据应用程序。
⑺ SQL如何删除重复的数据行
SQL Server删除重复行是我们最常见的操作之一,下面就为您介绍六种适合不同情况的SQL Server删除重复行的方法,供您参考。
1.如果有ID字段,就是具有唯一性的字段
delect table tableName where id not in ( select max(id) from table group by col1,col2,col3... )
group by 子句后跟的字段就是你用来判断重复的条件,如只有col1,那么只要col1字段内容相同即表示记录相同。
2. 如果是判断所有字段也可以这样 ,【对于表中的指定的字段的进行检查是否相同】
select * into #temp from tablename group by id1,id2,....
delete tablename
insert into table select * from #temp
drop table #temp
3. 首先去重复,再获取N*1条数据插入到临时表中,【对于表中的所有字段的进行检查是否相同】,再将原表的数据删除,然后将临时表的数据插入到原表,最后删除临时表。
select distinct * into #temp from tablename
delete tablename
go
insert tablename select * from #temp
go
drop table #temp
4. 没有ID的情况
select identity(int,1,1) as id,* into #temp from tabel
delect # where id not in (
select max(id) from # group by col1,col2,col3...)
delect table
inset into table(...)
select ..... from #temp
5. col1+','+col2+','...col5 联合主键
select * from table where col1+','+col2+','...col5 in (
select max(col1+','+col2+','...col5) from table
where having count(*)>1
group by col1,col2,col3,col4
)
group by 子句后跟的字段就是你用来判断重复的条件,如只有col1,那么只要col1字段内容相同即表示记录相同。
6.
select identity(int,1,1) as id,* into #temp from tabel
select * from #temp where id in (
select max(id) from #emp where having count(*)>1 group by col1,col2,col3...)
⑻ sqlserver利用存储过程去除重复行的sql语句
还是先上代码吧
,可以先看
SQL语句去掉重复记录,获取重复记录
复制代码
代码如下:
ALTER
procere
[dbo].[PROC_ITEMMASTER_GETUNIQUE]
@PAGEINDEX
INT,@uid
int,@itemnumber
varchar(50)
AS
begin
tran
--开始事务
drop
table
[ItemMaster].[dbo].[testim]
--删除表
--把不重复记录转存到testim中
select
*
into
[ItemMaster].[dbo].[testim]
from
[ItemMaster].[dbo].[dat_item_master]
where
item_uid
in(select
min(item_uid)
as
item_uid
from
[ItemMaster].[dbo].[dat_item_master]
group
by
item_number)
and
status=0
select
top
10
*
from
[ItemMaster].[dbo].[testim]
where
item_uid
not
in
(select
top
(10*(@PAGEINDEX-1))
item_uid
from
[ItemMaster].[dbo].[testim])
and
owneruid=@uid
and
item_number
like
@itemnumber+'%'
--判断是否出错
if
@@error<>0
begin
rollback
tran
--出错则回滚
end
else
begin
--否则提前事务
commit
tran
end
我的数据是这样的:因为item_uid是标识列,item_number有重复的,
我想过滤成这样:
顺带说几个在编程的时候遇到的小问题
1.程序
出现
Could
not
find
stored
procere
找不到这个存储过程
因为我的程序数据库有四个,而默认连接是A,但实际要执行B库里的存储过程,导致出错,
解决办法1:可在A里面建个一样的存储过程2:在执行连接的时候,替换下数据库就行了
2.
asp.net/C#
将存储过程中返回的数据集,填充到dataset/datatable
复制代码
代码如下:
SqlConnection
conn
=
new
SqlConnection(ConfigurationManager.ConnectionStrings["SolutionSQLServer"].ToString());
SqlCommand
cmd
=
new
SqlCommand("Test",conn);
cmd.CommandType
=
CommandType.StoredProcere;
cmd.Parameters.Add("@MaxId",
SqlDbType.Int).Value
=
12000;
SqlDataAdapter
sda
=
new
SqlDataAdapter(cmd);
DataTable
dt
=
new
DataTable();
sda.Fill(dt);
在这感谢
http://www.cnblogs.com/liujuncm5/archive/2009/08/31/1557569.html
3.在存储过程里面,写SQL语句不能动态不加order
by
功能
比如
复制代码
代码如下:
--·@new_orderby
是传入参数,不能这样写
select
top
(10*(2-1))
item_uid
from
testim
order
by
@new_orderby
--执行这个的时候,SQL会出现
The
SELECT
item
identified
by
the
ORDER
BY
number
1
contains
a
variable
as
part
of
the
expression
identifying
a
column
position.
Variables
are
only
allowed
when
ordering
by
an
expression
referencing
a
column
name.
不过我找到解决办法,不过很麻烦,
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=9328
(第二个回答用
'
sql
'进行连接)
http://databases.aspfaq.com/database/how-do-i-use-a-variable-in-an-order-by-clause.html (用case
end
也行)
4.
select
into
和
insert
into
select
两种复制文句
(这里感谢http://www.cnblogs.com/freshman0216/archive/2008/08/15/1268316.html)
1.INSERT
INTO
SELECT语句
语句形式为:Insert
into
Table2(field1,field2,...)
select
value1,value2,...
from
Table1
要求目标表Table2必须存在,由于目标表Table2已经存在,所以我们除了插入源表Table1的字段外,还可以插入常量。
2.SELECT
INTO
FROM语句
语句形式为:SELECT
vale1,
value2
into
Table2
from
Table1
要求目标表Table2不存在,因为在插入时会自动创建表Table2,并将Table1中指定字段数据复制到Table2中。
5.顺便复习下常用的SQL方法语句
复制代码
代码如下:
declare
@name
varchar(200)
--声明变量
set
@name='abcd;def'
--赋值
print
'exec
len
:'+Convert(varchar(10),Len(@name))
--convert(type,value)转换,Len(value)获取大小
print
'exec
charindex:'+Convert(varchar(10),CharIndex('e',@name))--CharIndex(find,value)
在value中查找find的位置
print
'not
replace:'+@name
print
'exec
replace:'+Replace(@name,';','')
--用replace替换
print
'exec
substring:'+Substring(@name,0,3)--用substring截取
print
@@RowCount
--返回上一行代码受影响的行数
作者:chenhuzi
⑼ SQL中表里面怎么删除重复数据
出现这种情况的原因是你的表没有建立关键字,当出现重复数据时,sqlserver自带的图形化工具删除就会出现你出现的问题,即不能删除也不能更新,你可以使用如下方法解决:
1、给表建立关键字,比如增加一列自增的字段,这时候就可以删除了,删除完成后再删除新增的列即可
2、不增加字段,使用delete语句删除,但是这种情况会删除符合条件的数据,包括重复的数据
3、推荐使用1的方法
⑽ sql 删除重复行怎么写
1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断
select * from people
where peopleId in (select peopleId from people group by peopleId having count
(peopleId) > 1)
2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录
delete from people
where peopleId in (select peopleId from people group by peopleId having count
(peopleId) > 1)
and rowid not in (select min(rowid) from people group by peopleId having count(peopleId
)>1)
3、查找表中多余的重复记录(多个字段)
select * from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having
count(*) > 1)
4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录
delete from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having
count(*) > 1)
and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)
5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录
select * from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having
count(*) > 1)
and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)