Ò׽ؽØÍ¼Èí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

SQL Server2005 applyµÄһЩÔËÓÃ

Àý×Ó£ºÕÒ³öÿ¸ö¸èÊÖÏúÁ¿×î¸ßµÄ3Ê׸è
create table singer_info
(
id int primary key identity(1,1),
name varchar(20),
song varchar(20),
amount int
)
 
insert into singer_info values('jay','aaa',1000)
insert into singer_info values('jay','bbb',2000)
insert into singer_info values('jay','ccc',3000)
insert into singer_info values('jay','ddd',4000)
insert into singer_info values('jay','eee',5000)
insert into singer_info values('jay1','fff',1000)
insert into singer_info values('jay1','ggg',2000)
insert into singer_info values('jay1','hhh',3000)
insert into singer_info values('jay1','iii',4000)
insert into singer_info values('jay2','jjj',1000)
insert into singer_info values('jay2','kkk',2000)
insert into singer_info values('jay2','lll',3000)
insert into singer_info values('jay2','mmm',4000)
insert into singer_info values('jay3','nnn',1000)
 
(1)ʹÓÃfunction apply
create function gettop
(@name varchar(20))
returns table
as
return (select top(3)id,name,song,amount
from singer_info
where name = @name
order by amount desc)
 
select distinct b.id,b.name,b.song,b.amount
from singer_info a
cross apply
gettop(a.name)as b
 
(2)ʹÓÃapply
select distinct c.id,c.name,c.song,c.amount
from singer_info a
cross apply
(select top(3)id,name,song,amount
from singer_info b
where b.name = a.name
order by amount desc) as c
order by c.name asc,c.amount desc
 
(3)ʹÓÃover partition by
select * from
(select a.id,a.name,a.song,a.amount,
row_number() over(partition by a.name order by a.name,a.amount desc) rn
from singer_info a)b
where b.rn<=3


Ïà¹ØÎĵµ£º

SQL Union ÓëUnion AllÏê½â

1.Union
UNION Ö¸ÁîµÄÄ¿µÄÊǽ«Á½¸ö SQL Óï¾äµÄ½á¹ûºÏ²¢ÆðÀ´¡£´ÓÕâ¸ö½Ç¶ÈÀ´¿´£¬ UNION ¸ú JOIN ÓÐЩÐíÀàËÆ£¬ÒòΪÕâÁ½¸öÖ¸Áî¶¼¿ÉÒÔÓɶà¸ö±í¸ñÖÐߢȡ×ÊÁÏ¡£ UNION µÄÒ»¸öÏÞÖÆÊÇÁ½¸ö SQL Óï¾äËù²úÉúµÄÀ¸Î»ÐèÒªÊÇͬÑùµÄ×ÊÁÏÖÖÀà¡£ÁíÍ⣬µ±ÎÒÃÇÓà UNIONÕâ¸öÖ¸Áîʱ£¬ÎÒÃÇÖ»»á¿´µ½²»Í¬µÄ×ÊÁÏÖµ (ÀàËÆ SELECT DISTINCT)¡£
UNION µ ......

ʹÓù«Óñí±í´ïʽ£¨CTE£©¼ò»¯Ç¶Ì×SQL ºÍ½øÐеݹéµ÷ÓÃ

1.ʹÓÃCTE¼ò»¯Ç¶Ì×sql
   ÏÈ¿´ÏÂÃæÒ»¸öǶÌ׵IJéѯÓï¾ä£º
select * from person.StateProvince where CountryRegionCode in 
         (select CountryRegionCode from person.CountryRegion where  ......

ʹÓà SQL Server ʱÐèÒª¾­³£Óõ½µÄ¼¸¸öÉèÖÃÑ¡Ïî


1. SET DEADLOCK_PRIORITY
˵Ã÷£º¿ØÖÆÔÚ·¢ÉúËÀËøÇé¿öʱ»á»°µÄ·´Ó¦·½Ê½¡£Èç¹ûÁ½¸ö½ø³Ì¶¼Ëø¶¨Êý¾Ý£¬²¢ÇÒÖ±µ½ÆäËü½ø³ÌÊÍ·Å×Ô¼ºµÄËøÊ±£¬Ã¿¸ö½ø³Ì²ÅÄÜÊÍ·Å×Ô¼ºµÄËø£¬¼´·¢ÉúËÀËøÇé¿ö¡£
Óï·¨£ºSET DEADLOCK_PRIORITY { LOW | NORMAL | @deadlock_var }
²ÎÊý£ºLOW  Ö¸¶¨µ±Ç°»á»°ÎªÊ×Ñ¡ËÀËøÎþÉüÆ·¡£Microsoft® S ......

Linq²éѯ DataSet ÄÜ·ñºÍSQL ¼¶Áª£¿

        ×î½ü×öÏîÄ¿µÄʱºò£¬Óöµ½ÁËÒ»¸öÎÊÌâ¡£ÎÒÖ÷ÒªÊÇ×öÒ»¸öWeb Services¸ø±ðÈËÓõġ£±ðÈË´«Ò»¸öÓû§IDºÅ¹ýÀ´£¬È»ºóÎÒ½«Õâ¸öÓû§µÄËùÓкÃÓѵÄÏÂÔØ¼Ç¼°ü×°³ÉÒ»¸öDataSet·µ»ØÈ¥¡£ ¶ø¸ù¾ÝÓû§IDºÅ»ñÈ¡¸ÃÓû§µÄËùÓкÃÓÑÐÅÏ¢£¬ÔòÊÇͨ¹ýÁíÒ»¸öWeb ServicesµÃµ½µÄ£¬ÕâÀïΪFriendDS¡£
  ......

Web°²È«¿ª·¢£ºSQL×¢Èë¹¥»÷ºÍÍøÒ³¹ÒÂí

ÉêÃ÷¡£ÎÄÕ½ö´ú±í¸öÈ˹۵㣬ÓëËùÔÚ¹«Ë¾ÎÞÈκÎÁªÏµ¡£
1.     ¸ÅÊö
ÍøÒ³¹ÒÂíÕâ¸ö»°ÌâÏëÀ´´ó¼Ò²¢²»Ä°Éú¡£ÎªÊ²Ã´ÓÐÕâô¶àµÄÍøÒ³ÉÏ´æÔÚ×ÅľÂíÈ¥¹¥»÷ÆÕͨÓû§£¿²»¿É·ñÈÏ£¬Ï൱һ²¿·ÖÍøÒ³Ô­±¾¾ÍÊǶñÒâµÄ£ºÍøÒ³µÄ×÷Õß¹ÊÒâÔÚÉÏÃæ·ÅÉÏľÂí£¬È»ºóͨ¹ý¸÷ÖÖÊÖ¶ÎÒýÓÕÓû§È¥ä¯ÀÀ¡£µ«ÊǾø´ó¶àÊý±»¹ÒÂíµÄÍøÒ³Ô­±¾ÊÇÕý³ ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØÍ¼ | ¸ÓICP±¸09004571ºÅ