16、说明:包括所有在 TableA 中但不在 TableB和TableC 中的行并消除所有重复行而派生出一个结果表 (select a from tableA ) except (select a from tableB) except (select a from tableC)
3 R' J u0 E; v0 c8 _ 17、说明:随机取出10条数据
8 @, |" R- f" P# Y3 U3 }% M& b select top 10 * from tablename order by newid()
7 j: ~) f4 c( u1 g' g/ h; w 18、说明:随机选择记录. q: S( K9 j0 l' j' i1 _, D7 _. p
select newid()! H2 L! F, M, Y! H) L8 p
19、说明:删除重复记录
1 B, {; s( u# ?. X( v( Q 1),delete from tablename where id not in (select max(id) from tablename group by col1,col2,...)7 k) D0 v/ N$ C9 h
2),select distinct * into temp from tablename
! `' U! e4 ^% c2 N: O3 C delete from tablename: w$ w/ @1 Y9 Z6 f! r; Q' O
insert into tablename select * from temp
! q6 s+ [: F+ P& Q f 评价: 这种操作牵连大量的数据的移动,这种做法不适合大容量但数据操作; I& `. V) N4 M5 ~
3),例如:在一个外部表中导入数据,由于某些原因第一次只导入了一部分,但很难判断具体位置,这样只有在下一次全部导入,这样也就产生好多重复的字段,怎样删除重复字段2 w8 C1 r) C# {- g% X2 B2 e2 ?
alter table tablename2 k \+ a2 c. K/ U
--添加一个自增列
% [# I, R0 T; y5 l* O0 D add column_b int identity(1,1)
. i" Y" G: R0 n: { delete from tablename where column_b not in(+ V" ^* \! \/ F) J
select max(column_b) from tablename group by column1,column2,...)
) K1 ^2 h7 ]6 Y9 |" S V9 Y/ A, C% c alter table tablename drop column column_b
* b& v; r& I; F+ Z; f2 F4 ? 20、说明:列出数据库里所有的表名7 ~4 G7 r- ? o! {( i) E
: M0 }, U ^/ K8 L8 i, |% h$ W, ^ select name from sysobjects where type='U' // U代表用户 |