62,267
社区成员
发帖
与我相关
我的任务
分享
CREATE PROCEDURE [dbo].[Pagination]
@TotalCount INT OUTPUT,
@TotalPage INT OUTPUT,
@Table NVARCHAR(1000), --将要查询的表名
@Column NVARCHAR(1000),--将要查询的字段,可多列
@OrderColumn NVARCHAR(100), --排序字段
@GroupColumn NVARCHAR(150),--分组字段
@PageSize INT,--分页大小
@CurrentPage INT, --要查询的页
@Group tinyint,--是否使用分组,0否1是
@Condition NVARCHAR(4000) --查询条件
AS
DECLARE @PageCount INT,--总页数
@strSql NVARCHAR(4000), --主查询语句
@strTemp NVARCHAR(2000), --临时变量
@strCount NVARCHAR(1000),--统计语句
@strOrderType NVARCHAR(1000) --排序语句
BEGIN
SET @PageCount=@PageSize*(@CurrentPage-1)
SET @strOrderType=' ORDER BY ' + @OrderColumn +' '
IF @Condition!=''
BEGIN
IF @CurrentPage=1
BEGIN
IF @GROUP=1
BEGIN
SET @strCount='SELECT @TotalCount=COUNT(*) FROM '+ @Table+ ' WHERE '+@Condition+' GROUP BY '+@GroupColumn
SET @strCount=@strCount+' SET @TotalCount=@@ROWCOUNT'
SET @strSql='SELECT TOP '+ str(@PageSize) +' '+ @Column +' FROM '+ @Table + ' WHERE ' + @Condition+' GROUP BY '+@GroupColumn + ' '+@strOrderType
END
ELSE
BEGIN
SET @strCount='SELECT @TotalCount=COUNT(*) FROM '+ @Table+ ' WHERE '+@Condition
SET @strSql='SELECT TOP '+ str(@PageSize) +' '+ @Column +' FROM '+ @Table + ' WHERE ' + @Condition + ' '+@strOrderType
END
END
ELSE
BEGIN
IF @GROUP=1
BEGIN
SET @strCount='SELECT @TotalCount=COUNT(*) FROM '+ @Table+ ' WHERE '+@Condition+' GROUP BY '+@GroupColumn
SET @strCount=@strCount+' SET @TotalCount=@@ROWCOUNT'
SET @strSql='SELECT * FROM (SELECT TOP (2000) '+ @Column +',ROW_NUMBER() OVER('+@strOrderType+') AS NUM FROM '+@Table+' WHERE '+@Condition+' GROUP BY '+@GroupColumn+') AS T WHERE NUM BETWEEN '+str(@PageCount+1)+ ' AND '+str(@PageCount+@PageSize)
END
ELSE
BEGIN
SET @strCount='SELECT @TotalCount=COUNT(*) FROM '+ @Table+ ' WHERE '+@Condition
SET @strSql='SELECT * FROM (SELECT TOP (2000) '+ @Column +',ROW_NUMBER() OVER('+@strOrderType+') AS NUM FROM '+@Table+' WHERE '+@Condition+') AS T WHERE NUM BETWEEN '+str(@PageCount+1)+ ' AND '+str(@PageCount+@PageSize)
END
END
END
ELSE--没有查询条件
BEGIN
IF @CurrentPage=1
BEGIN
IF @GROUP=1
BEGIN
SET @strCount='SELECT @TotalCount=COUNT(*) FROM '+ @Table+' GROUP BY '+@GroupColumn
SET @strCount=@strCount+'SET @TotalCount=@@ROWCOUNT'
SET @strSql='SELECT TOP '+ str(@PageSize) +' '+ @Column +' FROM '+ @Table+' GROUP BY '+@GroupColumn +' '+@strOrderType
END
ELSE
BEGIN
SET @strCount='SELECT @TotalCount=COUNT(*) FROM '+ @Table
SET @strSql='SELECT TOP '+ str(@PageSize) +' '+ @Column +' FROM '+ @Table +' '+@strOrderType
END
END
ELSE
BEGIN
IF @GROUP=1
BEGIN
SET @strCount='SELECT @TotalCount=COUNT(*) FROM '+ @Table+' GROUP BY '+@GroupColumn
SET @strCount=@strCount+'SET @TotalCount=@@ROWCOUNT'
SET @strSql='SELECT * FROM (SELECT TOP (2000) '+ @Column +',ROW_NUMBER() OVER('+@strOrderType+') AS NUM FROM '+@Table+' GROUP BY '+@GroupColumn+') AS T WHERE NUM BETWEEN '+STR(@PageCount+1)+ ' AND '+STR(@PageCount+@PageSize)
END
ELSE
BEGIN
SET @strCount='SELECT @TotalCount=COUNT(*) FROM '+ @Table
SET @strSql='SELECT * FROM (SELECT TOP (2000) '+ @Column +',ROW_NUMBER() OVER('+@strOrderType+') AS NUM FROM '+@Table+') AS T WHERE NUM BETWEEN '+STR(@PageCount+1)+ ' AND '+STR(@PageCount+@PageSize)
END
END
END
EXEC sp_executesql @strCount,N'@TotalCount INT OUTPUT',@TotalCount OUTPUT
IF @TotalCount>2000
BEGIN
SET @TotalCount=2000
END
IF @TotalCount%@PageSize=0
BEGIN
SET @TotalPage=@TotalCount/@PageSize
END
ELSE
BEGIN
SET @TotalPage=@TotalCount/@PageSize+1
END
SET NOCOUNT ON
EXEC (@strSql)
END
SET NOCOUNT OFF
/*****************************************************************
发帖
******************************************************************/
alter proc sendtopics
@sessionid varchar(60), 板块ID
@username varchar(30), 用户名
@topic varchar(255), 主体
@topictext varchar(8000), 发贴的正文
@islight int , 是否使用发光字
@istop int, 是否置顶
@ismessage int, 是否是公告信息
@addscore int 此贴子的分数
as
begin
declare @return int--用于记录返回值
declare @userid varchar(80)--用户ID
select @userid = id from users where username =@username 根据用户名获得用户ID
if exists (select * from blacklist
where userid=@userid and Sessionid=@sessionid
and datediff(dd,closeTime ,getdate())<=closeDays )
set @return = -1 //判断在此板块中次用户是否处在小黑屋 是则返回-1
else
begin
if not exists(select * from SendTopic
where Userid=@userid and datediff(dd,SendTime,getdate())=0)
update users set userintegral =userintegral +30 where id=@userid
insert into sendtopic values(newid(),@sessionid,@userid,@topic,@topictext,0,@ismessage,@istop,@addscore,0,getdate(),@userid,getdate(),0,0,@islight)
判断今天该用户是否发过帖子 如果未发过帖子 加30分 并插入数据库
update users set userintegral = userintegral-@addscore where id=@userid
if not exists(select * from UserSortPoint where userid =@userid and sessionid =@sessionid)
insert into UserSortPoint values (newid(),@userid,@sessionid,0) 然后 在减去该帖子的分数
if(@islight= 1) 在判断是否使用了发光字
update users set userintegral = userintegral-10 where id=@userid
set @return = 1
end
select @return 根据return的结果判断该用户是否处在小黑屋中
end
ALTER proc [dbo].[usp_daily_work_select]
@flag int,
@owner nvarchar(50),
@status nvarchar(50),
@project nvarchar(50),
@startdate1 datetime,
@startdate2 datetime,
@duedate1 datetime,
@duedate2 datetime,
@description nvarchar(2000),
@updatedatetime datetime,
@updatename nvarchar(50),
@type nvarchar(50),
@item nvarchar(50),
@code nvarchar(50),
@pagesize int,
@pageindex int
AS
Begin
--查询时显示的资料
if(@flag=1)
begin
if(@status='Delete')
begin
with temptbl as
(
select ROW_NUMBER()over(order by project asc,(right([Code],5)) desc) AS Number,
dw.[No],dw.Project,dw.Item,dw.Code,dw.[Type],dw.[Description (purpose/value)],dw.Status,dw.[%],
Convert(Varchar(10),dw.[Start Date],120)AS [Start Date],Convert(Varchar(10),dw.[Due Date],120)AS [Due Date],dw.Owner,dw.[Day/Hour],dw.Comment,dw.Updatedatetime,dw.UpdateName
from daily_work dw
where dw.Status='Delete'
and dw.Item like '%'+@item+'%'
and dw.Owner like ''+@owner+'%'
and dw.Status like '%'+@status+'%'
and dw.Project like '%'+@project+'%'
and ((dw.[Start Date] between @startdate1 and @startdate2) or dw.[Start Date] IS null)
and ((dw.[Due Date] between @duedate1 and @duedate2 )or dw.[Due Date] IS null)
and dw.[Description (purpose/value)] like '%'+@description+'%'
)
select * from temptbl where Number>=((@pageindex-1)*@pagesize+1)
and Number<=(@pageindex*@pagesize) order by project asc,(right([Code],5)) desc
end
else
begin
with temptbl as
(
select
ROW_NUMBER()over(order by project asc,(right([Code],5)) desc) AS Number,
dw.[No],dw.Project,dw.Item,dw.Code,dw.[Type],dw.[Description (purpose/value)],dw.Status,dw.[%],
Convert(Varchar(10),dw.[Start Date],120)AS [Start Date],Convert(Varchar(10),dw.[Due Date],120)AS [Due Date],dw.Owner,dw.[Day/Hour],dw.Comment,dw.Updatedatetime,dw.UpdateName
from daily_work dw
where dw.Owner like ''+@owner+'%'
and dw.Status like '%'+@status+'%'
and dw.Item like '%'+@item+'%'
and dw.Project like '%'+@project+'%'
and ((dw.[Start Date] between @startdate1 and @startdate2) or dw.[Start Date] IS null)
and ((dw.[Due Date] between @duedate1 and @duedate2 )or dw.[Due Date] IS null)
and dw.[Description (purpose/value)] like '%'+@description+'%'
and dw.Status<>'Delete'
)
select * from temptbl where Number>=((@pageindex-1)*@pagesize+1)
and Number<=(@pageindex*@pagesize) order by project asc,(right([Code],5)) desc
end
end