レポートを生成する次のSQLを作成しました。私はいくつかのSQLクエリを実行し、その結果を一時テーブルに保存し、sprocの最後にすべてのテーブルを結合します。ただし、このsprocをEFに追加すると、複雑な型が作成されず、それがないと役に立ちません。返された結果セットを EF で識別し、複合型を作成するにはどうすればよいですか。
ALTER PROC [dbo].[Report_AllMediaDetails]
(
@profileID AS INT,
@organisationID as INT,
@startDate as date,
@endDate as date
)
AS
create table #Temp_ReportData
(
ItemOrgID int,
MediaName char(100),
CountryName char(100),
ItemOverRideDate DateTime,
Rating int,
BatchNo int,
NoInBatch int,
BatchDate DateTime
)
insert into #Temp_ReportData
select
oi.ID,
mc.Name as MediaName,
lcc.Name as Country,
i.OverrideDate as Date,
oi.Rating,
b.ID BatchNo,
i.NumInBatch ItemNumberInBatch,
bah.ChangedAtUtc as BatchDate
from
ProfileResults PR
INNER JOIN Items i ON PR.ItemID = I.ID
inner join Batches b on b.ID=i.BatchID
inner join ItemOrganisations oi on i.ID=oi.ItemID
inner join Lookup_MediaChannels mc on i.MediaChannelID=mc.id
left outer join Lookup_Cities lc on lc.ID=mc.CityID
left outer join Lookup_Countries lcc on lcc.ID=mc.CountryID
inner join BatchActionHistory bah on b.ID=bah.batchid
where pr.ProfileID = @profileID
and oi.OrganisationID = @organisationID
and b.StatusID IN (6,7)
and bah.BatchActionID = 6
and i.StatusID = 2
and i.IsRelevant = 1
and i.OverrideDate between @startDate and @endDate
--Create a temp table to hold the B section ByLines
create table #Temp_ReportByLines
(
ItemOrgID int,
ByLine char(100),
BatchNo int,
NumInBatch int,
BatchDate Date,
Rating int,
ItemOverRideDate date
)
insert into #Temp_ReportByLines
select
oi.ID,
bl1.FirstName +' '+ bl1.Surname as ByLine,
b.ID,
i.NumInBatch,
bah.ChangedAtUtc,
oi.rating,
i.OverrideDate
from ProfileResults PR
INNER JOIN Items i ON PR.ItemID = I.ID
inner join Batches b on b.ID=i.BatchID
inner join ItemOrganisations oi on i.ID=oi.ItemID
left outer join ItemByLines ib on ib.ItemID=i.ID
left outer join ByLines bl1 on bl1.ID=ib.ByLineID
inner join BatchActionHistory bah on b.ID=bah.batchid
where pr.ProfileID = @profileID
and oi.OrganisationID = @organisationID
and b.StatusID IN (6,7)
and bah.BatchActionID = 6
and i.StatusID = 2
and i.IsRelevant = 1
and bl1.IsByLine=1
and i.OverrideDate between @startDate and @endDate
--Create a temp table to hold the I section issues
create table #Temp_ReportIssues
(
ItemOrgID int,
IssueNo int,
Issue char(300),
BatchNo int,
NumInBatch int,
BatchDate Date,
Rating int,
ItemOverRideDate date
)
insert into #Temp_ReportIssues
select
iss.ItemOrganisationID,
pri.Code, pri.Name,
td.BatchNo,
td.NoInBatch,
td.BatchDate,
td.Rating,
td.ItemOverRideDate
from ItemOrganisationIssues iss
inner join #Temp_ReportData td on td.itemorgid= iss.ItemOrganisationID
inner join ProjectIssues pri on pri.ID=iss.IssueID
--Create a temp table to hold the M section messages
create table #Temp_ReportMessages
(
ItemOrgID int,
MessageType char(100),
MessageNo int,
BatchNo int,
NumInBatch int,
BatchDate Date,
Rating int,
itemOverRideDate date
)
insert into #Temp_ReportMessages
select
itemorgid, lmt.Name as MessageType ,pm.NeptuneMessageID as MessageNo,
td.BatchNo,
td.NoInBatch,
td.BatchDate,
td.Rating,
td.ItemOverRideDate
from #Temp_ReportData td
left outer join ItemOrganisationMessages iom on iom.ItemOrganisationID=td.ItemOrgID
left outer join ProjectMessages pm on iom.MessageID=pm.id
left outer join Lookup_MessageTypes lmt on lmt.ID=pm.MessageTypeID
--Create a temp table to hold the S section Source
create table #Temp_ReportSources
(
ItemOrgID int,
SourceFullName char(100),
SourceType char(20),
BatchNo int,
NumInBatch int,
BatchDate Date,
Rating int,
ItemOverRideDate date
)
insert into #Temp_ReportSources
select
ios.ItemOrganisationID,
bl2.FirstName+' '+bl2.Surname as SourceFullName,
lst.name as SourceTypeDesc,
td.BatchNo,
td.NoInBatch,
td.BatchDate,
td.Rating,
td.ItemOverRideDate
from ItemOrganisationSources ios
inner join #Temp_ReportData td on td.ItemOrgID = ios.ItemOrganisationID
inner join ByLines bl2 on bl2.ID=ios.ByLineID and bl2.IsByLine=0
left outer join Lookup_SourceTypes lst on lst.ID=ios.SourceTypeID
-- Union all the temp tables into a formatted resultset
select
td.MediaName,td.CountryName,td.ItemOverRideDate, td.Rating,td.BatchNo,td.NoInBatch, td.BatchDate, '' as DataType,'' as ByLine,'' as IssueNo,'' as Issue, '' MessageNo, '' as Message,'' as SourceName,'' as SourceType from #Temp_ReportData td
union select '','',tb.ItemOverRideDate,tb.rating,tb.batchno,tb.NumInBatch,tb.batchdate,'B', tb.ByLine,'','','','','','' from #Temp_ReportByLines tb
union select '','',tr.ItemOverRideDate,tr.rating,tr.batchno,tr.NumInBatch,tr.batchdate,'I','',tr.issueno,tr.issue,'','','', '' from #Temp_ReportIssues tr
union select '','',tm.ItemOverRideDate,tm.rating,tm.batchno,tm.NumInBatch,tm.batchdate,'M','','','',tm.messageno,tm.messagetype,'','' from #Temp_ReportMessages tm
union select '','',ts.ItemOverRideDate,ts.rating,ts.batchno,ts.NumInBatch,ts.batchdate,'S','','','','','',ts.sourcefullname,ts.sourcetype from #Temp_ReportSources ts
order by td.NoInBatch,td.BatchNo, datatype