经典分页存储过程

ContractedBlock.gifExpandedBlockStart.gifCode
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go





ALTER PROCEDURE [dbo].[sp_GetEntities]
@PageSize 
int,
@sqlFullPopulate varchar(
4000),
@sqlPopulate varchar(
4000),
@orderSql varchar(
400),
@GetRelatedEntities bit,
@GetChildEntities bit,
@RelatedTypes varchar(
200),
@ReturnFields varchar(
4000),
@RelatedEntityReturnFields varchar(
4000),
@ChildEntityReturnFields varchar(
4000),
@TotalRecords 
int output
AS

SET NOCOUNT ON

CREATE TABLE #t
(
    IndexID 
int IDENTITY (11) NOT NULL,
    EntityID 
int
)
CREATE TABLE #EntityIDTable
(
    EntityID 
int
)
CREATE TABLE #RelatedPairEntityIDTableTemp
(
    IndexID 
int IDENTITY (11) NOT NULL,
    EntityID 
int,
    RelatedEntityID 
int,
    RelationType 
int
)
CREATE TABLE #RelatedEntityIDTableTemp
(
    IndexID 
int IDENTITY (11) NOT NULL,
    EntityID 
int
)
CREATE TABLE #RelatedEntityIDTable
(
    EntityID 
int
)
--Get total records count
declare @sql nvarchar(
4000)
set @sql= N'select @TotalRecords=count(*) from ' + N'(' + @sqlFullPopulate + N') a '
EXEC sp_executesql @sql,N
'@TotalRecords int output', @TotalRecords output

-- Get the records which in the current page
set @sql = 'insert into #t(entityid) select top ' + cast(@pageSize as varchar(20)) + ' entityid from (' + @sqlPopulate + ') a ' + @orderSql
EXEC (@sql)
insert into #EntityIDTable select entityid from #t order by IndexID desc

--Get the main entities
set @sql = 'select ' + @ReturnFields + ' from #EntityIDTable eid inner join tb_entities E on eid.entityid = e.entityid'
EXEC (@sql) 

--Get the related entities
if @GetRelatedEntities = 1 and (not @RelatedTypes is null) and @RelatedTypes <> ''
begin
    
set @sql = 'insert into #RelatedPairEntityIDTableTemp(entityid,relatedentityid,relationtype) select er.entityid,er.relatedentityid,er.relationtype from tb_entityrelations er where er.relationtype in (' + @RelatedTypes + ') and (er.entityid in (select eid.entityid from #entityidtable eid) or er.relatedentityid in (select eid.entityid from #entityidtable eid))'
    exec (@sql)
    insert into #RelatedEntityIDTableTemp(entityid) select entityid from #RelatedPairEntityIDTableTemp 
where entityid not in (select eid.entityid from #entityidtable eid)
    insert into #RelatedEntityIDTableTemp(entityid) select relatedentityid from #RelatedPairEntityIDTableTemp 
where relatedentityid not in (select eid.entityid from #entityidtable eid)
    insert into #RelatedEntityIDTable(entityid) select distinct entityid from #RelatedEntityIDTableTemp
    
set @sql = 'select ' + @RelatedEntityReturnFields + ' from #RelatedEntityIDTable reid join tb_entities e on reid.entityid = e.entityid'
    exec (@sql)
    select entityid,RelatedEntityID,relationtype from #RelatedPairEntityIDTableTemp
end

-- Get the child entities
if @GetChildEntities = 1
begin    
    
set @sql = 'select ' + @ChildEntityReturnFields + ' from tb_entities e where e.parentid in (select entityid as parentid from #entityidtable)'
    exec (@sql)
end

DROP TABLE #t
DROP TABLE #EntityIDTable
DROP TABLE #RelatedPairEntityIDTableTemp
DROP TABLE #RelatedEntityIDTableTemp
DROP TABLE #RelatedEntityIDTable

SET NOCOUNT OFF






转载于:https://www.cnblogs.com/duwamish/archive/2008/12/01/1344932.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值