插入資料並刪除重複

2021-06-16 02:37:04 字數 2779 閱讀 1009

方法一: if

object_id

('student')

isnot

null

drop

table

student 

--如果表不是空的那麼刪除表

create

table

student

(sno

int,sname

varchar(4

),s***

varchar(2

),sage

int,sdept

varchar(2

)) --建立表student

insert

into

[學生表

]select

95001,'

李勇','

男',20

,'cs'

union

allselect

95002,'

劉晨','

女',19

,'is'

union

allselect

95003,'

王敏','

女',18

,'is'

union

allselect

95004,'

張立','

男',19

,'ma'

union

allselect

96001,'

徐一','

男',20

,'is'

union

allselect

96002,'

張三','

女',21

,'cs'

union

allselect

96003,'

李四','

男',18

,'is'

--插入記錄

方法二:

with

學生表1 as(

select

95001,'

李勇','

男',20

,'cs'

union

allselect

95002,'

劉晨','

女',19

,'is'

union

allselect

95003,'

王敏','

女',18

,'is'

union

allselect

95004,'

張立','

男',19

,'ma'

union

allselect

96001,'

徐一','

男',20

,'is'

union

allselect

96002,'

張三','

女',21

,'cs'

union

allselect

96003,'

李四','

男',18

,'is'

)insert

into

學生表

select

*from

學生表1

方法三:

declare

@student

table

(sno

int,sname

varchar(4

),s***

varchar(2

),sage

int,sdept

varchar(2

))insert

into

@student

select

95001,'

李勇','

男',20

,'cs'

union

allselect

95002,'

劉晨','

女',19

,'is'

union

allselect

95003,'

王敏','

女',18

,'is'

union

allselect

95004,'

張立','

男',19

,'ma'

union

allselect

96001,'

徐一','

男',20

,'is'

union

allselect

96002,'

張三','

女',21

,'cs'

union

allselect

96003,'

李四','

男',18

,'is'

select

*from

@student

alter table student add id int identity(1,1)  --增加乙個自增字段,使表具有乙個唯一字段

delete from student where  id not in(select max(id) from student group by sname)--刪除重複

alter table student drop column id --刪除自增字段,恢復原表結構

select * from student order by sno;--根據編號排序顯示

DataTable插入資料,刪除重覆記錄

之前有篇文章簡單講了下批量插入資料的方法,但是沒有考慮到有重複值的情況,sqlbulkcopy也沒有提供相關方法。自己寫了乙個方法,傳入兩個table 目標table和源table 刪除重複行 目標table 源table public int deletereptrows datatable ta...

排序並刪除重複元素

usr bin python coding utf 8 def select sort lst left,right 0,len lst while left right p,i left,left 1 while i right if lst i lst p 覆蓋重複元素,陣列長度減一,即右邊界左...

刪除重複資料

介紹兩種刪除重複行的方式 1.使用臨時表,分組找出重複部分的id進行刪除 刪除table goods info 中存在重複goods id的記錄 select identity int,1,1 as autoid,into temptable from goods info select min a...