求一支持或条件模糊查询的分页存储过程?
我这有个通用分页存储过程,查询条件可以自己写入的。
declare @count intexec 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)ASbegindeclare @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 ' + @strWhereelse set @sql = N'select @Count=count(*) from [' + @tblName + ']'exec sp_executesql @sql,N'@Count int out',@Count outif @OrderType != 0begin set @strTmp = '<(select min' set @strOrder = ' order by [' + @fldName +'] desc'endelsebegin set @strTmp = '>(select max' set @strOrder = ' order by [' + @fldName +'] asc'endset @strSQL = 'select top ' + str(@PageSize) + ' * from [' + @tblName + '] where [' + @fldName + ']' + @strTmp + '([' + @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' [' + @fldName + '] from [' + @tblName + ']' + @strOrder + ') as tblTmp)' + @strOrderif @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 + ' ' + @strOrderif @PageIndex = 1begin set @strTmp ='' if @strWhere != '' set @strTmp = ' where ' + @strWhere set @strSQL = 'select top ' + str(@PageSize) + ' * from [' + @tblName + ']' + @strTmp + ' ' + @strOrderendexec (@strSQL)endgo
'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 + ' ' + @strOrderendexec (@strSQL)end