1、取出三张表里的符合条件的数据,合成一张表,并对这一张表分页;
2、计算合成的这张表总共多少条数据
存储过程语句如下:
Create PROCEDURE GetAnnouncementList
@pageSize int,
@pageIndex int
AS
BEGIN
--不返回影响的行数,提高性能--
SET NOCOUNT ON;
declare @rowCount int
--将数据查询出来放入临时表#temptable中,语法:“ select 字段 into 临时表名 from ” --
select ROW_NUMBER()over (order by #temptable.CreatedOn desc)as tempNum,* into #temptable from
(select 'markNews' as mark,News.NewsID,News.NewsTitle,News.CreatedOn from dbo.News where categoryID ='8b041bf4-24bf-4b35-85c7-f72717dc5752'
union all
select 'markActivity' as mark,ActivitIEs.ActivityID,ActivitIEs.Subject,ActivitIEs.CreatedOn from dbo.ActivitIEs where ActivitIEs.CheckStatus=1 and IsDeleted=0 and ActivitIEs.RealEndTime>(select GETDATE())
union all
select 'markTrain' as mark,Train.TrainID,Train.Subject,Train.CreatedOn from dbo.Train where CheckStatus=1 and Train.PlannedEndTime>(select GETDATE()))as #temptable
--计算临时表中的数据条数--
select @rowCount=COUNT(*) from #temptable
--对临时表中的数据分页读取-- select * from #temptable where #temptable.tempNum>(@pageIndex-1)*@pageSize and #temptable.tempNum<=(@pageIndex*@pageSize) return @rowCount END
总结以上是内存溢出为你收集整理的善用临时表---同时查询多张表的存储过程全部内容,希望文章能够帮你解决善用临时表---同时查询多张表的存储过程所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)