微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

善用临时表---同时查询多张表的存储过程

一个存储过程的需求是这样的:
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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