16、说明:包括所有在 TableA 中但不在 TableB和TableC 中的行并消除所有重复行而派生出一个结果表 (select a from tableA ) except (select a from tableB) except (select a from tableC)' v6 o* X, ~4 j$ G
17、说明:随机取出10条数据* ^- R ~8 C, Q( D9 z% N+ ?
select top 10 * from tablename order by newid()
6 e$ m: V1 G, G! _' R; h" D, u 18、说明:随机选择记录
0 H: g2 q3 W7 t! t1 g/ ~7 S& o select newid()& r8 _7 w% S7 t8 h
19、说明:删除重复记录
$ U5 _- V$ N% a6 q$ F- {& n# s0 h 1),delete from tablename where id not in (select max(id) from tablename group by col1,col2,...)
. X- O( k9 [) P- V- u* |, |; N6 U% ~ 2),select distinct * into temp from tablename
; [6 U: t4 @0 [$ d3 N delete from tablename) p& @1 m5 E, E
insert into tablename select * from temp
; D: z8 ~7 y2 o% W1 _9 U 评价: 这种操作牵连大量的数据的移动,这种做法不适合大容量但数据操作4 ]$ j5 C5 e: k8 B2 s7 u$ X/ F
3),例如:在一个外部表中导入数据,由于某些原因第一次只导入了一部分,但很难判断具体位置,这样只有在下一次全部导入,这样也就产生好多重复的字段,怎样删除重复字段
6 u# f" T" W% n" d) u alter table tablename
4 _7 X- {, J4 W --添加一个自增列
. \) }* e9 D' M; y add column_b int identity(1,1)6 q; j; E- b- B) a, P0 n, j. R
delete from tablename where column_b not in(
, Z8 ?; H) k `' D select max(column_b) from tablename group by column1,column2,...)
" i( L R- {: A+ w0 l. I9 ?. K alter table tablename drop column column_b
" r5 C7 \: c# `4 Q; G4 H7 \1 K 20、说明:列出数据库里所有的表名3 H! {7 g7 V$ P t
) T. [& r- N+ L6 e2 Y, ~ select name from sysobjects where type='U' // U代表用户 |