1

我创建了一个存储过程(在 SSRS 中使用),它的参数值大于一个。当我通过提供该参数的值在 SSMS 中执行此存储过程时,SP 不返回任何结果。它应该返回超过 1 行

下面是给出的SP

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 

您可以看到在 where 子句中,Project Home Group ( @ProjHomeGrp) 和 Project Hierarchy ( @ProjHier) 是参数,其值在提供时(大于 1)SP 返回零值。

我传递的价值是:

Start Date: 1/1/2011 
End Date: 12/31/2012
@ProjHomeGrp : PHG1,PHG2,PHG3,PHG4,PHG5,PHG6,PHG7
@ProjHier: PROH1, PROH2, PROH3

让我知道任何问题!

4

3 回答 3

1

这看起来不对:

[Project Home Group] IN ( @ProjHomeGrp )

您不能使用IN类型NVARCHAR并期望能够直接对其使用基于集合的操作。

这可能更接近您正在寻找的内容:

[Project Home Group] = @ProjHomeGrp

并且(从您的使用中猜测IN)如果您将NVARCHAR(MAX)参数用作分隔文本(例如以逗号作为分隔符的整数),则有更好的方法 - 您应该为此类事情使用表值参数

于 2012-05-07T10:47:42.077 回答
0

创建过程,如:

CREATE PROCEDURE Show_Sproceduresome
   AS 
 Begin
    select points,coursename,c.type from OfferType o,Courses c,Candidate cd, CourseOffer
     where name = 'Francisco' 
     and o.OfferId = co.OfferId 
     and co.CourseId = c.CourseId 
     and co.UCASNumber = cd.UCASNumber
 End

执行如下程序:

exec Sproceduresome

不显示结果或输出

于 2014-05-03T08:04:47.553 回答
0

正如 Oded 所说,表值参数是要走的路。然而,这里是一个基于动态 sql 的解决方案,所有的问题都可能暗示...

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 
于 2012-05-07T11:26:23.063 回答