I have this stored procedure
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 ActivityPlan_ID=@TempAPID and [ActivityRecurrence_ID]=@TempRecurID
--print 'CI Selection:'+@CISelection
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]=@TempRecurID 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 ActivityPlan_ID=@TempAPID and [ActivityRecurrence_ID]=@TempRecurID
end
else if(@CISelection='NA')
begin
select @Activities='ALL'
end
--print 'Activities:'+@Activities
select @IsAllLocs=[IsAllLocations] from [dbo].[ActivityPlan] where ActivityPlan_ID=@TempAPID and [ActivityRecurrence_ID]=@TempRecurID
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]=@TempRecurID and apf.FilterType='Locations'
if(LEN(@Locations)>0)
begin
select @Locations=LEFT(@Locations, LEN(@Locations) - 1)
end
end
--print 'Locations:'+@Locations
--print 'before updation'
update @TempLeisureActivites
set Activity=@Activities,Location=@Locations
where ActivityPlan_ID=@TempAPID and ActivityRecurrence_ID=@TempRecurID
--print 'after updation'
FETCH NEXT
FROM @RecurIDsCursor INTO @TempAPID,@TempRecurID
SET @RecurIDsRowsCount=@RecurIDsRowsCount-1
END
CLOSE @RecurIDsCursor
DEALLOCATE @RecurIDsCursor
end
select * from @TempLeisureActivites
end
It returns Result set while executing in SQL Server Management Studio but in Asp.net MVC using Entity Framework it returns an integer instead of Result Set like below.
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);
}
I found one article but it does not help me(Stored procedure returns int instead of result set). How Could i solve my problem?
INTspecifying the number of rows affected by an operation (likeINSERT,DELETE,UPDATE). For a stored procedure which does not use any of those operations, the return value from the stored procedure is defined to be -1. To get the result set from a stored procedure, try using the.ExecuteStoreQuery()method instead ofExecuteFunctionExecuteStoreQuery()instead ofExecuteFunctionto be able to get those rows .....sp_prefix for your stored procedures. Microsoft has reserved that prefix for its own use (see Naming Stored Procedures), and you do run the risk of a name clash sometime in the future. It's also bad for your stored procedure performance. It's best to just simply avoidsp_and use something else as a prefix - or no prefix at all!