sql 2005 ·ÖÒ³£¬ÅÅÃûº¯Êý
WITH V AS(SELECT ROW_NUMBER() OVER(order by fcustid desc) AS RN,* from ts_dict AS SourceView)
SELECT * from V WHERE RN BETWEEN 1 AND 10
תÔصØÖ·£ºhttp://www.cnblogs.com/nokiaguy/archive/2009/02/05/1384860.html
ÅÅÃûº¯ÊýÊÇSQL Server2005мӵŦÄÜ¡£ÔÚSQL Server2005ÖÐÓÐÈçÏÂËĸöÅÅÃûº¯Êý£º
1. row_number
2. rank
3. dense_rank
4. ntile
ÏÂÃæ·Ö±ð½éÉÜÒ»ÏÂÕâËĸöÅÅÃûº¯ÊýµÄ¹¦Äܼ°Ó÷¨¡£ÔÚ½éÉÜ֮ǰ¼ÙÉèÓÐÒ»¸öt_table±í£¬±í½á¹¹Óë±íÖеÄÊý¾ÝÈçͼ1Ëùʾ£º
ͼ1
ÆäÖÐfield1×ֶεÄÀàÐÍÊÇint£¬field2×ֶεÄÀàÐÍÊÇvarchar
Ò»¡¢row_number
row_numberº¯ÊýµÄÓÃ;ÊǷdz£¹ã·º£¬Õâ¸öº¯ÊýµÄ¹¦ÄÜÊÇΪ²éѯ³öÀ´µÄÿһÐмǼÉú³ÉÒ»¸öÐòºÅ¡£row_numberº¯ÊýµÄÓ÷¨ÈçÏÂÃæµÄSQLÓï¾äËùʾ£º
select row_number() over(order by field1) as row_number,* from t_table
ÉÏÃæµÄSQLÓï¾äµÄ²éѯ½á¹ûÈçͼ2Ëùʾ¡£
ͼ2
ÆäÖÐrow_numberÁÐÊÇÓÉrow_numberº¯ÊýÉú³ÉµÄÐòºÅÁС£ÔÚʹÓÃrow_numberº¯ÊýÊÇҪʹÓÃover×Ó¾äÑ¡Ôñ¶ÔijһÁнøÐÐÅÅÐò£¬È»ºó²ÅÄÜÉú³ÉÐòºÅ¡£
ʵ¼ÊÉÏ£¬row_numberº¯ÊýÉú³ÉÐòºÅµÄ»ù±¾ÔÀíÊÇÏÈʹÓÃover×Ó¾äÖеÄÅÅÐòÓï¾ä¶Ô¼Ç¼½øÐÐÅÅÐò£¬È»ºó°´×ÅÕâ¸ö˳ÐòÉú³ÉÐòºÅ¡£over×Ó¾äÖеÄorder by×Ó¾äÓëSQLÓï¾äÖеÄorder by×Ó¾äûÓÐÈκιØϵ£¬ÕâÁ½´¦µÄorder by ¿ÉÒÔÍêÈ«²»Í¬£¬ÈçÏÂÃæµÄSQLÓï¾äËùʾ£º
select row_number() over(order by field2 desc) as row_number,* from t_table order by field1 desc
ÉÏÃæµÄSQLÓï¾äµÄ²éѯ½á¹ûÈçͼ3Ëùʾ¡£
ͼ3
ÎÒÃÇ¿ÉÒÔʹÓÃrow_numberº¯ÊýÀ´ÊµÏÖ²éѯ±íÖÐÖ¸¶¨·¶Î§µÄ¼Ç¼£¬Ò»°ã½«ÆäÓ¦Óõ½WebÓ¦ÓóÌÐòµÄ·ÖÒ³¹¦ÄÜÉÏ¡£ÏÂÃæµÄSQLÓï¾ä¿ÉÒÔ²éѯt_table±íÖеÚ2ÌõºÍµÚ3Ìõ¼Ç¼£º
with t_rowtable
as
(
select row_number() over(order by field1) as row_number,* from t_table
)
select * from t_rowtable where row_number>1 and row_number < 4 order by f
Ïà¹ØÎĵµ£º
sqlÓï¾ä£¬È¡³ö±íAÖеĵÚ31Ìõµ½40Ìõ¼Ç¼£¨±íAÒÔ×Ô¶¯Ôö³¤µÄID×öÖ÷¼ü£¬×¢ÒâID¿ÉÄÜÊDz»Á¬ÐøµÄ£©
-->select top 10 * from a where id not in (select top 30 id from a order by id) order by id
²éѯǰʮÌõ¼Ç¼£¬µ«Ìõ¼þÊÇ£ºID²»ÔÚÇ°ÈýÊ®ÌõµÄIDÀïÃæ
-->select top 10 * from (select top 40 ......
1. ´´½¨ÊÓͼ£º
CREATE OR REPLACE VIEW SM_V_UNIT_AUTH AS
SELECT T2.UNIT_ID,
T2.SUPER_UNIT_ID,
T1.AUTH_ID,
T1.AUTH_NAME,
T1.A ......
°æȨÉùÃ÷£ºÔ´´×÷Æ·£¬ÈçÐèתÔØ£¬ÇëÓë×÷ÕßÁªÏµ¡£·ñÔò½«×·¾¿·¨ÂÉÔðÈΡ£
DB2ÁÙʱ±íÔÚSQL¹ý³ÌºÍSQLÓï¾äÖеIJâÊÔ×ܽá
²âÊÔÄ¿±ê£º
·Ö±ðÔÚSQL¹ý³ÌºÍSQLÓï¾äÖд´½¨ÁÙʱ±í£¬²¢²åÈëÊý¾Ý£¬¿´Ö´Ðнá¹ûÓÐʲôÒìͬ¡£
²âÊÔ»·¾³£º
DB2 UDB V9.1
Ö´Ðи½¼þÀïÃæµÄSQLÓï¾ä£¬µÃµ½Ò»¸ö±í¡ ......
Ò»Ö±¶¼ÏëÕÒ¸öʱ¼äÑо¿Ò»Ï£¬½ñÌ컨ÁËÒ»Ììʱ¼äȥʵ¼ùÁËһϣ¬·Ç³£ÓÐÓ᣿ÉÒÔ˵ÊÇÊý¾Ý¿âÀïÃæµÄ¾«»ª°É¡£»¹ºÃÎÒ½ñÌìȥŪÁ˼¸Ï£¬Ëä˵¶¼ÊÇЩ¼òµ¥µÄÓï¾ä£¬µ«ÊÇ×îÆðÂëÖªµÀÒÔºóÔÚÏîÄ¿¸ÃÔõôȥ×öÁË¡£ Ïл°ÉÙ˵¡£½øÈëÕýÌâ¡£
1.´´½¨SQL´æ´¢¹ý³Ì:
CREATE PROCEDURE stu_proc1//Ö¸¶¨¹ý³ÌÃû
@Sno varc ......
1¡¢Î½´Ê ν´ÊÔÊÐíÄú¹¹ÔìÌõ¼þ£¬ÒÔ±ãÖ»´¦ÀíÂú×ãÕâЩÌõ¼þµÄÄÇЩÐС£
2¡¢Ê¹Óà IN ν´Ê
ʹÓà IN ν´Ê½«Ò»¸öÖµÓëÆäËû¼¸¸öÖµ½øÐбȽϡ£ÀýÈ磺
SELECT NAME from STAFF WHERE DEPT IN (20, 15)
´ËʾÀýÏ൱ÓÚ£º
SELECT NAME from STAFF WHERE DEPT = 20 OR DEPT = 15
µ±×Ó²éѯ·µ»ØÒ»×éֵʱ£¬¿ÉʹÓà IN ºÍ NOT IN ÔËËã·û¡£ÀýÈç£ ......