mssql 创建存储过程简单实例

CREATE procedure [dbo].[cp_User_Increment]
    @channelId int,
    @currentPage int,
    @pageSize int,
    @userId int
as

if @channelId<=0
    begin
        select 
        CurrentTime,
        Count
        from
        (
            select 
            *,
            ROW_NUMBER() OVER(ORDER BY CurrentTime DESC) AS RowNum 
            from 
            (
                select 
                CONVERT(varchar(12), AddTime, 111 ) as CurrentTime,
                COUNT(1) as Count 
                from Users 
                where IsDel=0 and ChannelId in (select ChannelId from Channel_User where UserId=@userId)
                group by CONVERT(varchar(12), AddTime, 111) 
            )a 
        )b
        where RowNum BETWEEN (@currentPage-1)*@pageSize+1 and @currentPage*@pageSize
    end
else
    begin
    
        select 
        CurrentTime,
        Count
        from
        (
            select 
            *,
            ROW_NUMBER() OVER(ORDER BY CurrentTime DESC) AS RowNum 
            from 
            (
                select 
                CONVERT(varchar(12), AddTime, 111 ) as CurrentTime,
                COUNT(1) as Count 
                from Users
                where IsDel=0 and ChannelId=@channelId and ChannelId in (select ChannelId from Channel_User where UserId=@userId)
                group by CONVERT(varchar(12), AddTime, 111) 
            )a 
        )b
        where RowNum BETWEEN (@currentPage-1)*@pageSize+1 and @currentPage*@pageSize
    end

GO
原文地址:https://www.cnblogs.com/zoro-zero/p/5548536.html