×Ô¿ªÊ¼×öÏîÄ¿ÒÔÀ´£¬Ò»Ö±ÔÚÓá£Õâ¶Î´æ´¢¹ý³ÌµÄµÄÔ´´Õߣ¨SORRY£¬Íü¼ÇÃû×ÖÁË£©£¬Ð´µÃÕâ¶ÎSQL´úÂëºÜ²»´í£¬ÎÒÔÚÕâ¸ö»ù´¡ÉÏ£¬°´ÕÕÎÒµÄÏ°¹ßÒÔ¼°Ë¼Î¬·½Ê½£¬µ÷ÕûÁË´úÂ룬ֻ×ö·ÖÒ³²éѯÓá£
/**//*---------------------------------------------- *procedure name : prcPageResult * author : FuChun * create date : 2006-10-04 */ CREATE PROCEDURE prcPageResult -- »ñµÃijһҳµÄÊý¾Ý -- @currPage int = 1, --µ±Ç°Ò³Ò³Âë (¼´Top currPage) @showColumn varchar(2000) = '*', --ÐèÒªµÃµ½µÄ×ֶΠ(¼´ column1,column2,......) @tabName varchar(2000), --ÐèÒª²é¿´µÄ±íÃû (¼´ from table_name) @strCondition varchar(2000) = '', --²éѯÌõ¼þ (¼´ where condition......) ²»ÓüÓwhere¹Ø¼ü×Ö @ascColumn varchar(100) = '', --ÅÅÐòµÄ×Ö¶ÎÃû (¼´ order by column asc/desc) @bitOrderType bit = 0, --ÅÅÐòµÄÀàÐÍ (0ΪÉýÐò,1Ϊ½µÐò) @pkColumn varchar(50) = '', --Ö÷¼üÃû³Æ @pageSize int = 20 --·ÖÒ³´óС
AS BEGIN -- ´æ´¢¹ý³Ì¿ªÊ¼ -- ¸Ã´æ´¢¹ý³ÌÐèÒªÓõ½µÄ¼¸¸ö±äÁ¿ http://www.knowsky.com/ -- DECLARE @strTemp varchar(1000) DECLARE @strSql varchar(4000) --¸Ã´æ´¢¹ý³Ì×îºóÖ´ÐеÄÓï¾ä DECLARE @strOrderType varchar(1000) --ÅÅÐòÀàÐÍÓï¾ä (order by column asc»òÕßorder by column desc)
BEGIN IF @bitOrderType = 1 -- bitOrderType=1¼´Ö´ÐнµÐò BEGIN SET @strOrderType = ' ORDER BY '+@ascColumn+' DESC' SET @strTemp = '<(SELECT min' END ELSE BEGIN SET @strOrderType = ' ORDER BY '+@ascColumn+' ASC' SET @strTemp = '>(SELECT max' END
IF @currPage = 1 -- Èç¹ûÊǵÚÒ»Ò³ BEGIN IF @strCondition != '' SET @strSql = 'SELECT TOP '+STR(@pageSize)+' '+@showColumn+' FROM '+@tabName+ ' WHERE '+@strCondition+@strOrderType ELSE SET @strSql = 'SELECT TOP '+STR(@pageSize)+' '+@showColumn+' FROM '+@tabName+@strOrderType END
ELSE -- ÆäËûÒ³ BEGIN IF @strCondition !='' SET @strSql = 'SELECT TOP '+STR(@pageSize)+' '+@showColumn+' FROM '+@tabName+ ' WHERE '+@strCondition+' AND '+@pkColumn+@strTemp+'('+@pkColumn+')'+' FROM (SELECT TOP '+STR((@currPage-1)*@pageSize)+ ' '+@pkColumn+' FROM '+@tabName+@strOrderType+') AS TabTemp)'+@strOrderType ELSE SET @strSql = 'SELECT TOP '+STR(@pageSize)+' '+@showColumn+' FROM '+@tabName+ ' WHERE '+@pkColumn+@strTemp+'('+@pkColumn+')'+' FROM (SELECT TOP '+STR((@currPage-1)*@pageSize)+' '+@pkColumn+ ' FROM '+@tabName+@strOrderType+') AS TabTemp)'+@strOrderType END
END EXEC (@strSql) END -- ´æ´¢¹ý³Ì½áÊø ------------------------------------------------ GO µ÷Ó÷½·¨£º
prcPageResult 1,'*','TableName','','CreateDate',1,'PkID',25
ÉÏÃæ±íʾ£¬²éѯ±íTableNameµÄËùÓÐ×ֶΣ¬Ç°25Ìõ¼Ç¼£¬ÒòΪÊǵÚÒ»Ò³£¬ÅÅÐò×Ö¶ÎΪCreateDate£¬½µÐòÅÅÁУ¬Ö÷¼üÊÇPkID¡£Õâ¸ö´æ´¢¹ý³ÌµÄ¹¦ÄܱȽÏÇ¿´ó£¬ÓÃÔÚÏîÄ¿Öзdz£µÄÊÊÓᣲ»ÐÅÄú¿ÉÒÔÊÔÊÔ¿´£¬ÓÈÆäÊÇÔÚ°ÙÍò¼¶Êý¾ÝÉÏ£¬ËûµÄÓÅÊƾÍÏÔ¶ÎÞÒÉÁË£¬µ±È»£¬Õâ¶Î´úÂëÊÇ¿ÉÒÔת»»³ÉMySqlÖеĴ洢¹ý³ÌµÄ£¬²»¹ý£¬ÔÚÕâÀï¾Í²»¸ø´ó¼ÒÁË£¬ÄãÃÇ¿ÉÒÔ×Ô¼ºÊÔ×Åת»»¿´¿´¡£
ÏÂÃæµÄ´æ´¢¹ý³Ì²éѯ±íµÄ¼Ç¼Êý£º
/**//*---------------------------------------------- *procedure name : prcRowsCount * author : FuChun * create date : 2006-09-22 */ CREATE PROC prcRowsCount @tabName varchar(200), --ÐèÒª²éѯµÄ±íÃû @colName varchar(200)='*', --ÐèÒª²éѯµÄÁÐÃû @condition varchar(200)='' --²éѯÌõ¼þ AS BEGIN DECLARE @strSql varchar(255) IF @condition = '' SET @strSql='select count('+@colName+') from '+@tabName ELSE SET @strSql='select count('+@colName+') from '+@tabName+' where '+@condition EXEC (@strSql) END ------------------------------------------------ GO »¹ÓÐɾ³ý¼Ç¼µÄͨÓô洢¹ý³ÌºÍ²éѯµ¥Ìõ¼Ç¼µÄͨÓô洢¹ý³ÌÔÚÕâÀï¾Í²»·îËÍÁË£¬¸ÐлÌṩÔͨÓ÷ÖÒ³²éѯ´æ´¢¹ý³ÌµÄ¸çÃÇ£¬Ð»Ð»¡£ http://blog.csdn.net/fcrpg2005/archive/2007/02/22/1512707.aspx
|