Tuesday, 28 August 2018

The stored procedure does not show the result

I have created a stored procedure (to be used in SSRS) that has parameters whose values are more than one. When I execute this stored procedure in SSMS by providing the values for that parameter, SP doesn't return any result. It should return more than 1 rows

Below is the SP given
CREATE PROCEDURE [dbo].[sp_tst_CSENG_JulieCapitalHours]
        @StartDate DATETIME ,
        @EndDate DATETIME ,
        @ProjHomeGrp NVARCHAR(MAX) ,
        @ProjHier NVARCHAR(MAX)
AS
BEGIN
    SELECT  [Capital Project] ,
                    [Capital Task] ,
                    ResourceName ,
                    ProjectName ,
                    [Project Home Group] ,
                    ActualWork ,
                    TimeByDay ,
                    ResourceStandardRate ,
                    ActualWork * ResourceStandardRate AS Dollars ,
                    [Project Hierarchy]
    FROM [IR.CapOnly]
    WHERE ( TimeByDay >= @StartDate )
      AND ( [Project Home Group] IN ( @ProjHomeGrp ) )
      AND ( TimeByDay <= @EndDate )
      AND ( ActualWork > 0 )
      AND ( [Project Hierarchy] IN ( @ProjHier ) )
    ORDER BY ProjectName ,
             ResourceName
END

You can see that in the where clause, Project Home Group (@ProjHomeGrp) and Project Hierarchy (@ProjHier) are the parameters, whose value when supplied (more than 1) SP returns zero values.
The value that I'm passing are:
Start Date: 1/1/2011
End Date: 12/31/2012
@ProjHomeGrp : PHG1,PHG2,PHG3,PHG4,PHG5,PHG6,PHG7
@ProjHier: PROH1, PROH2, PROH3

Let me know for any questions!

As Oded said, table valued parameters are the way to go. However, here is a solution based on dynamic sql, with all the problems that might imply...
create procedure [dbo].[sp_tst_CSENG_JulieCapitalHours]
        @StartDate datetime ,
        @enddate datetime ,
        @ProjHomeGrp nvarchar(MAX) ,
        @ProjHier nvarchar(MAX)
    as
begin
    declare @sql nvarchar(max)
    declare @paramDefs nvarchar(max)

    select @sql = N'select  [Capital Project] ,
            [Capital Task] ,
            ResourceName ,
            ProjectName ,
            [Project Home Group] ,
            ActualWork ,
            TimeByDay ,
            ResourceStandardRate ,
            ActualWork * ResourceStandardRate AS Dollars ,
            [Project Hierarchy]
    from    [IR.CapOnly]
    where   ( TimeByDay >= @StartDate )
            and ( [Project Home Group] IN (' + @ProjHomeGrp + ') )
            and ( TimeByDay <= @EndDate) )
            and ( ActualWork > 0 )
            and ( [Project Hierarchy] IN ( ' + @ProjHier + ' ) )
    order by ProjectName, ResourceName'

    select @paramDefs = N'@StartDate datetime, @EndDate datetime'

    exec sp_executesql @sql, @paramDefs, @StartDate = @StartDate, @EndDate =  @EndDate
end

0 comments:

Post a Comment