2015-12-10 69 views
0

我有這個存儲過程存儲過程返回int值,而不是在實體結果集框架

create procedure [dbo].[sp_GetAllLesiureActivitiesNew] 
    (@ActivityLeaderID int = null) 
as 
begin 
    declare @TempLeisureActivites TABLE 
     ([ActivityPlan_ID] [int] NULL, 
     [ActivityRecurrence_ID] [int] NULL, 
     [ActivityName] [nvarchar](50) NULL, 
     [Activity] [nvarchar](max) NULL, 
     [IsResponse] [bit] NULL, 
     [IsLOI] [bit] NULL, 
     [clientcount] [int] NULL, 
     [Location] [nvarchar](max) NULL, 
     [StartDateTime] [datetime] NULL, 
     [EndDatetime] [datetime] NULL) 

    insert into @TempLeisureActivites 
     select distinct 
      ap.ActivityPlan_ID, ap.ActivityRecurrence_ID, 
      ap.ActivityName, orgla.Activity, orgla.IsResponse, 
      orgla.IsLOI, 
      (select count(distinct cc.ID) from Client cc join Activity_Clients acc on cc.ID=acc.Client_ID join ActivityPlan app on acc.ActivityRecurrence_ID=app.ActivityRecurrence_ID where app.ActivityRecurrence_ID=ap.ActivityRecurrence_ID and cc.Status=1) as clientcount,l.Location,ap.StartDateTime,ap.EndDatetime 
from ActivityPlan ap 
left outer join Location l on ap.ActivityLocationID =l.ID left join Activity_Clients ac on ap.ActivityRecurrence_ID=ac.ActivityRecurrence_ID 
left outer join Client c on ac.Client_ID=c.ID 
left outer join LeisureActivity orgla on ap.ActivityType=orgla.ID 
where ap.ActivityLeaderID in(0,@ActivityLeaderID) and c.[Status]=1 

    if(@ActivityLeaderID is not null and @ActivityLeaderID>0) 
    begin 


declare @Activities nvarchar(max) 
declare @Locations nvarchar(max) 
declare @CISelection nvarchar(50) 
declare @IsAllLocs bit 

declare @TempRecurID int 
declare @TempAPID int 

--BEGIN TRANSACTION T1 
--BEGIN TRY 

declare @RecurIDsCursor CURSOR 
declare @RecurIDsRowsCount int 

--print 'Before cursor logic starts' 
set @RecurIDsCursor=CURSOR STATIC FOR 
select ActivityPlan_ID,ActivityRecurrence_ID from @TempLeisureActivites 

OPEN @RecurIDsCursor 
set @RecurIDsRowsCount=(SELECT @@CURSOR_ROWS) 

--print 'cursor rows count:'+cast(@RecurIDsRowsCount as nvarchar) 

FETCH NEXT 
FROM @RecurIDsCursor INTO @TempAPID,@TempRecurID 

WHILE @RecurIDsRowsCount>0 
BEGIN 

--select @Activities=NULL,@Locations=NULL 
--print 'looping started...' 
select @Activities='',@Locations='' 

select @CISelection=NULL,@IsAllLocs=0 

--print 'Activity Plan ID'+cast(@TempAPID as nvarchar)+',Recur ID:'+ cast(@TempRecurID as nvarchar) 

select @CISelection=[CommonInterestsSelection] from [dbo].[ActivityPlan] where [email protected] and [ActivityRecurrence_ID][email protected] 


--print 'CI Selection:'[email protected] 

if(@CISelection='Specific') 
begin 
select @Activities+=(
case when la.Activity is not null then 
ISNULL(la.Activity,'')+',' end) from [dbo].[ActivityPlan_Filters] apf 
left outer join [dbo].[LeisureActivity] la on la.ID=apf.FilterID 
where [ActivityRecurrence_ID][email protected] and apf.FilterType='Common_Interests' 

