GH教學

GodHelp線上補習班、各類教學分享網站(程式設計、Flash遊戲教學、影音轉檔、動畫、繪圖、Go Home Schooling.....等等)

搜尋

SQL刪除重複資料,一次搞定

常常會有資料表沒有設定Primary Key
造成資料重複匯入
要如刪除重複部份
程序如下

--建置一個TEMP TABLE
drop table #A
create table #A(i int,s nvarchar(100))
--把來源資料下group by 並限制資料筆數是兩筆以上的加以匯入
insert into #A
select * from SourceTable
where iGroup in(
select iGroup from SourceTable
group by iGroup
having count(*) >1
)
--刪除重複的資料
delete AdminGroup
where iGroup in(
select iGroup from SourceTable
group by iGroup
having count(*) >1
)
--最後把整理匯入的Temp TABLE資料匯入即可
insert into SourceTable
select distinct i,s from #A

GH教學

0 意見:

張貼留言

活動

給您專屬賀卡

好站

知識+名片

GH知識+

天氣

Sitetag台灣部落格列表

統計

Blog Look Score and Rank