我有一个存储过程,您可以在下面找到逻辑,现在当我将此 sp 带到 EF {版本 5.0} 时,此 sp 将返回类型为 int。
Create Proc SP_GetResultBasedOnFilterForCoverage
(
@AgeID int,
@IncomeID int,
@GenderID int,
@EducationID int,
@ProductID int
)
as
set nocount on
Declare @query varchar(max), @index int, @totalrow int, @coverageTotal bigint
IF OBJECT_ID('tempdb..#SecondDataSet') IS NOT NULL
DROP TABLE #SecondDataSet
Create table #SecondDataSet
(
CoverageID int,
Coverage varchar(100),
Percentage float
)
Declare @CoverageTable Table
(
id int identity(1,1),
CoverageID int,
Coverage varchar(100),
CoverageCount int
)
insert into @CoverageTable
(CoverageID,Coverage,CoverageCount)
Select TCPC.CoverageID,Coverage.Coverage, COUNT(CustomerID) from TBL_Customer_Product_Choice as TCPC
inner join
Tbl_Coverage as coverage
on
coverage.CoverageID = TCPC.CoverageID
group by
TCPC.CoverageID,coverage.Coverage
set @index = 1;
Select @totalrow = COUNT(ID) from @CoverageTable
While(@index <= @totalrow)
begin
Declare @CurrentCoverageID int
Select @coverageTotal = CoverageCount, @CurrentCoverageID = CoverageID from @CoverageTable where id = @index
set @query = '
Insert into #SecondDataSet
(Percentage,CoverageID,Coverage)
Select Round((Cast(COUNT(TCPC.CustomerID) as float)/cast('+cast(@coverageTotal as varchar)+' as bigint)) * 100,1) as Percentage ,
TCPC.CoverageID ,TC.Coverage
from Tbl_customer_Product_Choice as TCPC
inner join
Tbl_Coverage as TC
on
TCPC.CoverageID = TC.CoverageID
Where productID = cast('+cast(@ProductID as varchar)+'as int) and TCPC.CoverageID = cast('+ cast(@CurrentCoverageID as varchar)+' as int)'
if(@AgeID <> 0)
begin
set @query += ' and AgeID = ' + CAST(@AgeID as varchar);
End
if( @IncomeID != 0)
begin
set @query += ' and IncomeID = ' + CAST(@IncomeID as varchar);
End
if(@EducationID != 0)
begin
set @query += ' and EducationID = ' + CAST(@EducationID as varchar);
End
if(@GenderID > -1)
begin
set @query += ' and GenderID = ' + CAST(@GenderID as varchar);
End
set @query += ' group by TCPC.CoverageID,TC.Coverage
order by COUNT(TCPC.CustomerID) desc
'
Exec(@query)
set @index = @index + 1;
end
Select * from #SecondDataSet