if(LEN(@Activities)>0) 
begin 
select @Activities=LEFT(@Activities, LEN(@Activities) - 1) 
end 
end 
else if(@CISelection='Top') 
begin 
select @Activities=[CommonInterestValue] from [dbo].[ActivityPlan] where [email protected] and [ActivityRecurrence_ID][email protected] 
end 
else if(@CISelection='NA') 
begin 
select @Activities='ALL' 
end 

--print 'Activities:'[email protected] 
select @IsAllLocs=[IsAllLocations] from [dbo].[ActivityPlan] where [email protected] and [ActivityRecurrence_ID][email protected] 
if(@IsAllLocs=1) 
begin 
select @Locations='ALL' 
end 
else if(@IsAllLocs=0) 
begin 
select @Locations+=(
case when loc.Location is not null then 
ISNULL(loc.Location,'')+',' end) from [dbo].[ActivityPlan_Filters] apf 
left outer join [dbo].[Location] loc on loc.ID=apf.FilterID 
where [ActivityRecurrence_ID][email protected] and apf.FilterType='Locations' 

if(LEN(@Locations)>0) 
begin 
select @Locations=LEFT(@Locations, LEN(@Locations) - 1) 
end 
end 

--print 'Locations:'[email protected] 

--print 'before updation' 
update @TempLeisureActivites 
set [email protected],[email protected] 
where [email protected] and [email protected] 

--print 'after updation' 

FETCH NEXT 
FROM @RecurIDsCursor INTO @TempAPID,@TempRecurID 

SET @[email protected] 
END 
CLOSE @RecurIDsCursor 
DEALLOCATE @RecurIDsCursor 


end 
select * from @TempLeisureActivites 
end 

它返回結果,而使用實體框架,它返回一個在SQL Server Management Studio,但在Asp.net MVC執行設置整數而不是如下所示的結果集。

public virtual int sp_GetAllLesiureActivitiesNew(Nullable<int> activityLeaderID) 
     { 
      var activityLeaderIDParameter = activityLeaderID.HasValue ? 
       new ObjectParameter("ActivityLeaderID", activityLeaderID) : 
       new ObjectParameter("ActivityLeaderID", typeof(int)); 

      return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("sp_GetAllLesiureActivitiesNew", activityLeaderIDParameter); 
     } 

我找到了一篇文章,但它不幫我(Stored procedure returns int instead of result set)。 我如何解決我的問題?

+2

存儲過程的返回值是一個'INT',用於指定受操作影響的行數(如'INSERT','DELETE','UPDATE')。對於不使用任何這些操作的存儲過程,存儲過程的返回值定義爲-1。要從存儲過程獲得結果集,請嘗試使用'.ExecuteStoreQuery()'方法而不是'ExecuteFunction' –

+0

@marc_s上面的存儲過程返回在Sql Server管理工作室中執行時的行。它在sql server中返回正確的表結果。但是在實體框架中它返回整數而不是結果集。 – Lavanya

+0

是的 - 我知道 - 你說的 - 我試圖告訴你使用** ExecuteStoreQuery()**而不是ExecuteFunction'來獲取這些行** ..... –

回答

1

ExecuteFunction()方法返回作爲整數受到影響的行數。

如果你想要它返回ObjectResult<YourEntityType>來代替。 將其更改爲:

return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<YourEntityType>("sp_GetAllLesiureActivitiesNew", activityLeaderIDParameter); 

但我認爲,這樣做的更好的方法是現在:

Dbcontext.Database.SqlQuery<YourEntityType>("storedProcedureName",params); 

Here的一篇文章,給出了一個例子。

+0

在DbContext.cs中它返回如下所示的整數public virtual int sp_GetAllLesiureActivitiesNew(可爲空的 activityLeaderID){} – Lavanya

+0

如果您希望它返回ObjectResult ,您必須知道EntityType。看看我的更新!否則它只會以整數形式返回受影響的行數。 –

相關問題