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)