日期:2014-05-18 浏览次数:20548 次
declare @count int exec UP_GetRecordByPage 'AllShop','ShopID',6,3,@count output,0,'ShopName like ''%酒店%'''
exec UP_GetRecordByPage 'AllShop','ShopID',6,3,@count output,0,'Tags like ''%酒店%'' or Address like ''%酒店%'' or ShopName like ''%酒店%'''。
CREATE PROCEDURE UP_GetRecordByPage @tblName varchar(255), -- 表名 @fldName varchar(255), -- 主键字段名 @PageSize int = 10, -- 页尺寸 @PageIndex int = 1, -- 页码 @Count int out, -- 返回记录总数 @OrderType int = 0, -- 设置排序类型, 非 0 值则降序 @strWhere varchar(1000) = '' -- 查询条件 (注意: 不要加 where) AS begin declare @strSQL varchar(6000) -- 主语句 declare @strTmp varchar(1000) -- 临时变量 declare @strOrder varchar(400) -- 排序类型 declare @sql nvarchar(4000) if @strWhere != '' set @sql = N'select @Count=count(*) from [' + @tblName + ']'+' where ' + @strWhere else set @sql = N'select @Count=count(*) from [' + @tblName + ']' exec sp_executesql @sql,N'@Count int out',@Count out if @OrderType != 0 begin set @strTmp = '<(select min' set @strOrder = ' order by [' + @fldName +'] desc' end else begin set @strTmp = '>(select max' set @strOrder = ' order by [' + @fldName +'] asc' end set @strSQL = 'select top ' + str(@PageSize) + ' * from [' + @tblName + '] where [' + @fldName + ']' + @strTmp + '([' + @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' [' + @fldName + '] from [' + @tblName + ']' + @strOrder + ') as tblTmp)' + @strOrder if @strWhere != '' set @strSQL = 'select top ' + str(@PageSize) + ' * from [' + @tblName + '] where [' + @fldName + ']' + @strTmp + '([' + @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' [' + @fldName + '] from [' + @tblName + '] where ' + @strWhere + ' ' + @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder if @PageIndex = 1 begin set @strTmp ='' if @strWhere != '' set @strTmp = ' where ' + @strWhere set @strSQL = 'select top ' + str(@PageSize) + ' * from [' + @tblName + ']' + @strTmp + ' ' + @strOrder end exec (@strSQL) end go
'Tags like( ''%酒店%'' or Address like ''%酒店%'' or ShopName like ''%酒店%'')'
------解决方案--------------------
通过,print 把sql语句, 打出来! 看看! 在想办法!
------解决方案--------------------
http://blog.csdn.net/fredrickhu/archive/2009/09/18/4566261.aspx
------解决方案--------------------
if @PageIndex = 1 --- 问题应该在这, begin set @strTmp ='' if @strWhere != '' set @strTmp = ' where ' + @strWhere set @strSQL = 'select top ' + str(@PageSize) + ' * from [' + @tblName + ']' + @strTmp + ' ' + @strOrder end exec (@strSQL) end