存储过程里分页的例子
Posted 努力向上的小蜗牛
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了存储过程里分页的例子相关的知识,希望对你有一定的参考价值。
1 /****** Object: StoredProcedure [dbo].[CBM_ProjectInfo_BudgetInfo] Script Date: 2016/10/20 16:10:06 ******/ 2 SET ANSI_NULLS ON 3 GO 4 5 SET QUOTED_IDENTIFIER ON 6 GO 7 8 9 create proc [dbo].[CBM_ProjectInfo_BudgetInfo] 10 @Pagesize int, 11 @CurrentPageIndex int, 12 13 @xmmc varchar(400),--项目名称 14 @pm varchar(50),--项目经理 15 @Flag varchar(3)--1 未初始化 2-金额为0 16 17 as 18 begin 19 create Table #temp_table(RowGuid varchar(50), 20 ContractName varchar(500), 21 LiXiangRenDeptGuid varchar(50), 22 [Year] int, 23 weihu_ET datetime, 24 ZongJinE_HT decimal(18,2), 25 ZongJinE_LX decimal(18,2), 26 KeHuGuid varchar(50), 27 LiXiangRenGuid varchar(50), 28 DiQuValue varchar(50), 29 XiangMuBH varchar(50), 30 XiangMuMC varchar(500), 31 KeHuDW varchar(50), 32 33 LiXiangRen varchar(50), 34 LiXiangSJ datetime, 35 HeTongZT varchar(50), 36 ProjectRank varchar(50), 37 JinZhanJD varchar(50), 38 MaintenanceType varchar(50), 39 XiangMuLX varchar(50), 40 41 IsGuiDang varchar(50), 42 recordtype varchar(50), 43 FinishPersent decimal(18,2), 44 CheckFrequency decimal(18,0), 45 ProcessVersionInstanceGuid varchar(50), 46 SoftMoney decimal(18,2), 47 LastCheckTime datetime, 48 LastSetTime datetime, 49 IsYY varchar(50), 50 ProjectManager varchar(50), 51 52 BudgetGuid1 varchar(50),--标前 1 53 BudgetName1 varchar(500), 54 Total1 decimal(10,0), 55 TotalA1 decimal(10,0), 56 Total_Remain1 decimal(10,0), 57 IsHave1 varchar(3), 58 59 BudgetGuid2 varchar(50),--标后 2 60 BudgetName2 varchar(500), 61 Total2 decimal(10,0), 62 TotalA2 decimal(10,0), 63 Total_Remain2 decimal(10,0), 64 IsHave2 varchar(3), 65 66 BudgetGuid3 varchar(50),--生产任务单 3 (包括采购-7 和指定外包-8) 67 BudgetName3 varchar(500), 68 Total3 decimal(10,0), 69 TotalA3 decimal(10,0), 70 Total_Remain3 decimal(10,0), 71 IsHave3 varchar(3), 72 73 BudgetGuid4 varchar(50),--维护 4 74 BudgetName4 varchar(200), 75 Total4 decimal(10,0), 76 TotalA4 decimal(10,0), 77 Total_Remain4 decimal(10,0), 78 IsHave4 varchar(3), 79 80 BudgetGuid5 varchar(50),--验收 5 81 BudgetName5 varchar(500), 82 Total5 decimal(18,0), 83 TotalA5 decimal(18,0), 84 Total_Remain5 decimal(18,0), 85 IsHave5 varchar(3) 86 ) 87 88 create Table #temp_table2(RowGuid varchar(50), 89 ContractName varchar(500), 90 LiXiangRenDeptGuid varchar(50), 91 [Year] int, 92 weihu_ET datetime, 93 ZongJinE_HT decimal(18,2), 94 ZongJinE_LX decimal(18,2), 95 KeHuGuid varchar(50), 96 LiXiangRenGuid varchar(50), 97 DiQuValue varchar(50), 98 XiangMuBH varchar(50), 99 XiangMuMC varchar(500), 100 KeHuDW varchar(50), 101 102 LiXiangRen varchar(50), 103 LiXiangSJ datetime, 104 HeTongZT varchar(50), 105 ProjectRank varchar(50), 106 JinZhanJD varchar(50), 107 MaintenanceType varchar(50), 108 XiangMuLX varchar(50), 109 110 IsGuiDang varchar(50), 111 recordtype varchar(50), 112 FinishPersent decimal(18,2), 113 CheckFrequency decimal(18,0), 114 ProcessVersionInstanceGuid varchar(50), 115 SoftMoney decimal(18,2), 116 LastCheckTime datetime, 117 LastSetTime datetime, 118 IsYY varchar(50), 119 ProjectManager varchar(50), 120 121 BudgetGuid1 varchar(50),--标前 1 122 BudgetName1 varchar(500), 123 Total1 decimal(10,0), 124 TotalA1 decimal(10,0), 125 Total_Remain1 decimal(10,0), 126 IsHave1 varchar(3), 127 128 BudgetGuid2 varchar(50),--标后 2 129 BudgetName2 varchar(500), 130 Total2 decimal(10,0), 131 TotalA2 decimal(10,0), 132 Total_Remain2 decimal(10,0), 133 IsHave2 varchar(3), 134 135 BudgetGuid3 varchar(50),--生产任务单 3 (包括采购-7 和指定外包-8) 136 BudgetName3 varchar(500), 137 Total3 decimal(10,0), 138 TotalA3 decimal(10,0), 139 Total_Remain3 decimal(10,0), 140 IsHave3 varchar(3), 141 142 BudgetGuid4 varchar(50),--维护 4 143 BudgetName4 varchar(500), 144 Total4 decimal(10,0), 145 TotalA4 decimal(10,0), 146 Total_Remain4 decimal(10,0), 147 IsHave4 varchar(3), 148 149 BudgetGuid5 varchar(50),--验收 5 150 BudgetName5 varchar(500), 151 Total5 decimal(18,0), 152 TotalA5 decimal(18,0), 153 Total_Remain5 decimal(18,0), 154 IsHave5 varchar(3) 155 ) 156 157 --初始化人员信息 158 insert into #temp_table(RowGuid,ContractName,LiXiangRenDeptGuid,[Year],weihu_ET,ZongJinE_HT,ZongJinE_LX,KeHuGuid,LiXiangRenGuid, 159 DiQuValue,XiangMuBH,XiangMuMC,KeHuDW,LiXiangRen,LiXiangSJ,HeTongZT,ProjectRank,JinZhanJD,MaintenanceType, 160 XiangMuLX,IsGuiDang,recordtype,FinishPersent,CheckFrequency,ProcessVersionInstanceGuid,SoftMoney, 161 LastCheckTime,LastSetTime,IsYY,ProjectManager, 162 BudgetGuid1,BudgetName1,Total1,TotalA1,Total_Remain1,IsHave1, 163 BudgetGuid2,BudgetName2,Total2,TotalA2,Total_Remain2,IsHave2, 164 BudgetGuid3,BudgetName3,Total3,TotalA3,Total_Remain3,IsHave3, 165 BudgetGuid4,BudgetName4,Total4,TotalA4,Total_Remain4,IsHave4, 166 BudgetGuid5,BudgetName5,Total5,TotalA5,Total_Remain5,IsHave5 167 ) 168 (select RowGuid,ContractName,LiXiangRenDeptGuid,[Year],weihu_ET,ZongJinE_HT,ZongJinE_LX,KeHuGuid,LiXiangRenGuid, 169 DiQuValue,XiangMuBH,XiangMuMC,KeHuDW,LiXiangRen,LiXiangSJ,HeTongZT,ProjectRank,JinZhanJD,MaintenanceType, 170 XiangMuLX,IsGuiDang,recordtype,FinishPersent,CheckFrequency,ProcessVersionInstanceGuid,SoftMoney, 171 LastCheckTime,LastSetTime,IsYY,\'\', 172 \'\',\'\',null,null,null,\'0\', 173 \'\',\'\',null,null,null,\'0\', 174 \'\',\'\',null,null,null,\'0\', 175 \'\',\'\',null,null,null,\'0\', 176 \'\',\'\',null,null,null,\'0\' 177 from View_ProjectManage WHERE JinZhanJD IN (\'16\',\'15\',\'18\',\'20\',\'21\',\'19\')) 178 179 update #temp_table set ProjectManager=b.username from #temp_table a,ProjectUsers b where a.RowGuid=b.projectguid and RoleID=\'31\' and IsMain=\'1\' 180 181 update #temp_table set BudgetGuid1=b.RowGuid,BudgetName1=b.BudgetName,Total1=b.Total,TotalA1=b.TotalA,Total_Remain1=b.Total-b.TotalA,IsHave1=\'1\' 182 from #temp_table a,CBM_BudgetInfo b where b.ProjectGuid=a.RowGuid and (b.PBudgetGuid is null or b.PBudgetGuid=\'\') and b.BudgetForm=1 183 184 update #temp_table set BudgetGuid2=b.RowGuid,BudgetName2=b.BudgetName,Total2=b.Total,TotalA2=b.TotalA,Total_Remain2=b.Total-b.TotalA,IsHave2=\'1\' 185 from #temp_table a,CBM_BudgetInfo b where b.ProjectGuid=a.RowGuid and (b.PBudgetGuid is null or b.PBudgetGuid=\'\') and b.BudgetForm=2 186 187 update #temp_table set BudgetGuid3=b.RowGuid,BudgetName3=b.BudgetName,Total3=b.Total,TotalA3=b.TotalA,Total_Remain3=b.Total-b.TotalA,IsHave3=\'1\' 188 from #temp_table a,CBM_BudgetInfo b where b.ProjectGuid=a.RowGuid and (b.PBudgetGuid is null or b.PBudgetGuid=\'\') and b.BudgetForm=3 189 190 update #temp_table set BudgetGuid4=b.RowGuid,BudgetName4=b.BudgetName,Total4=b.Total,TotalA4=b.TotalA,Total_Remain4=b.Total-b.TotalA,IsHave4=\'1\' 191 from #temp_table a,CBM_BudgetInfo b where b.ProjectGuid=a.RowGuid and (b.PBudgetGuid is null or b.PBudgetGuid=\'\') and b.BudgetForm=4 192 193 update #temp_table set BudgetGuid5=b.RowGuid,BudgetName5=b.BudgetName,Total5=b.Total,TotalA5=b.TotalA,Total_Remain5=b.Total-b.TotalA,IsHave5=\'1\' 194 from #temp_table a,CBM_BudgetInfo b where b.ProjectGuid=a.RowGuid and (b.PBudgetGuid is null or b.PBudgetGuid=\'\') and b.BudgetForm=5 195 196 if(@Flag=\'0\') 197 begin 198 199 insert into #temp_table2 select * from #temp_table where #temp_table.XiangMuMC like \'%\'+@xmmc+\'%\' and #temp_table.projectmanager like \'%\'+@pm+\'%\' 200 end 201 202 if(@Flag=\'1\') 203 begin 204 205 insert into #temp_table2 select * from #temp_table where #temp_table.XiangMuMC like \'%\'+@xmmc+\'%\' 206 and #temp_table.projectmanager like \'%\'+@pm+\'%\' and #temp_table.IsHave3=0 207 end 208 if(@Flag=\'2\') 209 begin 210 211 insert into #temp_table2 select * from #temp_table where #temp_table.XiangMuMC like \'%\'+@xmmc+\'%\' 212 and #temp_table.projectmanager like \'%\'+@pm+\'%\' and #temp_table.IsHave3=1 and isnull(#temp_table.Total3,0)=0 213 end 214 215 --output 216 declare @StrSql varchar(max) 217 declare @TopIndex int 218 set @TopIndex = (@CurrentPageIndex-1)*@Pagesize 219 set @StrSql=\'select top \'+str(@Pagesize)+\' * from #temp_table2 where RowGuid not in (select top \'+str(@TopIndex)+\' RowGuid from #temp_table2 order by LiXiangSJ desc) order by LiXiangSJ desc\' 220 221 exec (@StrSql) 222 select COUNT(*) as TotalCount from #temp_table2 223 end 224 225 226 227 GO
以上是关于存储过程里分页的例子的主要内容,如果未能解决你的问题,请参考以下文章