sql server2005 rownumͨÓô洢¹ý³Ì·ÖÒ³
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER PROCEDURE [dbo].[sp_GetRecordfromPage]
@TableName varchar(350), --±íÃû
@Fields varchar(5000) = '*', --×Ö¶ÎÃû(È«²¿×Ö¶ÎΪ*)
@OrderField varchar(5000), --ÅÅÐò×Ö¶Î(±ØÐë!Ö§³Ö¶à×Ö¶Î)
@sqlWhere varchar(5000) = Null,--Ìõ¼þÓï¾ä(²»ÓüÓwhere)
@pageSize int, --ÿҳ¶àÉÙÌõ¼Ç¼
@pageIndex int = 1, --Ö¸¶¨µ±Ç°ÎªµÚ¼¸Ò³
@distinct VARCHAR(50)=NULL, --È¥³ýÖØ¸´Öµ£¬×¢ÒâÖ»ÄÜÊÇÒ»¸ö×Ö¶Î
@top INT=NULL --²éѯTOP,²»´«ÎªÈ«²¿
AS
BEGIN
Declare @sql nvarchar(4000);
Declare @totalRecord int;
DECLARE @totalPage INT;
--¼ÆËã×ܼǼÊý
IF (@distinct IS NULL OR @distinct='')
BEGIN
IF (@SqlWhere='' OR @sqlWhere IS NULL)
SET @sql = 'select @totalRecord = count(1) from ' + @TableName
ELSE
SET @sql = 'select @totalRecord = count(1) from ' + @TableName + ' where ' + @sqlWhere
END
ELSE
BEGIN
IF (@SqlWhere='' OR @sqlWhere IS NULL)
SET @sql = 'select @totalRecord = count(distinct ' + @distinct + ') from ' + @TableName
ELSE
SET @sql = 'select @totalRecord = count(distinct ' + @distinct + ') from ' + @TableName + ' where ' + @sqlWhere
END
EXEC sp_executesql @sql,N'@totalRecord int OUTPUT',@totalRecord OUTPUT--¼ÆËã×ܼǼÊý
IF(@top>0)
BEGIN
--Ö¸¶¨TOP ¼Ç¼
SET @Fields= 'top ' + CAST(@top AS VARCHAR(20)) + ' ' + @Fields;
--Èç¹û×ܼǼÊý³¬¹ýTOPÊý,Éè×ܼÇÂ
Ïà¹ØÎĵµ£º
1¡¢²éÕÒ±íÖжàÓàµÄÖØ¸´¼Ç¼£¬Öظ´¼Ç¼ÊǸù¾Ýµ¥¸ö×ֶΣ¨peopleId£©À´ÅжÏ
select * from people
where peopleId in (select peopleId from people group by peopleId having count
(peopleId) >& ......
##################################################
# ÉùÃ÷£º
# ±¾ÎÄת×ÔCSDN£¬Óû§ID£ºhtl258
# ÔÌûµØÖ·£ºhttp://blog.csdn.net/htl258/archive/2010/04/28/5537416.aspx
####### ......
ǰ¶Îʱ¼äѧµÄÓйØsqlµÄÏà¹ØÖªÊ¶,ºÜ¶à¶¼Ã»¼°Ê±È¥ÕûÀí,½ñÌì¸ÕºÃÓÐʱ¼äÓÐûÐÄÇé×öÆäËûµÄÊÇ,¾ÍÕûÀíÕûÀí°É
Ò»Ö±ÒÔΪ×ö¿ª·¢µÄ¶ÔÊý¾Ý¿â²Ù×÷·½ÃæµÄÒªÇó²»»áºÜ¸ß,Ö»Òª»á¶ÔÊý¾ÝÔöɾ¸Ä²é¾ÍokÁË.ÏÖÔÚ²ÅÖªµÀ¶ÔÊý¾Ý¿âµÄ²Ù×÷»¹Ó¦¸Ã°üÀ¨Ð´´æ´¢ºÍº¯Êý,»¹Òª´´½¨ÐòÁС¢Ë÷Òý,ÊÓͼµÈ.
......
Áª½ÓÌõ¼þ¿ÉÔÚ from »ò WHERE ×Ó¾äÖÐÖ¸¶¨£¬½¨ÒéÔÚ from ×Ó¾äÖÐÖ¸¶¨Áª½ÓÌõ¼þ¡£WHERE ºÍ HAVING ×Ó¾äÒ²¿ÉÒÔ°üº¬ËÑË÷Ìõ¼þ£¬ÒÔ½øÒ»²½É¸Ñ¡Áª½ÓÌõ¼þËùÑ¡µÄÐС£
Áª½Ó¿É·ÖΪÒÔϼ¸Àࣺ
1. ......