SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: yuanwensheng
-- Create date: 2008-4-25
-- Description: 相册评论
-- =============================================
alter PROCEDURE SelectComments
@strGetFields varchar(1000) = '*', -- 需要返回的列
@fldName varchar(255)='', -- 排序的字段名
@PageSize int = 10, -- 页尺寸
@PageIndex int = 1, -- 页码
@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
@strWhere varchar(1500) = '' -- 查询条件 (注意: 不要加 where)
AS
declare @tblName varchar(255) --表名
declare @strSQL varchar(5000) -- 主语句
declare @strTmp varchar(110) -- 临时变量
declare @strOrder varchar(400) -- 排序类型
declare @sql varchar(2000) -- 生成临时表时用到的变量
declare @count int -- 个人相册的表数量
begin
--把所有需要的数据导入临时表 开始
create table #tbTmp(CommentID int,PictureID bigint,UserID bigint,CommentUserID bigint,CommentUserName nvarchar(50),Content nvarchar(1000),AddedTime datetime,tbtable nvarchar(255));
--因为要有删除记录操作 所以得CommentID(仅是单表的主键) 和tbtable 组合做主键才可以
set @count=1;
if @strWhere!=''
begin
set @sql='select *,''PicturesComments_0'' tbtable from PicturesComments_0 where '+@strWhere;
end
else
begin
set @sql='select *,''PicturesComments_0'' from PicturesComments_0';
end
--循环开始
WHILE @count<10
begin
if @strWhere!=''
begin
set @sql=@sql+' union all select *,''PicturesComments_'+cast(@count as varchar)+''' from PicturesComments_'+cast(@count as varchar)+' where '+@strWhere;
end
else
begin
set @sql=@sql+' union all select *,''PicturesComments_'+cast(@count as varchar)+''' from PicturesComments_'+cast(@count as varchar);
end
set @count=@count+1;
end
--循环结束
insert into #tbTmp exec (@sql);
-- 结束
set @tblName = '#tbTmp';
if @strWhere !=''
set @strSQL = 'select count(*) as Total from [' + @tblName + '] where '+@strWhere
else
set @strSQL = 'select count(*) as Total from [' + @tblName + ']'
exec (@strSQL);
if @OrderType != 0
begin
set @strTmp = '<(select min'
set @strOrder = ' order by [' + @fldName +'] desc'
--如果@OrderType不是0,就执行降序,这句很重要!
end
else
begin
set @strTmp = '>(select max'
set @strOrder = ' order by [' + @fldName +'] asc'
end
if @PageIndex<1
begin
set @PageIndex=1
--如果输入负值变为第一页
end
if @PageIndex = 1
begin
-- if @strWhere != ''
--
-- set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from [' + @tblName + '] where ' + @strWhere + ' ' + @strOrder
--
-- else
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from ['+ @tblName + '] '+ @strOrder
--如果是第一页就执行以上代码,这样会加快执行速度
end
else
begin
--以下代码赋予了@strSQL以真正执行的SQL代码
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' 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) +' '+@strGetFields+ ' from ['
--
-- + @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
--
-- + @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
--
-- + @fldName + '] from [' + @tblName + '] where ' + @strWhere + ' '
--
-- + @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder
end
end
exec (@strSQL)
GO