SQL ɾ³ýÖظ´Êý¾Ý£¬Ö»±£Áô1Ìõ
if not object_id('Tempdb..#T') is null
drop table #T
Go
Create table #T([ID] int,[Name] nvarchar(1),[Memo] nvarchar(2))
Insert #T
select 1,N'A',N'A1' union all
select 2,N'A',N'A2' union all
select 3,N'A',N'A3' union all
select 4,N'B',N'B1' union all
select 5,N'B',N'B2'
Go
--I¡¢NameÏàͬID×îСµÄ¼Ç¼(ÍƼöÓÃ1,2,3),±£Áô×îСһÌõ
·½·¨1:
delete a from #T a where exists(select 1 from #T where Name=a.Name and ID<a.ID)
·½·¨2:
delete a from #T a left join (select min(ID)ID,Name from #T group by Name) b on a.Name=b.Name and a.ID=b.ID where b.Id is null
·½·¨3:
delete a from #T a where ID not in (select min(ID) from #T where Name=a.Name)
·½·¨4(×¢:IDΪΨһʱ¿ÉÓÃ):
delete a from #T a where ID not in(select min(ID)from #T group by Name)
·½·¨5:
delete a from #T a where (select count(1) from #T where Name=a.Name and ID<a.ID)>0
·½·¨6:
delete a from #T a where ID<>(select top 1 ID from #T where Name=a.name order by ID)
·½·¨7:
delete a from #T a where ID>any(select ID from #T where Name=a.Name)
select * from #T
Éú³É½á¹û:
/*
ID Name Memo
----------- ---- ----
1 A A1
4 B B1
(2 ÐÐÊÜÓ°Ïì)
*/
--II¡¢NameÏàͬID±£Áô×î´óµÄÒ»Ìõ¼Ç¼:
·½·¨1:
delete a from #T a where exists(select 1 from #T where Name=a.Name and ID>a.ID)
·½·¨2:
delete a from #T a left join (select max(ID)ID,Name from #T group by Name) b on a.Name=b.Name and a.ID=b.ID where b.Id is null
·½·¨3:
delete a from #T a where ID not in (select max(ID) from #T where Name=a.Name)
·½·¨4(×¢:IDΪΨһʱ¿ÉÓÃ):
delete a from #T a where ID not in(select max(ID)from #T group by Name)
·½·¨5:
delete a from #T a where (select count(1) from #T where Name=a.Name and ID>a.ID)>0
·½·¨6:
delete a from #T a where ID<>(select top 1 ID fr
Ïà¹ØÎĵµ£º
Ëø»úÖÆ
NOLOCKºÍREADPASTµÄÇø±ð¡£
1. ¿ªÆôÒ»¸öÊÂÎñÖ´ÐвåÈëÊý¾ÝµÄ²Ù×÷¡£
BEGIN TRAN t
INSERT INTO Customer
SELECT 'a','a'
2. Ö´ÐÐÒ»Ìõ²éѯÓï¾ä¡£
SELECT * from Customer WITH (NOLOCK)
½á¹ûÖÐÏÔʾ"a"ºÍ"a"¡£µ±1ÖÐÊÂÎñ»Ø¹öºó£¬ÄÇôa½«³ÉΪÔàÊý¾Ý¡£(×¢:1ÖеÄÊÂÎñδÌá½») ¡£NOLOCK±íÃ÷ûÓжÔÊý¾Ý±íÌí¼Ó¹²Ï ......
µ¼³ö±í½á¹¹:
Tools-->Export User Objects -->Ñ¡ÔñÒªµ¼³öµÄ±í(°üÀ¨SequenceµÈ)-->.sqlÎļþ
µ¼³ö±íÊý¾Ý:
Tools-->Export Tables-->Ñ¡Ôñ±í,Ñ¡ÔñSQL Inserts-->.sqlÎļþ
Ö´ÐÐÕâЩ.sqlÎļþʱ,ҪʹÓÃн¨Command WindowÀ´Ö´ÐÐ. ......
INSERT INTO
tableName ( columnName1, columnName2, columnName3, columnName4)
VALUES ( '45', 'Î÷²Ø°ì', TO_Date( '2010-03-10 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), NULL,);
commit; ......
Student(S#,Sname,Sage,Ssex) ѧÉú±í
Course(C#,Cname,T#) ¿Î³Ì±í
SC(S#,C#,score) ³É¼¨±í
Teacher(T#,Tname) ½Ìʦ±í
ÎÊÌ⣺
1¡¢²éѯ“001”¿Î³Ì±È“002”¿Î³Ì³É¼¨¸ßµÄËùÓÐѧÉúµÄѧºÅ£»
select a.S# from (select s#,score from SC where C#='001') a,(select s#,score
fr ......