62,041
社区成员
发帖
与我相关
我的任务
分享
USE [HeFan_cs]
GO
/****** Object: StoredProcedure [dbo].[P_GetRecordFromPage] Script Date: 07/18/2012 11:22:41 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[P_GetRecordFromPage]
@tblName nvarchar(1000), -- 表名
@SelectFieldName nvarchar(4000), -- 要显示的字段名(不要加select)
@strWhere nvarchar(4000), -- 查询条件(注意: 不要加 where)
@OrderFieldName nvarchar(255), -- 排序索引字段名
@orderby nvarchar(1000), --排序字段
@PageSize int , -- 页大小
@PageIndex int = 1, -- 页码
@iRowCount int output, -- 返回记录总数
@OrderType int = 0 -- 设置排序类型, 非 0 值则降序
AS
declare @strSQL nvarchar(4000) -- 主语句
declare @strTmp nvarchar(4000) -- 临时变量
declare @strOrder nvarchar(400) -- 排序类型
declare @strRowCount nvarchar(4000) -- 用于查询记录总数的语句
declare @temporderby nvarchar(1000) --最终排序字段
set @OrderFieldName=ltrim(rtrim(@OrderFieldName))
if @OrderType != 0
begin
set @strTmp = '<(select min'
set @strOrder = ' order by ' + @OrderFieldName +' desc'
set @temporderby = ' order by ' + @orderby + ' desc'
end
else
begin
set @strTmp = '>(select max'
set @strOrder = ' order by ' + @OrderFieldName +' asc'
set @temporderby = ' order by ' + @orderby + ' asc'
end
set @strSQL = 'select top ' + str(@PageSize) + @SelectFieldName+' from '
+ @tblName + ' where ' + @OrderFieldName + @strTmp + '('
+ right(@OrderFieldName,len(@OrderFieldName)-charindex('.',@OrderFieldName)) + ') from (select top ' + str((@PageIndex-1)*@PageSize)
+ @OrderFieldName + ' from ' + @tblName + @strOrder + ') as tblTmp)'
+ @strOrder
if @strWhere != ''
begin
set @strSQL = 'select top ' + str(@PageSize) + @SelectFieldName+' from '
+ @tblName + ' where ' + @OrderFieldName + @strTmp + '('
+ right(@OrderFieldName,len(@OrderFieldName)-charindex('.',@OrderFieldName)) + ') from (select top ' + str((@PageIndex-1)*@PageSize)
+ @OrderFieldName + ' from ' + @tblName + ' where ' + @strWhere + ' '
+ @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder
end
if @PageIndex = 1
begin
set @strTmp = ''
if @strWhere != ''
set @strTmp = ' where ' + @strWhere
set @strSQL = 'select top ' + str(@PageSize) + @SelectFieldName+' from '
+ @tblName + @strTmp + ' ' + @strOrder
end
exec(@strSQL)
print @strSQL
if @strWhere!=''
begin
set @strRowCount = 'select @iRowCount=count(*) from ' + @tblName+' where '+@strWhere
end
else
begin
set @strRowCount = 'select @iRowCount=count(*) from ' + @tblName
end
exec sp_executesql @strRowCount,N'@iRowCount int out',@iRowCount out
select top 15 [Category].title as catetitle,[Product].id,[Product].sold,[Product].userid,[Product].updatetime,
[Product].shopprice,[Product].title as protitle,[Product].imgpath,[Rant].rantname,[Rant].shopendtime
from [Product],[Rant],[Category]
where [Product].sold <
(select min ( sold) from
(select top 15 [Product].sold
from [Product],[Rant],[Category]
where [Product].issale=1 and [Product].state=23 and [Product].userid in (4) and [Product].city=23 and [Product].parentid<>44 and [Rant].id=[Product].userid and [Category].id=[Product].parentid
order by [Product].sold desc ) as tblTmp)
and [Product].issale=1 and [Product].state=23 and [Product].userid in (4) and [Product].city=23 and [Product].parentid<>44 and [Rant].id=[Product].userid and [Category].id=[Product].parentid
order by [Product].sold desc