Code
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 (1, 1) NOT NULL,
EntityID int
)
CREATE TABLE #EntityIDTable
(
EntityID int
)
CREATE TABLE #RelatedPairEntityIDTableTemp
(
IndexID int IDENTITY (1, 1) NOT NULL,
EntityID int,
RelatedEntityID int,
RelationType int
)
CREATE TABLE #RelatedEntityIDTableTemp
(
IndexID int IDENTITY (1, 1) 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
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 (1, 1) NOT NULL,
EntityID int
)
CREATE TABLE #EntityIDTable
(
EntityID int
)
CREATE TABLE #RelatedPairEntityIDTableTemp
(
IndexID int IDENTITY (1, 1) NOT NULL,
EntityID int,
RelatedEntityID int,
RelationType int
)
CREATE TABLE #RelatedEntityIDTableTemp
(
IndexID int IDENTITY (1, 1) 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